Constructor Inference and Object, Collection and Dictionary Initializers

Topics: C# Language Design
Jun 20, 2014 at 11:05 PM
I can't find much information about "Constructor Inference" feature. I understand that if class is generic and those generic parameter appears in constructor than compiler try to infer it (like in new Tuple(3, "three", true); example). This is great, but I wonder if type inference from Object, Collection and new Dictionary Initializers will be possible to.

So my question is:
Could I write this code:
using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var list = new List<string>
        { 
            "foo", "bar" 
        };
        
        var dict = new Dictionary<string,int>
        {
            {"key1", 1},
            {"key2", 2}
        };
        
        var dict2 = new Dictionary<string,int>
        {
            ["key1"] =  1,
            ["key1"] =  1
        };
        
        var person = new Person<string>
        {
            Name = "name",
            ExtraInfo = "extraInfo"
        };
        
    }
}

public class Person<T>
{
    public string Name {get; set;}
    public T ExtraInfo {get; set;}  
}
like this:
using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var list = new List
        { 
            "foo", "bar" 
        };
        
        var dict = new Dictionary
        {
            {"key1", 1},
            {"key2", 2}
        };
        
        var dict2 = new Dictionary
        {
            ["key1"] =  1,
            ["key1"] =  1
        };
        
        var person = new Person
        {
            Name = "name",
            ExtraInfo = "extraInfo"
        };
        
    }
}

public class Person<T>
{
    public string Name {get; set;}
    public T ExtraInfo {get; set;}  
}
and get the exact same result?

If you plan this feature than I assume anonymous types would be supported to. So I could write var list = new List(new []{ new { PropString = "string1", PropInt = 1}, new { PropString = "string2", PropInt = 2} }); or var list = new List{ new { PropString = "string1", PropInt = 1}, new { PropString = "string2", PropInt = 2} }; and get list of objects of my anonymous type?