Click here to Skip to main content
15,902,445 members
Home / Discussions / C#
   

C#

 
GeneralRe: how to achieve cell_doubleclick event to be fired on gridview Pin
Richard MacCutchan22-May-17 22:58
mveRichard MacCutchan22-May-17 22:58 
GeneralRe: how to achieve cell_doubleclick event to be fired on gridview Pin
Mayank Kumar23-May-17 0:02
Mayank Kumar23-May-17 0:02 
GeneralRe: how to achieve cell_doubleclick event to be fired on gridview Pin
Richard MacCutchan23-May-17 0:08
mveRichard MacCutchan23-May-17 0:08 
GeneralRe: how to achieve cell_doubleclick event to be fired on gridview Pin
Mayank Kumar23-May-17 0:12
Mayank Kumar23-May-17 0:12 
GeneralRe: how to achieve cell_doubleclick event to be fired on gridview Pin
Pete O'Hanlon23-May-17 0:12
mvePete O'Hanlon23-May-17 0:12 
QuestionMerging 2 different JSON objects Pin
Farhad Eft21-May-17 21:41
Farhad Eft21-May-17 21:41 
AnswerRe: Merging 2 different JSON objects Pin
Richard MacCutchan21-May-17 22:49
mveRichard MacCutchan21-May-17 22:49 
GeneralRe: Merging 2 different JSON objects Pin
Farhad Eft24-May-17 0:41
Farhad Eft24-May-17 0:41 
Questionbest way to Lazy-Load a Dictionary' s 'Values ? Pin
BillWoodruff18-May-17 6:51
professionalBillWoodruff18-May-17 6:51 
AnswerRe: best way to Lazy-Load a Dictionary' s 'Values ? Pin
Afzaal Ahmad Zeeshan18-May-17 8:34
professionalAfzaal Ahmad Zeeshan18-May-17 8:34 
GeneralRe: best way to Lazy-Load a Dictionary' s 'Values ? Pin
BillWoodruff18-May-17 14:47
professionalBillWoodruff18-May-17 14:47 
GeneralRe: best way to Lazy-Load a Dictionary' s 'Values ? Pin
Afzaal Ahmad Zeeshan18-May-17 23:03
professionalAfzaal Ahmad Zeeshan18-May-17 23:03 
AnswerRe: best way to Lazy-Load a Dictionary' s 'Values ? Pin
Richard Deeming18-May-17 11:48
mveRichard Deeming18-May-17 11:48 
Not sure if it's better, but something like this might work:
C#
public class LazyDictionary<TKey, TValue> : IReadOnlyDictionary<TKey, TValue>
{
    private readonly ConcurrentDictionary<TKey, TValue> _values;
    private readonly Func<TKey, TValue> _valueFactory;
    private readonly Func<TKey, bool> _keyValidator;
    
    public LazyDictionary(Func<TKey, TValue> valueFactory, Func<TKey, bool> keyValidator = null, IEqualityComparer<TKey> keyComparer = null)
    {
        if (valueFactory == null) throw new ArgumentNullException(nameof(valueFactory));
        if (keyComparer == null) keyComparer = EqualityComparer<TKey>.Default;
        _values = new ConcurrentDictionary<TKey, TValue>(keyComparer);
        _valueFactory = valueFactory;
        _keyValidator = keyValidator;
    }
    
    private bool KeyIsValid(TKey key) => _keyValidator == null || _keyValidator(key);
    
    private TValue GetOrAdd(TKey key) => _values.GetOrAdd(key, _valueFactory);
    
    public int Count => _values.Count;
    
    public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => _values.GetEnumerator();
    
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
    
    public IEnumerable<TKey> Keys => _values.Keys;
    
    public IEnumerable<TValue> Values => _values.Values;
    
    public bool ContainsKey(TKey key) => KeyIsValid(key);
    
    public bool TryGetValue(TKey key, out TValue value)
    {
        if (!ContainsKey(key))
        {
            value = default(TValue);
            return false;
        }
        
        value = GetOrAdd(key);
        return true;
    }
    
    public TValue this[TKey key]
    {
        get
        {
            if (!ContainsKey(key)) throw new KeyNotFoundException();
            return GetOrAdd(key);
        }
    }
}

Usage:
C#
public IReadOnlyDictionary<Division, IList<Person>> DivisionToStaff { get; }
   = new LazyDictionary<Division, IList<Person>>(d => new List<Person>(), d => Enum.IsDefined(typeof(Division), d));

...

DivisionToStaff[personInstance.Division].Add(personInstance);

ContainsKey will return true for any valid key.

Count, GetEnumerator, and the Keys and Values properties will only reflect the keys which have already been created.

TryGetValue and the indexer will throw an exception for any invalid key; return the existing value for a valid key if it's already been created; and create the value for a valid key if it hasn't.

It has the added advantage of being (mostly) thread-safe.



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


GeneralRe: best way to Lazy-Load a Dictionary' s 'Values ? Pin
BillWoodruff18-May-17 14:52
professionalBillWoodruff18-May-17 14:52 
GeneralRe: best way to Lazy-Load a Dictionary' s 'Values ? Pin
Richard Deeming23-May-17 1:02
mveRichard Deeming23-May-17 1:02 
Questionhow to implement dynamic range compression for audio? Pin
Member 1319657418-May-17 3:45
Member 1319657418-May-17 3:45 
AnswerRe: how to implement dynamic range compression for audio? Pin
Richard MacCutchan18-May-17 3:47
mveRichard MacCutchan18-May-17 3:47 
GeneralRe: how to implement dynamic range compression for audio? Pin
Member 1319657418-May-17 3:56
Member 1319657418-May-17 3:56 
GeneralRe: how to implement dynamic range compression for audio? Pin
Richard MacCutchan18-May-17 3:58
mveRichard MacCutchan18-May-17 3:58 
GeneralRe: how to implement dynamic range compression for audio? Pin
kalberts23-May-17 22:09
kalberts23-May-17 22:09 
AnswerRe: how to implement dynamic range compression for audio? Pin
Gerry Schmitz18-May-17 5:51
mveGerry Schmitz18-May-17 5:51 
GeneralRe: how to implement dynamic range compression for audio? Pin
Member 1319657418-May-17 10:34
Member 1319657418-May-17 10:34 
QuestionSound Recognition system Pin
Member 1182866018-May-17 1:37
Member 1182866018-May-17 1:37 
AnswerRe: Sound Recognition system Pin
Pete O'Hanlon18-May-17 1:50
mvePete O'Hanlon18-May-17 1:50 
GeneralRe: Sound Recognition system Pin
ZurdoDev18-May-17 3:27
professionalZurdoDev18-May-17 3:27 

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.