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.
- It isn't
null
anymore, we "new
it up". - You aren't "
new
'ing it up" in theCreateTeacher
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:
This is my snippet (and your second one):