Re: new and override

Tech-Archive recommends: Fix windows errors by optimizing your registry



That won't work. I can just cast it to the base class and call the method.

A x = new B();
x.Print();

The real reason for the 'new' keyword is backwards compatibility.

Lets say I own class A and you own class B.
Your class B inherits from my class A. It has a print method, but mine does
not.

Six months later, I add a print method to my class A. You want to use the
new version of A, but want to keep using your version of print without
breaking mine. You add the 'new' keyword in your class, and thus any code
that was using your print will continue to do so. Unfortunately, any new
code that wants to use my print method has to cast the object to the base
type, but that is a small price for not breaking all your code.

You should never design code using the new keyword. Save it for situations
that you cannot control.

--
Jonathan Allen


"Eyal Safran" <eyal@xxxxxxxxxxx> wrote in message
news:1117821136.089125.79600@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
> yes, and also you can use new in order to change the access modifier of
> a method.
>
> so you can make a public method become protected or private.
>
> example:
>
> class A
> {
> /// <summary>
> /// The main entry point for the application.
> /// </summary>
> [STAThread]
> static void Main(string[] args)
> {
> object obj = new B();
> ((A)obj).Print(); // will work
> ((B)obj).Print(); // will not compile
> }
>
>
> public virtual void Print()
> {
> Console.WriteLine("A");
> }
> }
>
> class B : A
> {
> private new void Print()
> {
> Console.WriteLine("B");
> }
> }
>
> Eyal.
>


.



Relevant Pages

  • Re: Dynamic casting at runtime
    ... curious if you can dynamically cast an object. ... So I was hoping to have an instance of the base class, ... string methodName = 'myMethod'; ... string propertyName = 'myProperty'; ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: decorator pattern help
    ... i took out the new keyword and put override in its place. ... it doesn't provide the implementation required by the base class. ... Change "public new string Description" to "public override string ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Dynamic type convertion..
    ... As you already have a base class (it have to inherit from MBR to be ... if you cast the classes to the base class. ... Will you post an example with simple base class and 2 derived classes. ... >> public interface IMyObj ...
    (microsoft.public.dotnet.framework.remoting)
  • Re: Type Casting
    ... When you say so, then I thnink of String and Integer datatypes, where you ... >> cast is not valid.', ... > You're missing the fact that it can't be done. ... > into its base class, but you can't cast a base class to a derived class. ...
    (microsoft.public.dotnet.languages.vb)
  • using new. Do I have this right?
    ... Putting Polymorphism aside. ... in a base class is hiden in a derived class by declaring a method with ... the 'new' keyword but not the override keyword. ... But by explicitly marking it with 'new' your code ...
    (microsoft.public.dotnet.languages.csharp)