简体   繁体   中英

why the value of class member variable change?

i have a class

    class ABC
    {
          public int a = 2;
          public void valueA(ABC objabc)
          {
               a = 5;
               objabc = new ABC();
               objabc.a = 11;
           }
    }

then i write main as

    static void Main(string[] args)
    {
        ABC objabc = new ABC();
        objabc.a = 15;
        objabc.valueA(objabc);
        Console.WriteLine(objabc.a);
     }

When i execute this i found 5 in output.so my question is that why a = 5??why it is not 2, 11 or 15??

You are calling the valueA method on the object objabc . Inside the method the member a of this is set to 5. You can see the code as equivalent to:

class ABC
{
      public int a = 2;
      public void valueA(ABC objabc)
      {
           this.a = 5;
           objabc = new ABC();
           objabc.a = 11;
       }
}

So you are setting the member of the object on which you called the method to 5. This object being objabc from main , so the final value is 5. The fact that you are assigning it a new object reference afterwards makes no difference, because you are not passing it by reference so changes are not visible outside.

The result would be different (ie 11) if your code was this:

class ABC
{
      public int a = 2;
      public void valueA(ref ABC objabc)
      {
           a = 5;
           objabc = new ABC();
           objabc.a = 11;
       }
}

In this case the parameter would be passed by reference, so the assignment objabc = new ABC(); would be visible to the calling code (ie in Main ).

First you create an instance of ABC . This will set the value of a to 2. Then you set the value of a to 15 using the field directly. Then you call a method which sets the very same field to 5 and then it creates another instance in which you set the value of its version of a to 11. This other instance doesn't affect the first instance.

static void Main(string[] args)
{
    ABC objabc = new ABC(); // Value of a is 2
    objabc.a = 15; // Set it to 15
    objabc.valueA(objabc); // This resets it to 5, then creates a new object and sets it to 11
    Console.WriteLine(objabc.a); // You're still calling a on the old object, not the new one
 }

A class is a reference type. When you pass as a parameter "ABC objabc", your method creates a new objabc. It goes away when you exit the method because you don't return anything. So, you can: - Return your object and then print this object, returned by your method or; public ABC valueA(ABC objabc); - Pass the keyword "ref" before your objabc, to keep the reference to your class created before (IE: public void valueA(ref ABC objabc).

But it don't make many sense, pass the class object to the same class.

Good luck!

http://msdn.microsoft.com/en-us/library/ms173109.aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM