Click here to Skip to main content
15,904,877 members
Home / Discussions / C#
   

C#

 
GeneralRe: This is a style question regarding partial classes Pin
BillWoodruff20-Jul-19 17:53
professionalBillWoodruff20-Jul-19 17:53 
GeneralRe: This is a style question regarding partial classes Pin
honey the codewitch20-Jul-19 18:10
mvahoney the codewitch20-Jul-19 18:10 
GeneralRe: This is a style question regarding partial classes Pin
#realJSOP21-Jul-19 3:35
professional#realJSOP21-Jul-19 3:35 
AnswerRe: This is a style question regarding partial classes Pin
Bernhard Hiller21-Jul-19 20:54
Bernhard Hiller21-Jul-19 20:54 
GeneralRe: This is a style question regarding partial classes Pin
honey the codewitch21-Jul-19 21:05
mvahoney the codewitch21-Jul-19 21:05 
GeneralRe: This is a style question regarding partial classes Pin
Bernhard Hiller21-Jul-19 21:35
Bernhard Hiller21-Jul-19 21:35 
GeneralRe: This is a style question regarding partial classes Pin
honey the codewitch21-Jul-19 21:40
mvahoney the codewitch21-Jul-19 21:40 
Questionhow to read data type in excel Pin
rs1919-Jul-19 6:54
rs1919-Jul-19 6:54 
AnswerRe: C# Pin
Dave Kreskowiak19-Jul-19 8:14
mveDave Kreskowiak19-Jul-19 8:14 
GeneralRe: C# Pin
rs1919-Jul-19 9:38
rs1919-Jul-19 9:38 
GeneralRe: C# Pin
Dave Kreskowiak19-Jul-19 9:49
mveDave Kreskowiak19-Jul-19 9:49 
GeneralRe: C# Pin
BillWoodruff20-Jul-19 6:41
professionalBillWoodruff20-Jul-19 6:41 
GeneralRe: C# Pin
Dave Kreskowiak20-Jul-19 13:30
mveDave Kreskowiak20-Jul-19 13:30 
GeneralRe: C# Pin
BillWoodruff20-Jul-19 17:59
professionalBillWoodruff20-Jul-19 17:59 
AnswerRe: C# Pin
ZurdoDev19-Jul-19 10:12
professionalZurdoDev19-Jul-19 10:12 
AnswerRe: how to read data type in excel Pin
Richard MacCutchan19-Jul-19 21:39
mveRichard MacCutchan19-Jul-19 21:39 
AnswerRe: how to read data type in excel Pin
BillWoodruff20-Jul-19 6:44
professionalBillWoodruff20-Jul-19 6:44 
QuestionWCF UnobservedTaskException Pin
Bernhard Hiller19-Jul-19 2:57
Bernhard Hiller19-Jul-19 2:57 
AnswerRe: WCF UnobservedTaskException Pin
Richard Deeming19-Jul-19 3:40
mveRichard Deeming19-Jul-19 3:40 
GeneralRe: WCF UnobservedTaskException Pin
Bernhard Hiller19-Jul-19 4:24
Bernhard Hiller19-Jul-19 4:24 
GeneralRe: WCF UnobservedTaskException Pin
Richard Deeming19-Jul-19 4:41
mveRichard Deeming19-Jul-19 4:41 
GeneralRe: WCF UnobservedTaskException Pin
Bernhard Hiller21-Jul-19 21:00
Bernhard Hiller21-Jul-19 21:00 
Question'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff18-Jul-19 4:15
professionalBillWoodruff18-Jul-19 4:15 
A long time ago, Eric wrote:
Quote:
Any time you find yourself switching on a type in a generic you are almost certainly doing something wrong. Generics should be generic; they should operate identically completely independent of the type.

If T can only be int or string then don't write your code this way at all in the first place. Write two methods, one that returns an int and one that returns a string.
Of course, Eric is always right !

I thought the new pattern-matching facilities in C# 7,8 might allow me to simplify a 'Switch based on a Generic Type; here's a compilable (VS 2019, FrameWork 4.8) example:
C#
// 'KeyType has been set in the class initializer

public TKey GetNextKey(TKey key = default(TKey))
{
    if (! KeysUsed.Contains(key))
    {
        CurrentKey = key;
        KeysUsed.Add(key);
        return key;
    }
    
    switch (KeyType)
    {
        case Type _ when KeyType == typeof(Int32):
            int i = (Int32) (object) key + 1;
            CurrentKey = (TKey)(object) i;
            break;
        case Type _ when KeyType == typeof(string):  // incomplete
            string s = (string) (object) key;
            s += GetAGuid(); // code not shown here
            CurrentKey = (TKey)(object)s;
            break;

        default:
            throw new ArgumentException("unsupported TKey Type");
    }

    KeysUsed.Add(CurrentKey);
    return CurrentKey;
}
Okay, if you think "case Type _ when KeyType == typeof(Int32):" is simplification ... game over.

What really irritates me is the conversions to object necessary here. This issue came up in a QA question here in 2016; see FES_SiteCore's solution: [^]

references: Mads Torgensen on C# 8 patterns: [^], ReSharper recent blog: [^]
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot


modified 18-Jul-19 10:32am.

SuggestionRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
Richard Deeming18-Jul-19 5:02
mveRichard Deeming18-Jul-19 5:02 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff18-Jul-19 5:59
professionalBillWoodruff18-Jul-19 5:59 

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.