Answer to Question #269338 in C# for lein

Question #269338

If we change the integer argument of the call method for Fact into 10, what happens? Explain your answer


1
Expert's answer
2021-11-21T04:49:44-0500

Integer type is value type, so when we pass it to some method as argument, it will copy just a value, but no a reference. It means that any changes inside the method will not affect real value that was passed. To change value-type into the method we have to use ref/out keywords.

 static void Main(string[] args)
        {
            int someValue = 20;
            ChangeSomething(someValue);
            Console.WriteLine($"Value = {someValue}"); //20


            ChangeSomething(ref someValue);
            Console.WriteLine($"Value = {someValue}"); //10


            Console.ReadLine();
        }




        public static void ChangeSomething(int value)
        {
            value = 10;
        }


        public static void ChangeSomething(ref int value)
        {
            value = 10;
        }

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS