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

This C# program calculates and displays an upper triangular matrix based on user input. Problem Statement: The program takes the...
This C# program serves as a demonstration of bitwise operators, which are fundamental operators used for manipulating individual bits in...
This C# program is designed to interchange or swap the columns of a matrix. A matrix is a two-dimensional array...