Why in C# 3 I can do this:
DataTable dt = new DataTable() {
Columns = { "1", "2", "3" } };
But I can’t do this:
class Person {
int Id { get; set; }
}
class Program {
static void Main(string[] args)
{
var v = new List<Person> { 1, 2, 3 };
}
}
Because there is not implicit conversion from int to Person. If you were to define an implicit conversion for Person, that should work:
http://msdn.microsoft.com/en-us/library/z5z9kes2(v=VS.100).aspx
Note in the example that a double value is implicitly convertable to a Digit type. You could define an implicit conversion for int to Person.