Question

I am trying to pass reference type object into a method and changing it from there. When I change it I don't have that change on the variable which is passed to method.

Here is the example:

public interface IPerson
{
    string Name { get; }
}

public class Teacher : IPerson
{
    public string Name { get; set; }

    public string LastName { get; set; }
}

//......

public void CreateTeacher(IPerson teacher)
{
    teacher = new Teacher() { Name = "Teacher name", LastName = "Teacher's last name"};
}
//.....

//I am trying this, but after I call CreateTeacher() variable teacher is null
IPerson teacher=null;
CreateTeacher(teacher);
string n = teacher.Name;

After running CreateTeacher() variable teacher is still null? Can anyone explain why? I know I could return IPerson on my CreateTeacher() method or use out, but I can't use them.

The IPerson interface is in a different dll and Teacher class is in a different dll. The method CreateTeacher is the same dll where class is.

UPDATE:

Here is something else I tried:

  IPerson teacher=new Teacher();
  CreateTeacher(teacher);
  string n = teacher.Name;//I have the changes here now

  //......

    public void CreateTeacher(IPerson teacher)
    {
        ((Teacher)teacher).Name = "My Name";
    }

What is the difference between 1st example and 2nd? Is it because of the passed value was null?

Thanks for the help!

Was it helpful?

Solution 2

In addition to RedPolygon's answer.. I feel like this might clear things up.

As explained, you're changing the value of the reference.. not the object pointed to by the reference.

Change your code to this:

public interface IPerson {
    string Name { get; set; }
}

public void CreateTeacher(IPerson teacher) {
    teacher.Name = "Teacher name";
}

IPerson teacher = new Teacher();
CreateTeacher(teacher);

..that will work. Why? A couple of reasons.

  1. It isn't null anymore, we "new it up".
  2. You aren't "new'ing it up" in the CreateTeacher method, thereby pointing the copied reference at an entirely new object. This means you're changing the object pointed to by both of your references.

It's confusing.. yes, but you understand it when you have a play around with examples like this.

EDIT:

I had to fire up paint and put my awesome MSPaint skills on display:

This is your first snippet flow:

Pass by value for reference types 1

This is my snippet (and your second one):

Pass by value for reference types 2

OTHER TIPS

C# passes by values, not references. In your case, you are passing by value a reference which you are trying to change. You want to explicitly say that you want to pass by reference your reference using ref keyword:

public void CreateTeacher(ref IPerson teacher)
{
    teacher = new Teacher() { Name = "Teacher name", LastName = "Teacher's last name"};
}

Or better yet:

public Teacher CreateTeacher()
{
    return new Teacher() { Name = "Teacher name", LastName = "Teacher's last name"};
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top