This C# keyword instantiates a type. It invokes the type's constructor (with a matching argument signature). The constructor returns a new instance of the specified type.
The new modifier may also be used to eliminate a warning when a method hides a base class
method. This is a separate use of new.
We use the new operator to instantiate a class
instance. We combine the new operator with the type name and its constructor arguments to create a new instance of the type.
class
contains 2 user-defined constructors—these are invoked with the new operator.Perl()
type with a formal parameter list.Program
class
. The Program
type declaration has the implicit default constructor (which is added automatically).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); } }New Perl() New Perl(a, b, c) New Perl() True
Short
syntax, newIt is possible to omit the type name after the "new" keyword if the compiler can figure out the type from the rest of the statement.
var
" implicit type keyword on the left. Some part of the statement must include the type.using System; class Test { public Test(string word) { Console.WriteLine(word); } } class Program { static void Main() { // Use "new" based on the type of the variable on the left. Test test = new("Hello"); } }Hello
These are expected to hide base methods. The new modifier specifies that a method is supposed to hide a base method. It eliminates a warning issued by the compiler.
class
derives from the A class
.Class
A has a public method Y, while class
B has a public method Y that uses the new keyword to hide the base method Y.using System; class A { public void Y() { Console.WriteLine("A.Y"); } } class B : A { public new void Y() { // This method hides A.Y. // It is only called through the B type reference. Console.WriteLine("B.Y"); } } class Program { static void Main() { A ref1 = new A(); // Different new. A ref2 = new B(); B ref3 = new B(); ref1.Y(); ref2.Y(); ref3.Y(); } }A.Y A.Y B.Y
The new operator always results in an allocation. When you use the new operator, the memory is allocated and initialized to its default value.
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()
constraint.New instantiates types in a unified way. We saw a user-defined and overloaded constructor, and the default constructor for classes (which has no parameters).