methods and properties of the same name

  • Thread starter Thread starter Wiktor Zychla
  • Start date Start date
W

Wiktor Zychla

Why it is not allowed to have a methods and the properties with the same
name in a class?

For example I would like to be able to have

using System;

class C
{
int A( int p ) { return p*2; }
int A { get { return 5; } }

public static void Main() {}
}

One could argue that I can declare a method with an empty parameter list but
what I would like to know is why it could not work as shown above?

Regards
Wiktor Zychla
 
Wiktor,

While you could argue that it is possible, I think that for the sake of
ease, it is just not allowed. member (field, property, and method) must be
unique.

Hope this helps.
 
You can have class C
{
public int A(int p)
{
return p*2;
}
public int A()
{
return 5;
}
}

This does the same thing, but you will have to use int myInt = A();
 
Back
Top