Connecting Tech Pros Worldwide Forums | Help | Site Map

static and non-static methods with the same name and signature

Andrew Robinson
Guest
 
Posts: n/a
#1: Mar 17 '07
I would think that I could do this but apperently not. "'Class1' already
defines a member called 'Read' with the same parameter types"

public class Class1 {

public void Read() { }

public static void Read() {
new Class1().Read();
}
}


Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#2: Mar 17 '07

re: static and non-static methods with the same name and signature


Andrew Robinson <nemoby@nospam.nospamwrote:
Quote:
I would think that I could do this but apperently not. "'Class1' already
defines a member called 'Read' with the same parameter types"
>
public class Class1 {
>
public void Read() { }
>
public static void Read() {
new Class1().Read();
}
}
From the C# 1.1 spec, section 17.5:

<quote>
The name of a method must differ from the names of all other non-
methods declared in the same class. In addition, the signature of a
method must differ from the signatures of all other methods declared in
the same class.
</quote>

Whether a method is static or not isn't part of the signature.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
=?ISO-8859-1?Q?Arne_Vajh=F8j?=
Guest
 
Posts: n/a
#3: Mar 17 '07

re: static and non-static methods with the same name and signature


Andrew Robinson wrote:
Quote:
I would think that I could do this but apperently not. "'Class1' already
defines a member called 'Read' with the same parameter types"
>
public class Class1 {
>
public void Read() { }
>
public static void Read() {
new Class1().Read();
}
}
Of course.

Due to the fact that static methods can be called
"on instances", then it has to be this way.

Arne
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Mar 17 '07

re: static and non-static methods with the same name and signature


Arne Vajhøj <arne@vajhoej.dkwrote:
Quote:
Of course.

Due to the fact that static methods can be called
"on instances", then it has to be this way.
Fortunately, C# prevents you from calling

someInstance.StaticMethod();

but of course you can do

StaticMethod();

from an instance method.

It would be possible to allow this and "default" to the instance method
if there were two methods of the same name, because you could always
use the type name to call the static method if necessary.

However, I'm glad that it's disallowed, for the sake of readability.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Andrew Robinson
Guest
 
Posts: n/a
#5: Mar 17 '07

re: static and non-static methods with the same name and signature


Thanks for the info guys. All makes sense.

-Andy

"Jon Skeet [C# MVP]" <skeet@pobox.comwrote in message
news:MPG.206651055bf477e198d9c4@msnews.microsoft.c om...
Arne Vajhøj <arne@vajhoej.dkwrote:
Quote:
Of course.
>
Due to the fact that static methods can be called
"on instances", then it has to be this way.
Fortunately, C# prevents you from calling

someInstance.StaticMethod();

but of course you can do

StaticMethod();

from an instance method.

It would be possible to allow this and "default" to the instance method
if there were two methods of the same name, because you could always
use the type name to call the static method if necessary.

However, I'm glad that it's disallowed, for the sake of readability.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Mike Schilling
Guest
 
Posts: n/a
#6: Mar 18 '07

re: static and non-static methods with the same name and signature


Jon Skeet [C# MVP] wrote:
Quote:
Arne Vajhøj <arne@vajhoej.dkwrote:
Quote:
>Of course.
>>
>Due to the fact that static methods can be called
>"on instances", then it has to be this way.
>
Fortunately, C# prevents you from calling
>
someInstance.StaticMethod();
>
but of course you can do
>
StaticMethod();
>
from an instance method.
>
It would be possible to allow this and "default" to the instance
method if there were two methods of the same name, because you could
always use the type name to call the static method if necessary.
In which case

ambiguousName();

in a static method would have a different meaning from

ambiguousName()

in an instance method. Probably not the best idea.

Quote:
However, I'm glad that it's disallowed, for the sake of readability.
Me too.


Closed Thread