Reputation: 9396
public void MethodSample1(Itest variable)
{
variable.TestString = "some sample data";
Itest var1 = variable;
Console.WriteLine(variable.TestString);
MethodSample2(variable);
Console.WriteLine(variable.TestString);
Console.WriteLine(var1.TestString);
}
public void MethodSample2(Itest variable)
{
variable.TestString = "testdata";
}
public interface Itest
{
string TestString { get; set; }
}
Expected both the console output lines print "some sample data" but it seems that TestString
is being overwritten with the new value? is it not like "by default in C# all the values are passed by value?".
In short, how to preserve the value of "TestString" in MethodSample1
?
(I ran into this problem because all my projects are based upon a single interface)
Even after preserving the value, it does reflect! strange!
Upvotes: 2
Views: 1032
Reputation: 39650
The parameter is indeed passed by value, but the value you are passing is a reference to the original object.
If you want to preserve the original object's properties, you'll need to pass a copy of the original object. You could add a Clone() method to the interface or something similar:
public interface ITest
{
string TestString { get; set; }
ITest Clone();
}
public class Test : ITest
{
string TestString { get; set; }
ITest Clone() {
return new Test() {
TestString = this.TestString
};
}
}
Or, you could rethink your current approach. Do you really need to change the property of the interface? Or could you use a variable of type string instead?
Upvotes: 2
Reputation: 13882
how to preserve the value of "TestString" in MethodSample1?
Store it in a local variable.
public void MethodSample1(Itest variable)
{
variable.TestString = "some sample data";
string localTestString = variable.TestString;
Console.WriteLine(variable.TestString);
MethodSample2(variable);
variable.TestString = localTestString;
Console.WriteLine(variable.TestString);
}
But, this is wrong way of doing things. If you tell a little more what do you want to achieve, we could help more.
Upvotes: 1
Reputation: 223392
For your current problem, I don't think you can prevent any method from modifying the parameter passed to it.
Your variable
is a reference type object, In C# reference type's address is passed by value to a method, that is why you are seeing the change. For example if your method is written like:
public void MethodSample2(Itest variable)
{
variable = null;
}
You won't see the change in your MethodSample1
method, since the reference address is passed by value.
is it not like "by default in C# all the values are passed by value?".
Value types are passed by value, in your case variable
is a reference type.
You should see Parameter Passing C# by Jon Skeet
Not sure why you have to modify the passed object, For workaround you can create a temporary copy of the property and then set that value before existing from the method.
Upvotes: 5