C# Error CS1912 – Duplicate initialization of member ‘{0}’

C# Error

CS1912 – Duplicate initialization of member ‘{0}’

Reason for the Error & Solution

Duplicate initialization of member ‘name’.

An object initializer can initialize each member only one time.

To correct this error

  1. Remove the second initialization of the member in the object initializer.

Example

The following code generates CS1912 because memberA is initialized two times:

// cs1912.cs  
using System.Linq;  
  
public class TestClass  
{  
    public int memberA { get; set; }  
    public int memberB { get; set; }  
}  
  
public class Test  
{  
    static void Main()  
    {  
        TestClass tc = new TestClass() { memberA = 5, memberA = 6, memberB = 2}; // CS1912  
    }  
}  

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...