What will be displayed in the console as a result of the following code execution:
 
static public void Main(string[] args)
 { 
int i = 10;
string s = "Hello, world"; 

Console.WriteLine("i = " + i); 
ModifyInt(i); 
Console.WriteLine("i = " + i); 

Console.WriteLine("s = " + s); 
ModifyString(s); 
Console.WriteLine("s = " + s); 
}

 static void ModifyInt(int i)
 {
 i = 99;
 }

 static void ModifyString(string s) 
{
 s = "Hello, I've been modified";
 }
 
Explanation
Variables' scope is limited by a Main method, which they are declared inside of. When called a ModifyInt method receives the value of the variable (a copy) i instead of a reference to it. Therefore, the modified variable will be destroyed after the method's execution finishes.
Same story for the s variable. Although it is of a reference type (i.e it has to be passed as a link), it does not behave as an object and a new variable with a new content is created inside of a ModifyString method.

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Cosmo
Sign Up Now
or Subscribe for future quizzes