C# method overloading in implemented interfaces?

C

cang_google

Hi everyone,

I'm having an interesting problem where it seems that method overloads
are not being handled properly in interfaces. Consider the following
code:

interface Foo
{
void f();
void f(int x);
}

When a class is created that implements Foo, the following results:

class SpecialFoo : Foo
{
#region Foo Members

public void f()
{
// TODO: Add SpecialFoo.f implementation
}

void NS.Foo.f(int x)
{
// TODO: Add SpecialFoo.NS.Foo.f implementation
}

#endregion
}

(where NS is the namespace where Foo and SpecialFoo reside).

Is this a bug or simply a language feature that I'm not aware of?
Thanks,
-calvin
 
M

Mattias Sjögren

I'm having an interesting problem where it seems that method overloads
are not being handled properly in interfaces.

What is the problem? The code you posted compiles fine here.



Mattias
 
G

Guest

I'm having an interesting problem where it seems that method overloads
are not being handled properly in interfaces.

It looks like you posted the "starter" code that Visual Studio will add for
you if you ask it to. I guess VS uses a simple algorithm - implement the
first version of "f" implicitly but the second one explicitly.

I agree this might seem like an odd algorithm, but it is just stub code that
we can change if needed. VS 2005 gives you the choice of how the stub code
should look (implicit or explicit).
 
M

Manohar Kamath

Interfaces do no contain code, the classes that implement the interfaces
need to implement the methods and properties that are defined in the
interface. Not sure what you used to code the class (VS 2005?), but the code
itself looks good to me.
 
C

cang_google

Hi folks,

Thanks for the feedback. Yes, it's VS 2003 stub code. The problem is
that I expected:

class SpecialFoo : Foo
{
#region Foo Members
public void f()
{
}

public void f(int x)
{
}
#endregion
}

rather than

void NS.Foo.f(int x)

for the overloaded version of f taking the integer argument.

I assume VS 2005 returns the stub code that I expected?

Thanks,
-calvin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top