There cannot be two methods with the same signature, but different common limitations

I have this method:

public IEnumerable<MyType> DoSomething<TResult>(Func<MyType, TResult> func) 
    where TResult : AnotherType

Now I want this method to also be displayed for IEnumerable<AnotherType>. So I wrote this, which apparently does not compile:

public IEnumerable<MyType> DoSomething<TResult>(Func<MyType, TResult> func) 
    where TResult : IEnumerable<AnotherType>

I get a compiler error:

Signed Member

I am reading a Member with the same signature that is already defined with different type restrictions , which applies to members with a different return type. However, in my case, I do not distinguish methods return-type, but it param-list, which in the first place Func<MyType, TResult>, and Func<IEnumerable<MyType>, TResult>in the second. However, the compiler cannot handle this.

Is there any other way than a different method name for the second example?

+4
1

, .

, TResult ( Alfie Goodacre), IEnumerable<out T> T, Func<in T1, out TResult> TResult.

, :

public IEnumerable<MyType> DoSomething(Func<MyType, AnotherType> func) 

public IEnumerable<MyType> DoSomething(Func<MyType, IEnumerable<AnotherType>> func) 

- , , AnotherType .


:

public IEnumerable<MyType> DoSomething<TResult>(Func<MyType, TResult> func) 
  where TResult : AnotherType

public IEnumerable<MyType> DoSomething<TResult>(Func<MyType, IEnumerable<TResult>> func) 
  where TResult : AnotherType

, . , AnotherType interface, TResult - struct ( ), , , (out T out TResult).

+5

Source: https://habr.com/ru/post/1664407/


All Articles