about overlloading

T

Tony Johansson

Hello!

I'm reading a book and it say that I might get compile error if I overload
these two myMethods below.
I have testet but I don't get any compile error and the application work as
expected in run time.

I just wonder why the book say that I might get compile error,
I just wonder either it's correct or not correct to overload in this way.
I think that the compiler should be able to distinguish between these two it
so it does.

int myMethod(int);
int myMethod (int[]);

//Tony
 
N

Nicholas Paldino [.NET/C# MVP]

Tony,

I don't know why it said you would get a compiler error. I don't see
how. There is no violation of the language here. An array of a type is a
separate type than the type itself. Because of that, the signatures will
always vary, and you will never get a compiler error.

Hope this helps.
 
B

Bruce Wood

I'm reading a book and it say that I might get compile error if I overload
these two myMethods below.
I have testet but I don't get any compile error and the application work
as expected in run time.

I just wonder why the book say that I might get compile error,
I just wonder either it's correct or not correct to overload in this way.
I think that the compiler should be able to distinguish between these two
it so it does.

int myMethod(int);
int myMethod (int[]);

//Tony

You will not get a compiler error. You _would_ get a compiler error if
you attempted to overload like this:

int myMethod(int);
int[] myMethod(int);

because the parameters are the same in both cases. You cannot overload
changing only the return type.

However, the original code you posted compiles correctly, as you stated.
 

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