-4

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

6
  • 2
    In the first code block, status (in SetHealth()) is a parameter - it is variable with function scope, not global scope. 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... Commented Mar 7 at 13:48
  • 1
    Possibly you want to look into passing ref parameters (and/or out parameters) to understand better ... 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. Commented Mar 7 at 14:22
  • 1
    C# does not have "global variables"... stackoverflow.com/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. Commented Mar 7 at 22:07

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.