Re: interface mapping

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

From: pn (anonymous_at_discussions.microsoft.com)
Date: 03/29/04


Date: Mon, 29 Mar 2004 01:16:08 -0800

I don't think it's as simple as that. Hiding an inherited member doesn't make it inaccessible:

DerivedClass derived = new DerivedClass();
int i = ((BaseClass)derived).Foo; // invokes BaseClass.Foo

By the way, the new modifier doesn't have any effect on the semantics of a member declaration. It's just a way to explicitely state that a newly introduced member hides an inherited member. If you leave it out, you'll get a compiler warning, but the program will behave the same.

     
     ----- Chris A. R. wrote: -----
     
     What's surprising about this? You're using "new" in
         public new int Foo
     which hides the BaseClass Foo. Therefore, your derived class does not have
     access to your set in Foo.
     
     Chris A.R.
     
     "pn" <anonymous@discussions.microsoft.com> wrote in message
     news:1B42DC5C-8F8E-4D33-92FA-AF592DB39CDE@microsoft.com...
> Hi all,
>> I wonder if the Microsoft C# compiler performs interface mapping according
     to the C# language specification (ECMA-334, 2nd edition December 2002).
> Take a look at the following example:
>> interface I
> {
> int Foo { get; set; }
> }
>> class BaseClass
> {
> public int Foo
> {
> get { return 0; }
> set {}
> }
> }
>> class DerivedClass: BaseClass, I
> {
> public new int Foo
> {
> get { return 0; }
> }
> }
>> Now let us locate the implementation for I.Foo. According to the language
     specification (section 20.4.2 Interface mapping), DerivedClass.Foo doesn't
     match I.Foo because DerivedClass.Foo doesn't have a set accessor. So the
     search for a matching implementation continues in BaseClass. Because
     BaseClass.Foo matches I.Foo, we are done.
>> But to my surprise, running the compiler gives the following error:
> 'DerivedClass' does not implement interface member 'I.Foo.set'
> (Note: if DerivedClass.Foo is removed, the program compiles without
     errors.)
>> I've found other similar issues, but first I'd like to get your input on
     this one.
> Any comments would be greatly appreciated!
>> Regards,
> pn
     
     
     



Relevant Pages

  • Re: Static member function in abstract base class
    ... >have an Abstract Base Class used as base class for differentes ... >class AbstractBaseClass { ... >int main ... class DerivedClass: public AbstractBaseClass { ...
    (comp.object)
  • Re: need interface methods to be virtual-able
    ... public override int CompareTo(object obj) { ... What I see is that the "CompareTo" coming from IComparable should be ... DerivedClass d1 = new DerivedClass; ...
    (microsoft.public.dotnet.general)
  • Re: need interface methods to be virtual-able
    ... public override int CompareTo{ ... static void Main ... What I see is that the "CompareTo" coming from IComparable should be ... DerivedClass d1 = new DerivedClass; ...
    (microsoft.public.dotnet.general)