473,396 Members | 1,945 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

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();
}
}

Mar 17 '07 #1
5 3037
Andrew Robinson <ne****@nospam.nospamwrote:
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 - <sk***@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
Mar 17 '07 #2
Andrew Robinson wrote:
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
Mar 17 '07 #3
Arne Vajhøj <ar**@vajhoej.dkwrote:
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 - <sk***@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
Mar 17 '07 #4
Thanks for the info guys. All makes sense.

-Andy

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
Arne Vajhøj <ar**@vajhoej.dkwrote:
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 - <sk***@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

Mar 17 '07 #5
Jon Skeet [C# MVP] wrote:
Arne Vajhøj <ar**@vajhoej.dkwrote:
>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.

However, I'm glad that it's disallowed, for the sake of readability.
Me too.
Mar 18 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
17
by: cheeser | last post by:
Hello all, Please see the question in the code below... Thanks! Dave #include <iostream>
5
by: klaus triendl | last post by:
hi, recently i discovered a memory leak in our code; after some investigation i could reduce it to the following problem: return objects of functions are handled as temporary objects, hence...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
13
by: asm23 | last post by:
Hi,I need some help to clarify the warning "initial value of reference to non-const must be an lvalue". I'm searching in this groups to find someone has the same situation like me. I found in...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
12
by: puzzlecracker | last post by:
is it even possible or/and there is a better alternative to accept input in a nonblocking manner?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.