Home
Search
C# new KeywordUse the new operator to call a class constructor. New is a keyword and a modifier.
dot net perls
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.
Parameters
Next We instantiate the Program class. The Program type declaration has the implicit default constructor (which is added automatically).
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); } }
New Perl() New Perl(a, b, c) New Perl() True
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.
C# program that uses new modifier
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(); // differentnew. 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.
Reference types, such as classes, are always allocated on the managed heap. And types that inherit from System.ValueType (structs) are typically allocated on the stack memory.
ValueType
Struct
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 Class, Method
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).
Home
© 2007-2021 sam allen. see site info on the changelog