I was thinking about List functions and not thinking that Count () was an extension method when I made the false assumption that I could write a class with the same property name and method name. The motivation in my mind at that time was that I could “parameterize a property” for special cases.
Now I understand that I can do this if I use the extension method.
Is this intentionally forbidden in classes, but allowed in extensions or just a non-existent function?
void Main()
{
var list = new List<string>();
list.Add("x");
Console.WriteLine (list.Count);
list.Add("x");
Console.WriteLine (list.Count());
var c = new C();
Console.WriteLine (c.Count);
Console.WriteLine (c.Count());
}
public class C
{
public int Count { get { return 3; } }
}
public static class X
{
public static int Count(this C c)
{
return 4;
}
}
source
share