CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: new Keyword

  1. #1
    Join Date
    Feb 2001
    Location
    CT,USA
    Posts
    6

    new Keyword

    The C# language asks foe a "new" keyword when I derive an interface "IDerived" from interface "IBase" and the function "getName()" is declared in both Interfaces. Does this mean , that C# doesnot allow method overridding.

    Java Developer
    SRG Systems Inc.
    CT, USA

  2. #2
    Join Date
    Nov 2000
    Location
    Belarus
    Posts
    10

    Re: new Keyword

    C# supports virtual functions overriding. But by default your IDerive.getName() isn't an override of your IBase.getName(). It's a NEW virtual function although it has the same name. The compiler warns you. If you specify explicitly that IDerive.getName()is a new virtual function by using the new keyword it will stop warn you. But if you want IDerive.getName()to be an override of IBase.getName()you MUST use the override keyword.

    public interface IBase
    {
    public virtual getName();
    }
    public interface IDerive: IBase
    {
    public override virtual getName();
    }


  3. #3
    Join Date
    Jun 1999
    Location
    Atlanta, GA USA
    Posts
    344

    Re: new Keyword

    Actually, the distinction is as follows.

    If you want polymorphism, then you need to use the virtual/override keyword pair as you suggested.

    However, if you simply want to supply a new (overridden) version of the method, then you need only define the method as new.


    Cheers,
    Tom Archer - CodeGuru
    Inside C# (early 2001)
    Teach Yourself Visual InterDev in 24 Hours

    ------
    Tom Archer, Archer Consulting Group Inc.
    Author - Inside C#, Visual C++.NET Bible, Extending MFC Apps with .NET
    http://www.ArcherConsultingGroup.com
    Consulting * Training * Custom Development * Enterprise Solutions

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured