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.
setis anActionandgetis aFuncfor exampledelegate {return SomeProperty}passes getter;delegate(YourType value) {SomeProperty = value}passes setter. If you only need to pass one or two places, this is shorthand for the wrapped methods you would otherwise create.Interfacewith that property, and changing the signature of the called method to take that interface as a parameter.Funcpoint directly to the property's getter method (the hiddenget_PropertyNamemethod you see in ILSpy) - which would avoid the extra indirectCALLjump.