Click here to Skip to main content
15,169,508 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to read data type in excel Pin
BillWoodruff20-Jul-19 7:44
mveBillWoodruff20-Jul-19 7:44 
QuestionWCF UnobservedTaskException Pin
Bernhard Hiller19-Jul-19 3:57
MemberBernhard Hiller19-Jul-19 3:57 
AnswerRe: WCF UnobservedTaskException Pin
Richard Deeming19-Jul-19 4:40
mveRichard Deeming19-Jul-19 4:40 
GeneralRe: WCF UnobservedTaskException Pin
Bernhard Hiller19-Jul-19 5:24
MemberBernhard Hiller19-Jul-19 5:24 
GeneralRe: WCF UnobservedTaskException Pin
Richard Deeming19-Jul-19 5:41
mveRichard Deeming19-Jul-19 5:41 
GeneralRe: WCF UnobservedTaskException Pin
Bernhard Hiller21-Jul-19 22:00
MemberBernhard Hiller21-Jul-19 22:00 
Question'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff18-Jul-19 5:15
mveBillWoodruff18-Jul-19 5:15 
SuggestionRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
Richard Deeming18-Jul-19 6:02
mveRichard Deeming18-Jul-19 6:02 
BillWoodruff wrote:
C#
TKey key = default(TKey)
Since you're using a recent compiler version, you can omit the type name from the default keyword here:
C#
public TKey GetNextKey(TKey key = default)


BillWoodruff wrote:
C#
if (! KeysUsed.Contains(key))
{
    CurrentKey = key;
    KeysUsed.Add(key);
    return key;
}
If KeysUsed is a HashSet<TKey>, you can simplify that to:
C#
if (KeysUsed.Add(key))
{
    CurrentKey = key;
    return key;
}



BillWoodruff wrote:
C#
switch (KeyType)
As you've shown, this is a horrible way to solve the problem. I'd be inclined to make the "next key generation" strategy external to the class, and pass it in via the constructor:
C#
public class Foo<TKey>
{
    private readonly HashSet<TKey> KeysUsed;
    private readonly Func<TKey, TKey> GenerateNextKey;
    
    public Foo(Func<TKey, TKey> generateNextKey, IEqualityComparer<TKey> comparer = default)
    {
        if (generateNextKey is null) throw new ArgumentNullException(nameof(generateNextKey));
        
        GenerateNextKey = generateNextKey;
        KeysUsed = new HashSet<TKey>(comparer ?? EqualityComparer<TKey>.Default);
    }
    
    public TKey CurrentKey { get; private set; }
    
    public TKey GetNextKey(TKey key = default)
    {
        while (!KeysUsed.Add(key))
        {
            key = GenerateNextKey(key);
        }
        
        CurrentKey = key;
        return key;
    }
}
You could then provide static key generators and/or factory methods to simplify creating the class:
C#
public static class Foo
{
    public static class Int32
    {
        public static readonly Func<int, int> GenerateNextKey = i => i + 1;
        
        public static Foo<int> Create() => new Foo<int>(GenerateNextKey);
    }
    
    public static class String
    {
        public static readonly Func<string, string> GenerateNextKey = s => s + GetAGuid();
        
        private static string GetAGuid() => Guid.NewGuid().ToString("N");
        
        public static Foo<string> Create(IEqualityComparer<string> comparer = default) => new Foo<string>(GenerateNextKey, comparer);
    }
}
Usage:
C#
Foo<int> fooInt = Foo.Int.Create();
Foo<string> fooString = Foo.String.Create(StringComparer.OrdinalIgnoreCase);
Foo<Guid> fooGuid = new Foo<Guid>(_ => Guid.NewGuid());




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

GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff18-Jul-19 6:59
mveBillWoodruff18-Jul-19 6:59 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
honey the codewitch20-Jul-19 11:47
mvahoney the codewitch20-Jul-19 11:47 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff20-Jul-19 14:34
mveBillWoodruff20-Jul-19 14:34 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
honey the codewitch20-Jul-19 14:38
mvahoney the codewitch20-Jul-19 14:38 
QuestionProblem deserialising JSON data [SOLVED by the brilliant Richard Deeming] Pin
Richard MacCutchan18-Jul-19 3:03
mveRichard MacCutchan18-Jul-19 3:03 
AnswerRe: Problem deserialising JSON data Pin
OriginalGriff18-Jul-19 3:35
mveOriginalGriff18-Jul-19 3:35 
GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 4:06
mveRichard MacCutchan18-Jul-19 4:06 
GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 5:05
mveRichard MacCutchan18-Jul-19 5:05 
AnswerRe: Problem deserialising JSON data Pin
Richard Deeming18-Jul-19 5:42
mveRichard Deeming18-Jul-19 5:42 
GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 7:02
mveRichard MacCutchan18-Jul-19 7:02 
QuestionGetting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Member 1453381917-Jul-19 12:29
MemberMember 1453381917-Jul-19 12:29 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Luc Pattyn17-Jul-19 12:49
sitebuilderLuc Pattyn17-Jul-19 12:49 
GeneralRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Member 1453381917-Jul-19 13:00
MemberMember 1453381917-Jul-19 13:00 
GeneralRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Luc Pattyn17-Jul-19 13:13
sitebuilderLuc Pattyn17-Jul-19 13:13 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Eddy Vluggen17-Jul-19 14:51
professionalEddy Vluggen17-Jul-19 14:51 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
phil.o17-Jul-19 23:13
mvephil.o17-Jul-19 23:13 
AnswerRe: Getting Exception Window Class Name is not Valid in Windows Application using vb.net Pin
Richard Deeming17-Jul-19 23:52
mveRichard Deeming17-Jul-19 23:52 

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.