Reputation: 3523
Must be going crazy here but I can't see what I'm doing wrong in the following code to cause the syntax error
CS0828: Cannot assign 'method group' to anonymous type property
I can't see anything anonymous about the code, the property has a name. The method I assign it it is named etc.
// A basic function that takes an int and has void return
private static void DoSomething(int d)
{
// .... do stuff
};
// A class is defined with an Action<int> delegate
private class XX
{
public Action<int> YY { get; set; }
}
// An instance of the class which clearly assigns a known method to the property
private static readonly XX xx = new
{
// error "CS0828: Cannot assign 'method group' to anonymous type property"
YY = DoSomething
};
Upvotes: 0
Views: 167
Reputation: 3024
Actually there are 3 solutions to fix the compile error:
// Object initializer with optional constructor parentheses
XX xx = new XX() { YY = DoSomething };
// Object initializer without constructor parentheses
XX xx = new XX { YY = DoSomething };
// Target-typed new expression together with object initializer
XX xx = new() { YY = DoSomething };
Upvotes: 0
Reputation: 1524
More eleborations on Sweeper answers.
In C#, new { }
is used to create an instance of an anonymous type, which is a type defined at runtime without a formal class declaration.
Therefore, when you have used new { YY = DoSomething }
, you are creating an anonymous type instance with a property named YY
, and you're trying to assign the DoSomething
method pointer to it ,BUT it have nothing to do with your class XX and it's properties it is new seperated class.
To create an instance of your class XX
and assign the DoSomething method to its YY
property.
You should use new XX { }
to explicitly create an object of type XX
because otherwise, without new XX
, you're creating something that doesn't have the YY
. This approach is called a target-typed new expression.
new XX { }
this is creating type on the fly (WHAT YOU WANT)
new {}
this creating annomoys object on the fly also :D (WHAT WAS WRONG)
Upvotes: 0
Reputation: 273540
You confused target-typed new
expressions with anonymous types!
new { ... }
creates an anonymous type, so YY
there is a property of the anonymous type, so the compiler doesn't know what delegate type it should convert the method group into.
You should use new() { ... }
instead, which creates an object of the target type XX
, and also uses an object initialiser to initialise it.
While object initialisers normally allow you to omit ()
when there are no constructor parameters, target-typed new
expressions always require ()
, regardless of whether you are using an object initialiser. Otherwise, the syntax would conflict with anonymous types!
Upvotes: 6
Reputation: 3495
you should create an instance of the XX
class and then assign the method to the YY property:
private static readonly XX xx = new XX
{
// error "CS0828: Cannot assign 'method group' to anonymous type property"
YY = DoSomething
};
Upvotes: 0