0
public abstract class Flattenable<T_Id, T_Order> where T_Id : struct, IComparable
                                                 where T_Order : IComparable
{
    public abstract T_Id ID { get; }
    public abstract Nullable<T_Id> Parent_ID { get; }
    public abstract T_Order Order { get; }
}

partial class MyObjectClass : Flattenable<int, int>
{
    public override int ID => this.My_Id;
    public override int? Parent_ID => this.My_ParentId;
    public override int Order => this.My_Ordinal;
}

public class FlatTree<T, T_Id, T_Order> where T : Flattenable<T_Id, T_Order>
                                           where T_Id : struct, IComparable
                                           where T_Order : IComparable
{

    public FlatTree(IEnumerable<T> list)
    {}
    //Code using Flattenable .ID, Parent_ID , Order
}

In this code, I have an abstract class Flattenable to extent an existing class with the necessery properties for my method to work.
My issue is that initialising my object I want the generik type to be some how "herited". So One don't have to check MyObjectClass definition for the Flattenable generic type.

What I want var temp = new FlatTree<MyObjectClass>(myList);. Usage :

List<MyObjectClass> myList = GetItems();
var temp = new FlatTree<MyObjectClass,int,int>(myList); // => work

// What I want
var temp = new FlatTree<MyObjectClass>(myList); // => Sub type get sniffed from MyObjectClass.
var temp = new FlatTree(myList); // Sub type get sniffed from Enumerable T
2
  • 3
    Type inference doesn't work for constructors. If you want to shorten this, create a factory method like public static FlatTree<...> Create<...>(T data) => return new FlatTree<...>(data); Commented Dec 6, 2021 at 15:13
  • 1
    @CamiloTerevinto, I can't manage to make it work dotnetfiddle.net/QWG97w. Its always asking for the full argument for type inference. Commented Dec 6, 2021 at 15:36

1 Answer 1

1
// What I want
var temp = new FlatTree(myList); // Sub type get sniffed from Enumerable T

Type inference does not work on constructors, because the constructor name is effectively the type of the object you're trying to instantiate.

The above syntax would be unable to resolve ambiguity if you codebase had existing class definitions for both FlatTree and any generic FlatTree<>; which supports the notion that constructors must explicitly specify all the generic types in order to identify the correct class.

// What I want
var temp = new FlatTree<MyObjectClass>(myList); // => Sub type get sniffed from MyObjectClass.

Even if you weren't dealing with constructors, partial type inference is currently not possible in C#. Type inference cannot complete a partial generic typing for you.

You have to specify the entire set of generic types to be used; or in cases where type inference works it means that you don't have to specify any generic type. There is no case where type inference only resolves some but not all of the generic types.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. First "type inference" sure give better search result than "type sniffing". From your answer and 20min of doc reading It seams that there is no solution, right? Even If I were to use a Method as factory
@DragandDrop Correct. The syntax you want is not valid in the language right now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.