最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

methods - Unexpected behavior of Global Scope Variable in C# - Stack Overflow

programmeradmin1浏览0评论

I have been trying to understand two code blocks. The first code block:

string status = "Healthy";

Console.WriteLine($"Start: {status}");
SetHealth(status, false);
Console.WriteLine($"End: {status}");

void SetHealth(string status, bool isHealthy) 
{
    status = (isHealthy ? "Healthy" : "Unhealthy");
    Console.WriteLine($"Middle: {status}");
}

The first code block outputs:

Start: Healthy
Middle: Unhealthy
End: Healthy

The second code block:

string status = "Healthy";

Console.WriteLine($"Start: {status}");
SetHealth(false);
Console.WriteLine($"End: {status}");

void SetHealth(bool isHealthy) 
{
    status = (isHealthy ? "Healthy" : "Unhealthy");
    Console.WriteLine($"Middle: {status}");
}

The second code block outputs:

Start: Healthy
Middle: Unhealthy
End: Unhealthy

My question is although status is set as a global variable in both the code blocks, how can SetHealth() method not alter the status variable in first code block and alter the status variable in the other code block before and after SetHealth() method call

I have been trying to understand two code blocks. The first code block:

string status = "Healthy";

Console.WriteLine($"Start: {status}");
SetHealth(status, false);
Console.WriteLine($"End: {status}");

void SetHealth(string status, bool isHealthy) 
{
    status = (isHealthy ? "Healthy" : "Unhealthy");
    Console.WriteLine($"Middle: {status}");
}

The first code block outputs:

Start: Healthy
Middle: Unhealthy
End: Healthy

The second code block:

string status = "Healthy";

Console.WriteLine($"Start: {status}");
SetHealth(false);
Console.WriteLine($"End: {status}");

void SetHealth(bool isHealthy) 
{
    status = (isHealthy ? "Healthy" : "Unhealthy");
    Console.WriteLine($"Middle: {status}");
}

The second code block outputs:

Start: Healthy
Middle: Unhealthy
End: Unhealthy

My question is although status is set as a global variable in both the code blocks, how can SetHealth() method not alter the status variable in first code block and alter the status variable in the other code block before and after SetHealth() method call

Share Improve this question asked Mar 7 at 13:43 bibashmanjusubedibibashmanjusubedi 1,2592 gold badges11 silver badges12 bronze badges 6
  • 2 In the first code block, status (in SetHealth()) is a parameter - it is variable with function scope, not global scope. – topsail Commented Mar 7 at 13:47
  • 1 "how can SetHealth() method not alter the status variable in first code block" - You have two status variables in the first example. One is global, the other is not. I'm sure there's a duplicate somewhere about C# and passing by reference/value... – David Commented Mar 7 at 13:48
  • 1 Possibly you want to look into passing ref parameters (and/or out parameters) to understand better ... – topsail Commented Mar 7 at 13:49
  • Though string is a reference type, it's also immutable, therefor when passing it as parameter, a copy is created. This copy does not point to the original and thus behaves as an unrelated local variable. – Bouke Commented Mar 7 at 14:22
  • 1 C# does not have "global variables"... stackoverflow/questions/14368129/… . Also while I can't think of a language which will behave the way you expect, it may be good idea to clarify what made you think that behavior is unexpected... Possibly you used to some other language/environment where it is the case - and people may suggest other cases that you'd find "unexpected" in C# that way. – Alexei Levenkov Commented Mar 7 at 22:07
 |  Show 1 more comment

1 Answer 1

Reset to default 5

In the first code block you've declared a parameter called status. That's a separate variable to the status variable declared at the top - they're independent, so changing the value of the parameter won't change the value of the variable. (To prove that they're entirely separate, you could change the type of the parameter to something entirely different, e.g. int.) Within the local function, the meaning of status is the parameter, not the top-level variable.

(It's unclear why you'd want that parameter anyway, mind you. It has no positive effects - it just creates the problem you've seen.)

As an aside, I wouldn't suggest generally referring to "global" variables. The code you've shown is slightly unusual compared with 99% of C# code, in that it's in a file with top-level statements. But that's effectively equivalent to creating a class with a Main method. Until you're really comfortable with the core parts of C#, you might want to avoid using top-level statements, and instead just keep an explicit Program.cs, declaring a class called Program with a method called Main. That way you won't need to worry about exactly what the top-level statements code is equivalent to.

发布评论

评论列表(0)

  1. 暂无评论