-1

I am confused about how the code below works. Why is the output not 10 but 5?

public class Program
{
    public  void MyFunc(int x){
        x = 10;
    }
    public void Main()
    {
        int x = 5;
        MyFunc(x);
        Console.WriteLine(x);
    }
}

Thanks a lot.

Chris Catignani
  • 5,040
  • 16
  • 42
  • 49
Mina
  • 13
  • 1

1 Answers1

0

Read about the "ref keyword" Now result is 10


 public static void MyFunc(ref int x)
        {
            x = 10;
        }
        public static void Main()
        {
            int x = 5;
            MyFunc(ref x);
            Console.WriteLine(x);
        }