Add to Collection if Not Null

How about an extension method for your dictionary?

public static void AddIfNotNull<T,U>(this Dictionary<T,U> dic, T key, U value) 
where U : class {
    if (value != null) { dic.Add(key, value); }
}

You could then do this:

myDictionary.AddIfNotNull("...",myObject.whatever);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)