Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Collection initializer requires its type 'type' to implement System.Collections.IEnumerable.
In order to use a collection initializer with a type, the type must implement IEnumerable
. This error can occur if you accidentally use collection initializer syntax when you meant to use an object initializer.
If the type does not represent a collection, use object initializer syntax instead of collection initializer syntax.
If the type does represent a collection, modify it to implement
IEnumerable
before you can use collection initializers to initialize objects of that type.If the type represents a collection and you do not have access to the source code, just initialize its elements by using their class constructors or other initialization methods.
The following code produces CS1922:
// cs1922.cs
public class Test
{
public static void Main()
{
// Collection initializer.
var tc = new TestClass {1,"hello"} ; // CS1922
// Object initializer.
var tc2 = new TestClass { memberA = 1, memberB = "hello" }; // OK
}
}
public class TestClass
{
public int memberA { get; set; }
public string memberB { get; set; }
}
.NET feedback
.NET is an open source project. Select a link to provide feedback: