TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# New Class Instance

This C# example uses the new operator to call a class constructor. New instantiates types.

New instantiates a type.

It invokes the type's constructor (with a matching argument signature). After completion, the constructor returns a new instance of the specified type. We can then use the instance.

Example. We use the new operator when instantiating instances of a class. You must combine the new operator with the type name and its constructor arguments to create a new instance of the type. You can use any constructor available.

ClassConstructor

Note: The result of the new operator and the constructor invocation is an instance of the type.

Also: This program shows the default constructor that all classes have—unless another constructor is specified.

Based on:

.NET 4.5

C# program that uses new operator

using System;

class Perl
{
    public Perl()
    {
	// Public parameterless constructor.
	Console.WriteLine("New Perl()");
    }
    public Perl(int a, int b, int c)
    {
	// Public parameterful constructor.
	Console.WriteLine("New Perl(a, b, c)");
    }
}

class Program
{
    static void Main()
    {
	// Create a new instance of the Perl type.
	// ... Use the new operator.
	// ... Then reassign to another new object instance.
	Perl perl = new Perl();
	perl = new Perl(1, 2, 3);

	// Another Perl instance.
	Perl perl2 = null;
	perl2 = new Perl();

	// Instantiate the Program class.
	// ... No constructor is declared, so there is a default.
	Program program = new Program();
	Console.WriteLine(program != null);
    }
}

Output

New Perl()
New Perl(a, b, c)
New Perl()
True

This program defines two types: Perl and Program. The Perl class contains two user-defined constructors—these are invoked with the new operator. The Program class introduces the Main method and an implicit default constructor.

Tip: To call the constructors in the Perl type, specify the new operator, and then use the Perl() type with a formal parameter list.

Parameter List

Next, the program instantiates the Program class. This may be confusing. The Program type declaration does not have a constructor in the source text. The C# compiler actually inserts one for you, called the implicit default constructor.

Note: This constructor does nothing except set the memory to its default values. The default constructor is public.

Default Constructor

Usage. New has several usages in the C# language. The most common usage is for instantiating new objects. The new keyword can be used as a modifier to indicate that a method is supposed to hide a method from a derived type.

New Modifier

Also: When declaring generic classes, you can specify that a type must be a reference type with the new() constraint.

Generic Class

Memory allocation. The new operator always results in an allocation. When you use the new operator, the memory is allocated and initialized to its default value. Then the constructor logic is executed and it can change the values from their defaults.

Reference types, such as classes, are always allocated on the managed heap. And types that inherit from System.ValueType, which are considered structs, are typically allocated on the stack memory.

ValueTypeStruct

Summary. New is an operator in the C# language. It instantiates types in a unified way. We saw a user-defined and overloaded constructor, and the default constructor for classes (which has no parameters).


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf