4

I want to understand better the difference between using 'new' to allocate memory for variables and the cases when new is not required.

When I declare

int i; // I don't need to use new.

But

List<string> l = new List<string>();

Does it make sense to say "new int()" ?

user776676
  • 4,265
  • 13
  • 58
  • 77
  • 3
    "Does it make sense to say "new int()"?" Actually, it does. You'll get the default value of 0. However, it remains a value type, not a reference type. – BoltClock Nov 11 '11 at 18:26
  • You usually use `new` to allocate memory on the heap. `int i;` is on the stack. – Mike Christensen Nov 11 '11 at 18:26
  • 1
    Yes, `new int()` makes sense. There's not much to say here; go to MSDN and read the [docs](http://msdn.microsoft.com/en-us/library/fa0ab757.aspx) there. – Jon Nov 11 '11 at 18:27
  • 2
    @Mike Christensen: Not precisely — `new` on value types puts them on the stack too. – BoltClock Nov 11 '11 at 18:27
  • Yea unlike C++, where memory is allocated for an object is defined by the author of the object, not the code instantiating the object. – Mike Christensen Nov 11 '11 at 18:29
  • Don't think of it as "allocating memory", but rather "creating objects". In the case of `int i = 0` or `int i = new int()` an "object" has still been created, albeit "one that is on the stack". –  Nov 11 '11 at 18:35
  • 2
    @MikeChristensen: Others have already pointed out the flaws in that statement. I just want to add a link: If you're thinking in terms of [stack and heap](http://blogs.msdn.com/b/ericlippert/archive/2009/04/27/the-stack-is-an-implementation-detail.aspx), you're doing it wrong. –  Nov 11 '11 at 18:38
  • See also Eric Lippert's answer to [Why is it possible to instantiate a struct without the new keyword?](http://stackoverflow.com/questions/7767669/why-is-it-possible-to-instantiate-a-struct-without-the-new-keyword/7769694#7769694) – Brian Nov 11 '11 at 18:43

7 Answers7

6

You will need to use new to allocate any reference type (class).

Any value type (such as int or structs) can be declared without new. However, you can still use new. The following is valid:

int i = new int();

Note that you can't directly access a value type until it's been initialized. With a struct, using new TheStructType() is often valuable, as it allows you full use of the struct members without having to explicitly initialize each member first. This is because the constructor does the initialization. With a value type, the default constructor always initializes all values to the equivalent of 0.

Also, with a struct, you can use new with a non-default constructor, such as:

MyStruct val = new MyStruct(32, 42);

This provides a way to initialize values inside of the struct. That being said, it is not required here, only an option.

Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
3

Any reference type (such as classes) will require new. Value types (such as int) are simple values and do not require new.

Tevo D
  • 3,351
  • 21
  • 28
3

You do not need to new value types in c#. All other types you do.

DanTheMan
  • 3,277
  • 2
  • 21
  • 40
2

Have look to this MSDN documentation on new

It is also used to invoke the default constructor for value types, for example:

int myInt = new int();

In the preceding statement, myInt is initialized to 0, which is the default value for the type int. The statement has the same effect as:

int myInt = 0;

Community
  • 1
  • 1
DeveloperX
  • 4,633
  • 17
  • 22
1

int i is value type that's why you don't need to initialize and new List<string>() is reference type, you need to assign an object instance to it

Abdul Munim
  • 18,869
  • 8
  • 52
  • 61
  • 3
    No, you have to initialize a variable no matter what type it is in order to use it (meaningfully anyway). – BoltClock Nov 11 '11 at 18:28
1

Reference types must be allocated using new.

Value types do not have to be heap allocated. Integer, Double, and struct types are examples of value types. A value type that is a local var will be stored on the function call stack. A value type that is a field of a class will be stored in the class's instance data.

dthorpe
  • 35,318
  • 5
  • 75
  • 119
1

Simply check the IL: you can see the compiler emits either an 'initobj' or 'newobj'.

The initobj is emitted for both int i = 0; and int i = new int();

http://msdn.microsoft.com/en-us/library/system.reflection.emit.opcodes.initobj(v=vs.85).aspx

Gayot Fow
  • 8,710
  • 1
  • 35
  • 48