Samir Bhogayta
What is New modifiers?
By Samir Bhogayta in OOP/OOD on Sep 03 2015
  • Atish Soni
    Oct, 2019 11

    The new keyword explicitly hides a member that is inherited from a base class. When you hide an inherited member, the derived version of the member replaces the base class version.

    public class BaseC
    {
    public int x;
    public void Invoke() { }
    }
    public class DerivedC : BaseC
    {
    new public void Invoke() { }
    }

    In this example, BaseC.Invoke is hidden by DerivedC.Invoke.

    • 1
  • Anil Kumar Murmu
    Feb, 2016 5

    Now lets assume one of the scenario, if we have a function say Add(int firstNumber, int secondNumber); it is present both in parent class as well as child class. Now the rules in inheritance says if we create an instance of Child class, it has access to all public data members/functions in Child class as well as parent class. Now if we compile this code in VS, then we will get a warning to use New key word in Child class Add() method definition. This is the concept of method hiding

    • 1
  • Samir Bhogayta
    Sep, 2015 3

    The new modifiers hides a member of the base class. C# supports only hide by signature.

    • 1
  • Jeet Agrawal
    Jul, 2018 4

    New modifiers used to hide specific implementation provided by base class and allow new specific implementation of derive class.

    • 0
  • Keshav Chauhan
    Dec, 2016 26

    New modifiers used to hide specific implementation provided by base class and allow new specific implementation of derive class.

    • 0
  • Keshav Chauhan
    Dec, 2016 26

    New modifiers used to hide specific implementation provided by base class and allow new specific implementation of derive class.

    • 0
  • Prakash Tripathi
    Apr, 2016 10

    Good explanation Dheeraj Kumar Jha.

    • 0
  • Keerthi Venkatesan
    Apr, 2016 4

    new modifier creates a new member with the same name.

    • 0
  • Dheeraj Kumar Jha
    Mar, 2016 14

    New modifier 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. e.g.: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. 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 newA ref2 = new B();B ref3 = new B();ref1.Y();ref2.Y();ref3.Y();} }OutputA.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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS