I was working on a project of mine when something didn't work as I expected, and now I am reevaluating my entire being. Take the following class and method:
public class Test{
public string Name { get; set; }
public int Value { get; set; }
}
public Test ReturnExample(Test test)
{
Test example = test;
example.Name = "abc";
return example;
}
Now, if I have a FirstValue
and a SecondValue
:
Test FirstValue = new Test {Name = "First", Value = 8 };
Test SecondValue = new Test {Name = "Second", Value = 12 };
All seems pretty generic and straightforward so far but what surprised me was when you do:
SecondValue = ReturnExample(FirstValue);
What I expected the values to be after was:
FirstValue:
Name = "First"
Value = 8
SecondValue:
Name = "abc"
Value = 8
However, what I got was:
FirstValue:
Name = "abc"
Value = 8
SecondValue:
Name = "abc"
Value = 8
So, what's going on here and what can I change to get what I want?