在C#中按值传递引用类型

我想按值将引用类型传递给C#中的方法。 有没有办法做到这一点。

在C ++中,如果我想通过Value传递,我总是可以依赖复制构造函数来发挥作用。 在C#中有什么办法除外:1。显式创建一个新对象2.实现IClonable然后调用Clone方法。

这是一个小例子:

让我们用C ++中的A类来实现一个拷贝构造函数。

一个方法func1(Class a),我可以通过说func1(objA)来调用它(自动创建一个副本)

C#中是否存在类似的东西。 顺便说一句,我正在使用Visual Studio 2005。

不,C#中没有等效的拷贝构造函数。 你传递的是什么(按价值)是一个参考

ICloneable也有风险,因为它的定义很差,无论是深层还是浅层(加上它得不到很好的支持)。 另一个选择是使用序列化,但同样,它可以快速绘制比您预期更多的数据。

如果担心您不希望方法进行更改,则可以考虑使该类不可变。 然后没有人可以做任何令人讨厌的事情。

正如已经解释过的那样,没有与C ++的拷贝构造函数等价的东西。

另一种技术是使用对象不可变的设计。 对于不可变对象,传递引用和副本之间没有(语义)差异。 这是System.String的设计方式。 其他系统(特别是函数式语言)更多地应用了这种技术。

这个链接shd回答你的问题 – http://msdn.microsoft.com/en-us/library/s6938f28(VS.80).aspx

根据此链接(已发布): http : //msdn.microsoft.com/en-us/library/s6938f28 (VS.80) .aspx

按值传递引用类型如下所示。 引用类型arr通过值传递给Change方法。

除非为arrays分配了新的内存位置,否则任何更改都将影响原始项目,在这种情况下,更改完全是本地方法的。

 class PassingRefByVal { static void Change(int[] pArray) { pArray[0] = 888; // This change affects the original element. pArray = new int[5] {-3, -1, -2, -3, -4}; // This change is local. System.Console.WriteLine("Inside the method, the first element is: {0}", pArray[0]); } static void Main() { int[] arr = {1, 4, 5}; System.Console.WriteLine("Inside Main, before calling the method, the first element is: {0}", arr [0]); Change(arr); System.Console.WriteLine("Inside Main, after calling the method, the first element is: {0}", arr [0]); } } 

最后,有关更深入的讨论,请参阅Jon Skeet在此问题中的post:

在C#中通过引用或值传递对象

看看这个

 public class Product { public string Name; public string Color; public string Category; public Product(Product o) { this.Name=o.Name; this.Color=o.Color; this.Category=o.Category; } // Note we need to give a default constructor when override it public Product() { } } public Product Produce(Product sample) { Product p=new Product(sample); p.Category="Finished Product"; return p; } Product sample=new Product(); sample.Name="Toy"; sample.Color="Red"; sample.Category="Sample Product"; Product p=Produce(sample); Console.WriteLine(String.Format("Product: Name={0}, Color={1}, Category={2}", p.Name, p.Color, p.Category)); Console.WriteLine(String.Format("Sample: Name={0}, Color={1}, Category={2}", sample.Name, sample.Color, sample.Category));