Home
Map
new KeywordUse the new keyword to call a constructor and create a type instance. Use new on a class to hide a base method.
C#
This page was last reviewed on Dec 16, 2022.
New. 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.
An additional use. 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.
New keyword. 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
Constructor
Note The result of the new operator and the constructor invocation is an instance of the type.
Here The Perl class contains 2 user-defined constructors—these are invoked with the new operator.
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
Next We instantiate the 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, new. It 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.
Note This does not work if we use the "var" implicit type keyword on the left. Some part of the statement must include the type.
var
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
New methods. 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.
Here This program introduces 2 classes, A and B. The B class derives from the A class.
Next 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.
Note The new keyword prevents a warning from the C# compiler when this program is compiled.
Warning A "new" method can be confusing. Hiding methods is often not a good idea in program design.
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
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.
New usage. 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.
Also When declaring generic classes, you can specify that a type must be a reference type with the new() constraint.
Generic
A summary. 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).
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Dec 16, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.