Search code examples
methodsprintinginstancevarref

c# printing after call methods


   public class A
{
    public int A1 { get; set; }
    public int A2 { get; set; }
}

 static void Main(string[] args)
    {
        
        int i = 0;
        Method1(i);
        Console.WriteLine($"i={i}");
        var str = "This is a string";
        Method2(str);
        Console.WriteLine(str);
        var a = new A()
        {
            A1 = 5,
            A2 = 6
        };
        Method3(a);
        Console.WriteLine($"a.A1={a.A1}, a.A2={a.A2}");
        Method4(a);
        Console.WriteLine($"a.A1={a.A1}, a.A2={a.A2}");
        Method5(ref a);
        Console.WriteLine($"a.A1={a.A1}, a.A2={a.A2}");
      
        
    }
 private static void Method5(ref A a)
    {
        a = new A()
        {
            A1 = 10,
            A2 = 11
        };
    }

    private static void Method4(A a)
    {
        a = new A()
        {
            A1 = 10,
            A2 = 11
        };
    
    }

    private static void Method3(A a)
    {
        a.A1 = 6;
    }

    private static void Method2(string str)
    {
        str = "This is a new string";
    }

    private static void Method1(int i)
    {
        i = 5;
    }

the output I expect vs the real output

I thought that output will be : i=0

This is a string a.A1=5, a.A2=6 a.A1=5, a.A2=6 a.A1=10, a.A2=11 but the output is : i=0 This is a string a.A1=6, a.A2=6 a.A1=6, a.A2=6 a.A1=10, a.A2=11

I will be glad to get explanation how method3 and method 4 are working. Thanks a lot !


Solution

  • Let us take a look at Method3(a)

    private static void Method3(A a)
        {
            a.A1 = 6;
        }
    

    The previous value was 5, right? Now it's declared as 6 and the official value is now 6. You are referring to the variable A1 and giving it a new value!

    Console.WriteLine($"a.A1={a.A1}, a.A2={a.A2}");
    

    So basically you override the new value (6) with the old value (5), because you're using a reference type.

    So when we hop into Method4(a) the values are A1 = 6 and A2 = 6. The reason to why Method4 isn't changing to A1 = 10 and A2 = 11 is because you are not referring to anything. There is no reference type. It doesn't know that you're referring to the object a just because you passed the variable name as an argument.

    So you've pretty much initialised a new object of class A in Method4 with no references. When it has no references, it doesn't know where to store the values in Method(4) hence why the values aren't changing.

    If you look at Method5(a), you use the "ref" keyword that means the ref keyword indicates that a value is passed by reference. The ref points at the object you have initialised at first.

    I know it might be confused, but I can highly recommend using debug and then follow it step-by-step. Take your time, note down what is changing and like that you will understand much better.

    Read these articles: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument-by-reference https://www.codecademy.com/learn/learn-c-sharp/modules/learn-csharp-references

    If you find my post helpful, please upvote it.

    Happy coding :-)