Click here to Skip to main content
15,918,471 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.

 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
Mike Hankey25-Nov-17 2:15
mveMike Hankey25-Nov-17 2:15 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
BillWoodruff25-Nov-17 4:57
professionalBillWoodruff25-Nov-17 4:57 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
OriginalGriff25-Nov-17 5:23
mveOriginalGriff25-Nov-17 5:23 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
Dave Sexton26-Nov-17 21:11
Dave Sexton26-Nov-17 21:11 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
Gary Wheeler27-Nov-17 1:25
Gary Wheeler27-Nov-17 1:25 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
Gerry Schmitz27-Nov-17 3:14
mveGerry Schmitz27-Nov-17 3:14 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
jschell27-Nov-17 9:14
jschell27-Nov-17 9:14 
GeneralRe: I have a horrible feeling I've been using a pattern for years... Pin
Richard Deeming28-Nov-17 3:49
mveRichard Deeming28-Nov-17 3:49 
A cunning way of asking a programming question in the Lounge! Poke tongue | ;-P

The all field should be readonly, since you never replace it.

You should probably specify an explicit StringComparer for the dictionary, to make it more obvious that the key is case-sensitive.

I'd be inclined to move the Add to the static method, and leave the constructor alone. I'd also replace the ContainsKey / indexer pair with a single TryGetValue call:
C#
public static MyClass Get(string name, List<string> data)
{
    if (!all.TryGetValue(name, out MyClass instance)
    {
        instance = new MyClass(name, data);
        all.Add(name, instance);
    }
    
    return instance;
}

And as Gerry said, if there's any possibility of the method being called by multiple threads, use a ConcurrentDictionary[^] instead:
C#
private static readonly ConcurrentDictionary<string, MyClass> all = new ConcurrentDictionary<string, MyClass>(StringComparer.Ordinal);

public static MyClass Get(string name, List<string> data)
{
    return all.GetOrAdd(name, key => new MyClass(key, data));
}

Let's hope your class doesn't contain any unmanaged or disposable resources, since you'd have no way of knowing when to clean them up. Big Grin | :-D



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


QuestionProgramming Question of the Week? Pin
megaadam24-Nov-17 0:34
professionalmegaadam24-Nov-17 0:34 
AnswerRe: Programming Question of the Week? Pin
theoldfool24-Nov-17 1:02
professionaltheoldfool24-Nov-17 1:02 
AnswerRe: Programming Question of the Week? Pin
CPallini24-Nov-17 1:42
mveCPallini24-Nov-17 1:42 
AnswerRe: Programming Question of the Week? Pin
lopatir24-Nov-17 1:46
lopatir24-Nov-17 1:46 
JokeRe: Programming Question of the Week? Pin
V.24-Nov-17 1:49
professionalV.24-Nov-17 1:49 
AnswerRe: Programming Question of the Week? Pin
super24-Nov-17 2:30
professionalsuper24-Nov-17 2:30 
AnswerRe: Programming Question of the Week? Pin
A_Griffin24-Nov-17 2:32
A_Griffin24-Nov-17 2:32 
GeneralRe: Programming Question of the Week? Pin
Jim_Snyder24-Nov-17 3:28
professionalJim_Snyder24-Nov-17 3:28 
GeneralRe: Programming Question of the Week? Pin
PIEBALDconsult24-Nov-17 4:53
mvePIEBALDconsult24-Nov-17 4:53 
AnswerRe: Programming Question of the Week? Pin
Gary Wheeler27-Nov-17 1:26
Gary Wheeler27-Nov-17 1:26 
QuestionWill you play a game with me? Pin
Nagy Vilmos23-Nov-17 22:54
professionalNagy Vilmos23-Nov-17 22:54 
AnswerRe: Will you play a game with me? Pin
CodeWraith23-Nov-17 23:15
CodeWraith23-Nov-17 23:15 
GeneralRe: Will you play a game with me? Pin
Nagy Vilmos23-Nov-17 23:34
professionalNagy Vilmos23-Nov-17 23:34 
GeneralRe: Will you play a game with me? Pin
OriginalGriff23-Nov-17 23:48
mveOriginalGriff23-Nov-17 23:48 
GeneralRe: Will you play a game with me? Pin
Nagy Vilmos23-Nov-17 23:51
professionalNagy Vilmos23-Nov-17 23:51 
GeneralRe: Will you play a game with me? Pin
CodeWraith24-Nov-17 0:00
CodeWraith24-Nov-17 0:00 
GeneralRe: Will you play a game with me? Pin
CodeWraith24-Nov-17 0:04
CodeWraith24-Nov-17 0:04 

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.