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 | Show 1 more comment1 Answer
Reset to default 5In 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.
SetHealth()
) is a parameter - it is variable with function scope, not global scope. – topsail Commented Mar 7 at 13:47status
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:48ref
parameters (and/orout
parameters) to understand better ... – topsail Commented Mar 7 at 13:49