TheDeveloperBlog.com

Home | Contact Us

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

<< Back to C-SHARP

C# new Keyword

Use the new operator to call a class constructor. New is a keyword and a modifier.
New. This 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.Keywords
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.ClassConstructor

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); } } Output 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(); } } Output 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.ValueTypeStruct
Specification. The annotated C# specification discusses the new modifier on page 433. This modifier instructs the compiler not to issue a warning when hiding is intended.

Tip: A common programming error occurs when a developer does not realize a method is actually hiding (not overriding) a base method.

Tip 2: Many design features in the C# language are intended to reduce common errors like this one.

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).
© TheDeveloperBlog.com
The Dev Codes

Related Links:


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