24

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.

4 Answers4

20

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.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
8

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.

Bjarke Ebert
  • 1,920
  • 18
  • 26
3

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#

Community
  • 1
  • 1
redspidermkv
  • 503
  • 10
  • 25
0

take a look at this

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));
Mohsen
  • 4,000
  • 8
  • 42
  • 73