简体   繁体   中英

Passing Reference types by value in C#

I want to pass a reference type by value to a method in C#. Is there a way to do it.

In C++, I could always rely on the copy constructor to come into play if I wanted to pass by Value. Is there any way in C# except: 1. Explicitly creating a new object 2. Implementing IClonable and then calling Clone method.

Here's a small example:

Let's take a class A in C++ which implements a copy constructor.

A method func1(Class a), I can call it by saying func1(objA) (Automatically creates a copy)

Does anything similar exist in C#. By the way, I'm using Visual Studio 2005.

No, there is no copy-constructor equivalent in C#. What you are passing (by value) is a reference .

ICloneable is also risky, since it is poorly defined whether that is deep vs shallow (plus it isn't very well supported). Another option is to use serialization, but again, that can quickly draw in much more data than you intended.

If the concern is that you don't want the method making changes, you could consider making the class immutable. Then nobody can do anything nasty to it.

As already explained, there's no equivalent to C++'s copy constructors.

Another technique is to use a design where the objects are immutable. For immutable objects there is no (semantic) difference between passing a reference and a copy. This is the way System.String is designed. Other systems (notably functional languages) apply this technique much more.

According to this link (already posted): http://msdn.microsoft.com/en-us/library/s6938f28(VS.80).aspx

Passing a reference type by value is shown below. The reference type arr is passed by value to the Change method.

Any changes will affect the original item unless the array is assigned a new memory location in which case, the change is entirely local to the method.

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]);
    }
}

In closing, for a more in depth discussion, see the post by Jon Skeet in this question:

Passing Objects By Reference or Value in C#

此链接应该包含您的问题的答案-http: //msdn.microsoft.com/zh-cn/library/s6938f28(VS.80).aspx

take a look atthis

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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM