Re: interface mapping
From: pn (anonymous_at_discussions.microsoft.com)
Date: 03/29/04
- Next message: Jeffrey Tan[MSFT]: "RE: CD Rom used,free and capacity"
- Previous message: Mark Rae: "Re: Use PostgreSQL as DB"
- In reply to: Chris A. R.: "Re: interface mapping"
- Next in thread: Chris A. R.: "Re: interface mapping"
- Reply: Chris A. R.: "Re: interface mapping"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Jeffrey Tan[MSFT]: "RE: CD Rom used,free and capacity"
- Previous message: Mark Rae: "Re: Use PostgreSQL as DB"
- In reply to: Chris A. R.: "Re: interface mapping"
- Next in thread: Chris A. R.: "Re: interface mapping"
- Reply: Chris A. R.: "Re: interface mapping"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|