C# Error CS0746 – Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

C# Error

CS0746 – Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

Reason for the Error & Solution

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

An anonymous type must be declared with a member assignment, simple name, or member access.

To correct this error

  1. Ensure that your declaration uses only member assignment, simple names, or member access expressions.

Example

The following code generates CS0746 in the declaration of incorrect_1 and incorrect_2. The following declarations show two of the correct ways to declare an anonymous type.

// cs0746.cs  
public class C  
{  
    public static int Main()  
    {  
        int i = 100;  
        string s = "Bottles of beer.";  
  
        var incorrect_1 = new { a.b = 1 }; // CS0746
        var incorrect_2 = new {100, "Bottles of beer."}; // CS0746  
        var correct_1 = new { i, s }; //OK  
        var correct_2 = new {num = i, message = s}; // OK  
  
        return 1;  
    }  
}  

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

C# Compiler Error CS0442 – ‘Property’: abstract properties cannot have private accessors Reason for the Error You’ll get this error...
This is a really simple one . Below is a simple example of an enum called “Designation” defined with the...
This blog post explain the usage of the Checked Block in .NET and how you can use them in Visual...