C# Error CS0747 – Invalid initializer member declarator

C# Error

CS0747 – Invalid initializer member declarator

Reason for the Error & Solution

Invalid initializer member declarator.

An object initializer is used to assign values to properties or fields. Any expression which is not an assignment to a property or field is a compile-time error.

To correct this error

  1. Ensure that all expressions in the initializer are assignments to properties or fields of the type. In the following example, the second expression is an error because the value 1 is not assigned to any property or field of List<int>.

Example

The following code generates CS0747:

// cs0747.cs  
using System.Collections.Generic;  
  
public class C  
{  
    public static int Main()  
    {  
        var t = new List<int> { Capacity = 2, 1 }; // CS0747  
        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...