Issue
This Content is from Stack Overflow. Question asked by Andrey Pelepets
Is it possible in C#10+ or any future version of C# to avoid specifying of two obvious type parameters (T1
and T2
) of TestClass
generic?
All suggestions are acceptable – fixing design of TestClass
, some syntax sugar or waiting of new version of C# :).
Example of TestClass
generic usage shown insideTestMethod
.
public class TestClass<T1, T2, TConverter>
where T1 : BaseClass1, new()
where T2 : BaseClass2, new()
where TConverter : SomeConverter<T1, T2>, new()
{
public TConverter Converter { get; } = new TConverter();
public static void TestMethod()
{
var testClass = new TestClass<Class1, Class2, TestConverter>();
//var testClassWanted = new TestClass<TestConverter>(); // just to show what I want
}
private class TestConverter : SomeConverter<Class1, Class2> { }
private class Class1 : BaseClass1 { }
private class Class2 : BaseClass2 { }
// some implementation...
}
public class BaseClass1 { }
public class BaseClass2 { }
public class SomeConverter<T1, T2>
where T1 : BaseClass1, new()
where T2 : BaseClass2, new()
{
public T1 c1 { get; } = new T1();
public T2 c2 { get; } = new T2();
// some implementation...
}
I have also tried following (it doesn’t work)
public class TestClass<TConverter>
where T1 : BaseClass1, new()
where T2 : BaseClass2, new()
where TConverter : SomeConverter<T1, T2>, new()
{
}
PS, I am sure that type parameters are obvious due to duplicated constraints of T1
and T2
inside TestClass
and SomeConverter
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.