Click here to Skip to main content
15,891,204 members
Home / Discussions / C#
   

C#

 
QuestionMemory Management in Unit Testing Pin
User-862169531-Mar-20 19:20
User-862169531-Mar-20 19:20 
AnswerRe: Memory Management in Unit Testing Pin
Richard MacCutchan31-Mar-20 21:04
mveRichard MacCutchan31-Mar-20 21:04 
AnswerRe: Memory Management in Unit Testing Pin
Pete O'Hanlon1-Apr-20 0:24
mvePete O'Hanlon1-Apr-20 0:24 
AnswerRe: Memory Management in Unit Testing Pin
F-ES Sitecore1-Apr-20 1:38
professionalF-ES Sitecore1-Apr-20 1:38 
QuestionReference Types v. Value Types Pin
Richard Andrew x6431-Mar-20 10:52
professionalRichard Andrew x6431-Mar-20 10:52 
AnswerRe: Reference Types v. Value Types Pin
OriginalGriff31-Mar-20 11:42
mveOriginalGriff31-Mar-20 11:42 
AnswerRe: Reference Types v. Value Types Pin
BillWoodruff31-Mar-20 12:31
professionalBillWoodruff31-Mar-20 12:31 
GeneralRe: Reference Types v. Value Types Pin
Richard Andrew x6431-Mar-20 13:38
professionalRichard Andrew x6431-Mar-20 13:38 
GeneralRe: Reference Types v. Value Types Pin
BillWoodruff31-Mar-20 18:38
professionalBillWoodruff31-Mar-20 18:38 
AnswerRe: Reference Types v. Value Types Pin
F-ES Sitecore1-Apr-20 1:19
professionalF-ES Sitecore1-Apr-20 1:19 
GeneralRe: Reference Types v. Value Types Pin
Richard Deeming1-Apr-20 2:20
mveRichard Deeming1-Apr-20 2:20 
GeneralRe: Reference Types v. Value Types Pin
F-ES Sitecore1-Apr-20 2:38
professionalF-ES Sitecore1-Apr-20 2:38 
QuestionOverloaded Methods with Generics Pin
#realJSOP30-Mar-20 5:52
mve#realJSOP30-Mar-20 5:52 
AnswerRe: Overloaded Methods with Generics Pin
OriginalGriff30-Mar-20 6:34
mveOriginalGriff30-Mar-20 6:34 
GeneralRe: Overloaded Methods with Generics Pin
#realJSOP30-Mar-20 6:50
mve#realJSOP30-Mar-20 6:50 
GeneralRe: Overloaded Methods with Generics Pin
OriginalGriff30-Mar-20 8:28
mveOriginalGriff30-Mar-20 8:28 
GeneralRe: Overloaded Methods with Generics Pin
#realJSOP31-Mar-20 0:21
mve#realJSOP31-Mar-20 0:21 
AnswerRe: Overloaded Methods with Generics Pin
BillWoodruff30-Mar-20 16:33
professionalBillWoodruff30-Mar-20 16:33 
GeneralRe: Overloaded Methods with Generics Pin
#realJSOP31-Mar-20 0:18
mve#realJSOP31-Mar-20 0:18 
GeneralRe: Overloaded Methods with Generics Pin
BillWoodruff31-Mar-20 5:17
professionalBillWoodruff31-Mar-20 5:17 
AnswerRe: Overloaded Methods with Generics Pin
Richard Deeming31-Mar-20 1:26
mveRichard Deeming31-Mar-20 1:26 
GeneralRe: Overloaded Methods with Generics Pin
BillWoodruff31-Mar-20 5:32
professionalBillWoodruff31-Mar-20 5:32 
GeneralRe: Overloaded Methods with Generics Pin
Richard Deeming31-Mar-20 8:51
mveRichard Deeming31-Mar-20 8:51 
If you start with the methods from John's post, you'll see the issue:
C#
public static void Foo<T>(T value) => Console.WriteLine("Foo a single value");
public static void Foo<T>(IEnumerable<T> values) => Console.WriteLine("Foo multiple values");

public static void Bar()
{
    Foo(42); // Calls Foo(T) - Correct
    Foo(Enumerable.Range(1, 42)); // Calls Foo(IEnumerable) - Correct
    
    Foo(new[] { 1, 2, 3 }); // Calls Foo(T) - *** INCORRECT ***
    Foo(new List<int> { 1, 2, 3 }); // Calls Foo(T) - *** INCORRECT ***
    
    Foo((new[] { 1, 2, 3 }).AsEnumerable()); // Calls Foo(IEnumerable) - Correct
    Foo((new List<int> { 1, 2, 3 }).AsEnumerable()); // Calls Foo(IEnumerable) - Correct
}
Generic Overload Resolution | C# Online Compiler | .NET Fiddle[^]

The question was, given an x whose compile-time type implements IEnumerable<>, is there a way to make Foo(x) call Foo(IEnumerable<T>) instead of Foo(T), without modifying the call-site.

The answer seems to be that there isn't. If the compile-time type of x is not exactly equal to IEnumerable<>, then the compiler prefers to call the single-value method, using the compile-time type as the type parameter, rather than casting to IEnumerable<T> and calling the multi-value method.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer

GeneralRe: Overloaded Methods with Generics Pin
BillWoodruff31-Mar-20 11:49
professionalBillWoodruff31-Mar-20 11:49 
GeneralRe: Overloaded Methods with Generics Pin
Richard Andrew x6431-Mar-20 13:57
professionalRichard Andrew x6431-Mar-20 13:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.