TheDeveloperBlog.com

Home | Contact Us

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

C# New Modifier

This C# program uses the new modifier in a derived class. New eliminates a warning.

New methods 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. No functionality is changed. But an annoying warning is silenced.

Example. This program introduces two 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.

Class

Note: The new keyword prevents a warning from the C# compiler when this program is compiled.

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(); // Different new
	A ref2 = new B();
	B ref3 = new B();

	ref1.Y();
	ref2.Y();
	ref3.Y();
    }
}

Output

A.Y
A.Y
B.Y

In this example, we use the A and B types. With ref1, the base class is used directly. This results in the A.Y method being used. For ref2, the base class is used with a derived class. This results in the A.Y method being used again.

Tip: For ref2, the A.Y method is used because the override keyword is not used on B.Y.

Override

Finally: With ref3, the derived class is used directly. The "new" method B.Y is thus used.

Specification. The annotated C# specification discusses the new modifier on page 433. The main reason the new modifier is included in the language is to instruct the compiler not to issue a warning when hiding is intended.

Apparently, a common programming error occurs when a developer does not realize a method is actually hiding (not overriding) a base method. Many design features in the C# language are intended to reduce common errors like this one.

Summary. The new keyword has multiple uses. We looked at the new modifier on a method in a derived type. We then demonstrated how the object hierarchy is used to locate the correct implementation to call.

Review: We looked at some reference material for the new modifier. The new modifier is not the same as the new class constructor.

New


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