1

Suppose I have an object:

public class A
{
    private int m_a;
    private int m_b;
    A(int a, int b)
    {
        m_a = a;
        m_b = b;
    }
}

This gets the job done but I have a hazy memory of being told it unnecessarily copies the integers into arguments a and b and then into m_a and m_b. Is there a way to define the class such that the parameters pass straight through to their member counterparts?

Please note that I don't want to discuss C# Object Constructor - shorthand property syntax as here the onus is upon the person using the class.

Of course I could be entirely wrong and maybe the compiler removes such trivialities thus I should be happy to be educated either way.

Community
  • 1
  • 1
Robino
  • 4,530
  • 3
  • 37
  • 40

5 Answers5

6

This works but I have a hazy memory of being told it unnecessarily copies the integers into arguments a and b and then into m_a and m_b.

Yes, the values are copied into the parameters a and b, and then into the state of the object itself.

Fundamentally that's just part of how member invocations work, and is unavoidable. A future version of C# may well have a way to make that simpler to express in source code, but the "overhead" which is present will still be present - and is utterly insignificant in the vast, vast majority of programs. (It may be optimized away by the JIT compiler by inlining, in fact... but you should almost certainly not worry about it.)

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
1

I think you're thinking of C++ member initialization lists.

Sean
  • 60,939
  • 11
  • 97
  • 136
1

The only thing I can think of is using ref, which will work like a pointer in C++, but that isn't the thing you want to use I guess since it has some other implications...

Also, they don't prevent copying the int in the second step (setting m_a)

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
1

The best that you could do, if there is a lot of such fields that have to be assigned in the constructor, is to use a struct to both pass your parameters and store the values.

In the end you have to supply your parameter values anyway, either by storing them in a structure or by passing them explicitely as constructor arguments. But it will spare you a lot of time if you do not have to work with those values one-by-one all the time.

swalex
  • 3,885
  • 3
  • 28
  • 33
1

The way that constructors are designed in C# I can't se how that could be posible due to the memory handling of .Net.

public void Foo()
{       
   var a = new A(1,2);
   //The 1 and 2 values can safely be removed from memory here.
}
public class A
{
   int _a;
   int _b;
   public A(int a, int b)
   {
      _a = a;
      _b = b;
   }
}

You won't have your haze of memory in unnecessarily copies the integers as the constructor parameteres are removed from memory. Think of the required life time of the storage of fields. In this case 1 and 2 only needs to be stored to the construktor is executed. But _a and _b will exsist in the life time of the object a. This means you need to have some means of promoting arguments in a constructor into either fields int the class or properties in order to persist them in the life time of a.

I think this might clear it up for you http://blogs.msdn.com/b/ericlippert/archive/2010/09/30/the-truth-about-value-types.aspx

Frode
  • 336
  • 4
  • 15