RSS

Functionality of the “new” keyword in C#

02 May

When you declare a class variable, it is initially unassigned. To use a class variable, you must create an instance of the corresponding class and assign it to the class variable. To create an instance of a class, you use the new operator.

The new operator does two things:

  • It causes the CLR to allocate memory for your object
  • It then invokes a constructor to initialize the fields in that object.

The version of the constructor that runs depends on the parameters that you specify for the new operator. The following code example shows how to create and use instances of the House class

House MyHouse = new House();
House YourHouse = new House();

If you call new and do not specify any parameters, the default constructor runs. Remember that if you define one or more constructors for a class, the C# compiler does not create a default constructor for you automatically.

 
Leave a comment

Posted by on May 2, 2012 in Visual C# . NET

 

Tags: , , , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: