Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

New {object} vs {object} = new {object}

Tags:

.net

vb.net

I was just wondering if there is any difference between the two different new object initializers or is it just syntactic sugar.

So is:

Dim _StreamReader as New Streamreader(mystream)

and different to:

Dim _StreamReader as Streamreader = new streamreader(mystream)

Is there any difference under the hood? or are they both the same? Which one do you prefer to use?

like image 558
Nathan W Avatar asked Nov 16 '08 23:11

Nathan W


People also ask

What is the difference between object create and new object?

The major difference is that Object. Create returns the new object while the constructor function return the constructor of the object or the object. This is due to the important difference that new actually runs constructor code, whereas Object. create will not execute the constructor code.

What is new object ()?

Using new Object() allows you to pass another object. The obvious outcome is that the newly created object will be set to the same reference. Here is a sample code: var obj1 = new Object(); obj1. a = 1; var obj2 = new Object(obj1); obj2.

What is the difference between creating an object using new () and create () methods?

create() is factory construction. You are delegating new() to the factory - the factory looks for overrides and replaces construction of your class with some other derived class. You should always use create() rather than using new() for classes registered with the factory.

What is the difference between an object and an object literal?

Objects created using object literal are singletons, this means when a change is made to the object, it affects the object entire the script. Whereas if an object is created using constructor function and a change is made to it, that change won't affect the object throughout the script.


1 Answers

In VB.NET, they're identical. The As New variant is canonical.

In VB6, their semantics actually differed (apart form the obvious fact that VB6 didn't allow assignments in declarations): the As New variant would create an object that could never be Nothing. Rather, the runtime would ensure that the object was always properly initialized before each access to it.

like image 164
Konrad Rudolph Avatar answered Nov 24 '22 13:11

Konrad Rudolph