C# syntax

  • Thread starter Vladimir Nesterovsky
  • Start date
V

Vladimir Nesterovsky

Hello there.

Why does one need to use "new" keyword in C# in order to construct object?
Does semantic of constructor invoke have other meaning except of object
construction in the heap (or value type in a stack)?
 
J

Jon Skeet [C# MVP]

Vladimir Nesterovsky said:
Why does one need to use "new" keyword in C# in order to construct object?

It helps to make the difference between methods and constructors
obvious.
Does semantic of constructor invoke have other meaning except of object
construction in the heap (or value type in a stack)?

Not as far as I'm aware - what kind of thing were you thinking of?
 
V

Vladimir Nesterovsky

Why does one need to use "new" keyword in C# in order to construct
object?
It helps to make the difference between methods and constructors
obvious.


Not as far as I'm aware - what kind of thing were you thinking of?

I just noticed some code that is very overloaded with this keyword. :) In
C# it has no useful semantic meaning in my opinion.
 
J

Jon Skeet [C# MVP]

Vladimir Nesterovsky said:
I just noticed some code that is very overloaded with this keyword. :)

What do you mean by "overloaded" here?
In C# it has no useful semantic meaning in my opinion.

Its use is in terms of readability, and I for one am glad it's there.
 
M

Morten Wennevik

Maybe

MyObject mobj; // mobj is currently empty and needs to be assigned an
object
mobj = new MyObject();
OR
mobj = new MyObject(int number);
OR
mobj = new MyObject(int string);
OR
....

The new keyword simply indicates that a new MyObject is created and the
reference to the object is put into mobj.
 
G

Guest

overloading just means that, different ways of constructing, look at the
relevant ctor to see what its doing (usually just setting fields)


Maybe

MyObject mobj; // mobj is currently empty and needs to be assigned an
object
mobj = new MyObject();
OR
mobj = new MyObject(int number);
OR
mobj = new MyObject(int string);
OR
....

The new keyword simply indicates that a new MyObject is created and the
reference to the object is put into mobj.
 
D

Daniel O'Connell

Vladimir Nesterovsky said:
Hello there.

Why does one need to use "new" keyword in C# in order to construct object?
Does semantic of constructor invoke have other meaning except of object
construction in the heap (or value type in a stack)?
It seems rather unlikely, but the following code construct can exist:

class MyTestObject
{

public MyTestObject()
{
//do constructor stuff
}
}

class MyOtherObject
{
//the meanings are different between these two.
//this one obviously creates a new object;
MyTestObject myObject = new MyTestObject();
//but what does this one do?
MyTestObject myObject = MyTestObject();

MyTestObject MyTestObject()
{
return null;
}

}

without using the new keyword, this code would be invalid, which would be
VERY unpleasent when someone decided to come up with a class with the same
name as one of your methods. As I said, its unlikely, but it IS possible.
 
J

Jon Skeet [C# MVP]

overloading just means that, different ways of constructing, look at the
relevant ctor to see what its doing (usually just setting fields)

Yes, I know what overloading is, but it doesn't seem to fit in with
"some code which is very overloaded with this keyword".
 
M

Morten Wennevik

This is confusing because the method MyTestObject has the same name as the
class MyTestObject

You could do
string mystring;
mystring = MyTestObject();

but this MyTestObject has nothing to do with the class of the same name.
With the new keyword you create a new object of the type name
MyTestObject. Framework then assigns memory to fit the new object and
calls the constructor for the object.
 
M

Morten Wennevik

Well, you would probably get a compiler error seeing as MyTestObject()
returns a MyTestObject
 
K

Kirill Ritvinsky

Jon said:
Yes, I know what overloading is, but it doesn't seem to fit in with
"some code which is very overloaded with this keyword".

I think, "some code which is very overloaded with this keyword" means
that the "new" keyword occurs too many times in some portion of code.

It's just not very applicable translation from Russian :)
 
D

Daniel O'Connell

Hrmm, your posts won't quote in OE, thats weird.

Anyway, thats exactly the point. He asked why the new keyword, so in the
above case when
MyTestObject obj = MyTestObject();
is called, what will obj be? Will it call the method or will it call the
constructor of MyTestObject. Without the new keyword, a strange spec
declaration would have to exist to differentiate between these situations.
The point of it was to illustrate the need for the new keyword in object
construction. The new keyword at the very least differentiates from this
problem.

This is confusing because the method MyTestObject has the same name as the
class MyTestObject

You could do
string mystring;
mystring = MyTestObject();

but this MyTestObject has nothing to do with the class of the same name.
With the new keyword you create a new object of the type name
MyTestObject. Framework then assigns memory to fit the new object and
calls the constructor for the object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top