Skip to main content

How to Resolve Error "CS0747: Invalid initializer member declarator" in C#

The Compiler Error CS0747 is a syntax error that occurs during Object Initializer or Collection Initializer usage. The message reads: "Invalid initializer member declarator".

In C#, object initializers (e.g., new Person { Name = "Alice" }) generally require assignments to properties or fields. Collection initializers (e.g., new List<int> { 1, 2, 3 }) require simple values added to the list. This error occurs when you mix up the syntax—for example, trying to assign a value to a property but omitting the assignment operator =, or trying to invoke a method inside an object initializer block where only property assignments are expected.

This guide explains the correct syntax for object and collection initializers and how to fix malformed declarations.

Understanding Initializer Syntax

C# distinguishes between initializing an Object (setting properties) and initializing a Collection (adding items).

  • Object Initializer: { Prop1 = Value1, Prop2 = Value2 }
    • Rule: Must be an assignment (Identifier = Value).
  • Collection Initializer: { Value1, Value2, Value3 }
    • Rule: Must be an expression (a value) that fits the collection's Add() method.

CS0747 typically means you are inside an Object Initializer block, but you provided something that isn't a property assignment.

Scenario 1: Missing Assignment Operator in Object Initializer

This is a common typo. You intended to set a property, but you forgot the equals sign =, making the statement look like a variable declaration or a loose expression.

Example of error

public class User
{
public string Name { get; set; }
public int Age { get; set; }
}

public class Program
{
static void Main()
{
// ⛔️ Error CS0747: Invalid initializer member declarator.
// The compiler sees 'Name "Alice"', which is not valid syntax.
// It expects 'Name = "Alice"'.
var u = new User { Name "Alice", Age = 30 };
}
}

Solution: Add the Assignment Operator

Ensure every member in the curly braces follows the Property = Value format.

public class Program
{
static void Main()
{
// ✅ Correct: Using '=' to assign the value.
var u = new User { Name = "Alice", Age = 30 };
}
}

Scenario 2: Mixing Collection and Object Syntax

This usually happens with classes that are both objects (have properties) and collections (implement IEnumerable and Add). C# allows initializing either the properties or the collection items, but mixing them in the same list requires specific syntax (using an indexer or separate initialization).

However, a more common error is simply using commas incorrectly in an object initializer, making it look like a collection list.

Example of error

Providing just a value inside an object initializer without saying which property it belongs to.

public class Point
{
public int X { get; set; }
public int Y { get; set; }
}

public class Program
{
static void Main()
{
// ⛔️ Error CS0747: '10' is just a value.
// 'Point' is not a collection, so you cannot just list numbers.
// You must assign them to X and Y.
var p = new Point { X = 5, 10 };
}
}

Solution: Explicit Property Names

Assign the orphan value to its corresponding property.

public class Program
{
static void Main()
{
// ✅ Correct: Both values are assigned to properties.
var p = new Point { X = 5, Y = 10 };
}
}

Scenario 3: Attempting Method Calls in Initializers

Object initializers are strictly for setting fields or properties. You cannot call arbitrary methods (like Console.WriteLine or a class method) inside the initializer block.

Example of error

public class Logger
{
public string Prefix { get; set; }
public void Init() { }
}

public class Program
{
static void Main()
{
// ⛔️ Error CS0747: Invalid initializer member declarator.
// You cannot call 'Init()' here. Only assignments are allowed.
var log = new Logger
{
Prefix = "Info",
Init()
};
}
}

Solution: Call Methods After Initialization

Finish the initialization first, then call the method on the variable.

public class Program
{
static void Main()
{
// 1. Initialize properties
var log = new Logger { Prefix = "Info" };

// 2. Call methods
log.Init();
}
}

Conclusion

CS0747 indicates a syntax error inside the { ... } block of a new expression.

  1. Check Assignments: Ensure you are using Property = Value.
  2. Check Syntax: Did you forget the = sign?
  3. Check Content: Are you trying to add a raw value or call a method? Remove those from the initializer block.