Click here to Skip to main content
15,887,596 members
Home / Discussions / C#
   

C#

 
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 
Strange - it works for me:
C#
public void DoSomething()
    {
    MyClass m = new MyClass();
    List<MyClass> list = new List<MyClass>() { m };
    Console.WriteLine($"{ProcessDepending(list)}, {ProcessDepending(m)}");
    }
public int ProcessDepending<T>(T instance)
    {
    if (instance is System.Collections.IEnumerable collection)
        {
        return ProcessCollection(collection);
        }
    return ProcessInstance(instance);
    }

private int ProcessInstance<T>(T instance)
    {
    return 1;
    }

private int ProcessCollection(IEnumerable collection)
    {
    return 2;
    }

private class MyClass { }
I get 2, 1 as I expected.

The only thing I can't figure out is a way to get the collection in a form that I can define this:
C#
private int ProcessCollection<T>(IEnumerable<T> collection)
Because I can't get at the type of the collection objects at compile time ...
I can do this though:
C#
public int ProcessDepending<T>(T instance)
    {
    if (instance is IEnumerable<object> collection)
        {
        return ProcessCollection<object>(collection);
        }
    return ProcessInstance(instance);
    }

private int ProcessCollection<T>(IEnumerable<T> collection)
    {
    return 2;
    }
And it'll work. Messy ...
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
AntiTwitter: @DalekDave is now a follower!

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 
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 
GeneralRe: Overloaded Methods with Generics Pin
Richard Deeming1-Apr-20 0:04
mveRichard Deeming1-Apr-20 0:04 
GeneralRe: Overloaded Methods with Generics Pin
BillWoodruff1-Apr-20 2:07
professionalBillWoodruff1-Apr-20 2:07 
GeneralRe: Overloaded Methods with Generics Pin
Richard Deeming1-Apr-20 2:13
mveRichard Deeming1-Apr-20 2:13 
GeneralRe: Overloaded Methods with Generics Pin
Richard Deeming1-Apr-20 0:15
mveRichard Deeming1-Apr-20 0:15 
GeneralRe: Overloaded Methods with Generics Pin
#realJSOP31-Mar-20 23:26
mve#realJSOP31-Mar-20 23:26 
AnswerRe: Overloaded Methods with Generics Pin
James Curran7-Apr-20 4:48
James Curran7-Apr-20 4:48 
QuestionWpf Excel Pin
RajaMohammed.A26-Mar-20 3:55
RajaMohammed.A26-Mar-20 3:55 
AnswerRe: Wpf Excel Pin
Richard MacCutchan26-Mar-20 4:12
mveRichard MacCutchan26-Mar-20 4:12 

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.