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

  • Thread starter Thread starter Andrew Robinson
  • Start date Start date
A

Andrew Robinson

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();
}
}
 
Andrew Robinson said:
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.
 
Andrew said:
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
 
Arne Vajhøj said:
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.
 
Thanks for the info guys. All makes sense.

-Andy

Arne Vajhøj said:
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 said:
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.

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

Me too.
 
Back
Top