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 in. ModifyInt method receives the value of the i variable instead of its reference as a parameter. Therefore, the modified variable will be destroyed after the method's execution.
Similarly, for the s variable. Although it has 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 a ModifyString method.

Follow CodeGalaxy

Mobile Beta

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