Click here to Skip to main content
15,911,485 members
Home / Discussions / C#
   

C#

 
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 
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 
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 5:59
professionalBillWoodruff18-Jul-19 5:59 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
honey the codewitch20-Jul-19 10:47
mvahoney the codewitch20-Jul-19 10:47 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
BillWoodruff20-Jul-19 13:34
professionalBillWoodruff20-Jul-19 13:34 
GeneralRe: 'Switch on 'Type: C# 7,8 offer no relief; and, Eric Lippert is still right Pin
honey the codewitch20-Jul-19 13:38
mvahoney the codewitch20-Jul-19 13:38 
QuestionProblem deserialising JSON data [SOLVED by the brilliant Richard Deeming] Pin
Richard MacCutchan18-Jul-19 2:03
mveRichard MacCutchan18-Jul-19 2:03 
AnswerRe: Problem deserialising JSON data Pin
OriginalGriff18-Jul-19 2:35
mveOriginalGriff18-Jul-19 2:35 
GeneralRe: Problem deserialising JSON data Pin
Richard MacCutchan18-Jul-19 3:06
mveRichard MacCutchan18-Jul-19 3:06 

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.