C#中按值传递
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11142177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass by Value in C#
提问by ViV
How can I pass an object of a "MyClass" (C#) by Parameter-by-Value to a method? example:
如何通过 Parameter-by-Value 将“MyClass”(C#)的对象传递给方法?例子:
MyClass obj = new MyClass();
MyClass.DontModify(obj); //Only use it!
Console.Writeline(obj.SomeIntProperty);
...
...
public static void DontModify(MyClass a)
{
a.SomeIntProperty+= 100;// Do something more meaningful here
return;
}
采纳答案by Daniel Pe?alba
By default object types are passed by value in C#. But when you pass a object referenceto a method, modifications in the object are persisted. If you want your object to be inmutable, you need to cloneit.
默认情况下,对象类型在 C# 中按值传递。但是,当您将对象引用传递给方法时,对象中的修改将被持久化。如果您希望对象不可变,则需要克隆它。
In oder to do it, implement the ICloneableinterface in your class. Here is a mock example of how to use ICloneable:
为此,请在您的类中实现ICloneable接口。这是一个如何使用 ICloneable 的模拟示例:
public class MyClass : ICloneable
{
private int myValue;
public MyClass(int val)
{
myValue = val;
}
public void object Clone()
{
return new MyClass(myValue);
}
}
回答by Reed Copsey
By default, it is passed by value. However, you're passing the object referenceby value, which means you can still edit values within the object.
默认情况下,它是按值传递的。但是,您是按值传递对象引用,这意味着您仍然可以编辑对象内的值。
In order to prevent the object from being able to change at all, you would need to actually clone the object prior to passing it into your method. This would require you to implement some method of creating a new instance that is a copy of your original object, and then passing in the copy.
为了防止对象根本无法更改,您需要在将其传递到您的方法之前实际克隆该对象。这将要求您实现一些创建新实例的方法,该实例是原始对象的副本,然后传入该副本。
回答by MusiGenesis
public static void DontModify(MyClass a)
{
MyClass clone = (MyClass)a.Clone();
clone.SomeIntProperty+= 100;// Do something more meaningful here
return;
}
回答by Dan Lister
You could create a Clone method on your object to pass the return value to your method. C# cannot pass reference types by value so this might be a good alternative.
您可以在您的对象上创建一个 Clone 方法以将返回值传递给您的方法。C# 不能按值传递引用类型,因此这可能是一个不错的选择。
public MyClass CreateClone()
{
return new MyClass() { SomeIntProperty = this.SomeIntProperty };
}
回答by alsafoo
class Program
{
static void Main(string[] args)
{
Person p1 = new Person()
{
Name = "Alsafoo",
Address = new Address()
{
City = "Chicago"
}
};
Person p2 = new Person(p1.Address);
p2 = p1.GetClone(CloningFlags.Shallow);
p2.Name = "Ahmed";
p2.Address = new Address(){City = "Las Vegas"};
Console.WriteLine("p1 first name: {1} --- p1 city: {2} {0}p2 first name: {3} ---- p2 city: {4}",
Environment.NewLine, p1.Name, p1.Address.City, p2.Name, p2.Address.City);
Console.ReadKey();
}
}
public class Person
{
public Person()
{}
public Person(Address a)
{
Address = a;
}
public string Name { get; set; }
public Address Address { get; set; }
}
public class Address
{
public string City { get; set; }
}
Download this extension https://www.nuget.org/packages/CloneExtensions/1.2.0

