Change class property value in method

10,973

Solution 1

Is the m which is pass to the method is a copy of the object or it's just reference.

You are passing a copy of the "m" reference to the methods. If you want to pass the actual reference to the MyClass object in memory you could use the ref keyword:

public class Program
{
    public static void Main(string[] args)
    {
        Myclass m = new Myclass();
        SetToNull(ref m);
        if(m == null)
            Console.WriteLine("NULL!");
        Console.ReadKey();
    }

    static void SetToNull(ref Myclass m)
    {
        m = null;
    }
}

Then the SetToNull method will set the actual "m" reference to a null reference.

Solution 2

First i want to notice that you are asking about field and not an attribute, attribute are configured this way:

public class Myclass
{
    public int someProp{ get; set; }
}

About Your Question you are right, when you pass a instance to method you are passing a "pointer" to this instance, you cannot replace the instance itself this way, you can just change it properties/fields. in order to replace/change the instance itself you need to pass a reference using the ref keyword.

the fixed code will be:

public class Myclass
{
    public int someProp;
}
public class Program
{
    public static void Main(string[] args)
    {
        Myclass m = new Myclass();
        Console.WriteLine(m.someProp);
        ChangeValue(m);
        Console.WriteLine(m.someProp);
        SetToNull(ref m);
        Console.WriteLine(m.someProp);
        Console.ReadKey();
    }
    static void ChangeValue(Myclass m)
    {
        m.someProp = 10;
    }
    static void SetToNull(ref Myclass m)
    {
        m = null;
    }
}

some places to read for more information:

Solution 3

Using your SetToNull function as an example, it can be rewritten as below. Thus you can see you are not changing the value of m in your main function, but _m instead. _m is also a reference, which is a copy of m.

static void SetToNull(Myclass _m)
{
    _m = null;
}

Solution 4

The m passed around is just a reference.

When you set a property of the object, you're changing a property of the referenced object.

When you set m itself to null, you're changing what m references which is why your original reference in Main still points to the original object.

Share:
10,973
E_N_Y
Author by

E_N_Y

Updated on June 04, 2022

Comments

  • E_N_Y
    E_N_Y almost 2 years

    I have following code

    public class Myclass
    {
        public int someProp{ get; set; };
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            Myclass m = new Myclass();
            Console.WriteLine(m.someProp);
            ChangeValue(m);
            Console.WriteLine(m.someProp);
            SetToNull(m);
            Console.WriteLine(m.someProp);
            Console.ReadKey();
        }
        static void ChangeValue(Myclass m)
        {
            m.someProp = 10;
        }
        static void SetToNull(Myclass m)
        {
            m = null;
        }
    }
    

    The result is 0 10 10
    I'm wondering why after I set the class to null it shows 10.
    Is the m which is pass to the method is a copy of the object or it's just reference.

    • René Vogt
      René Vogt over 7 years
      in addition to Justin's answer, if you use different names for your variables and parameters, your mistake should become more obvious.
  • Daniel Henao
    Daniel Henao almost 4 years
    I know the question is very old, but what happens when instead of a property of a class, i want to change the value of a partial class property... so, in ASP, for example, depending on a view action,I call a method in wich i have to perform update over my class property.... So, just when another event of the same kind calls the same method to update the property, THAT property doesnt have the previous value, why?