We all know that properties in C# get compiled to actual plain old methods. But unlike method(-group)s, they can't be given as arguments to other methods that expect a delegate like a Func<T>
or Action<T>
(getter and setter). Is there anything special prohibiting this, or is it just a feature that didn't come "for free" when making method groups implicitly convertible to delegates?
Example code:
public class MyClass
{
public static int X { get; set; }
}
private static void Foo<T>(Func<T> f)
{
Console.WriteLine(f());
}
private static void Bar<T>(Action<T> f)
{
f(default(T));
}
private static void Test()
{
// neither of these lines compile, even with an explicit type-cast.
Foo(MyClass.X);
Bar(MyClass.X);
}
If I had to guess, I'd say that the syntactical problem of differentiating between invocations and references to the property itself wasn't worth solving when we can just do the extra fuzz of writing () => MyClass.X
or x => MyClass.X = x
.