Add to Collection if Not Null

I have a very large object with many nullable-type variables. I also have a dictionary which I want to fill up with this object's non-null variables.

The code will look something like this

if (myObject.whatever != null)
{
myDictionary.Add("...",myObject.whatever);
}
if (myObject.somethingElse != null)
{
myDictionary.Add("...",myObject.somethingElse);

...

EDIT (Sorry messed up the code)

When we repeat this for the umpteenth time we get a mess of very long code. Is there some shorter way I could write this mess? I know about the Conditional Operator (aka ?) but that's just for assignments. Is there something like that for adding to a collection?

Answers


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);

I'd recommend writing an extension method:

public static class MyExtensions
{
    public static void AddIfNotNull<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
    {
        if ((object)value != null)
            dictionary.Add(key, value);
    }
}

Using (object)value != null ensures that this works as you'd expect with nullable types, (e.g. int?) value types, (e.g. int) and reference types (e.g. SomeClass). If you compare it to default(TValue), then an int of 0 will not be added, even though it's not null. If you include a TValue : class requirement, you can't use Nullable<T> as the type, which it sounds like is your most common usage.


You can make a method that hides your if:

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

private static void AddIfNotNull<K,T>(
    IDictionary<K,T> myDictionary
,   K key
,   T value) {
    if (value != default(T)) {
        myDictionary.Add(key, value);
    }
}

You can earn some "points for style" by making the method an extension (you need to add it to a static class then):

private static void AddIfNotNull<K,T>(
    this IDictionary<K,T> myDictionary
,   K key
,   T value) {
    if (value != default(T)) {
        myDictionary.Add(key, value);
    }
}

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

If you know that you are inserting only reference type objects, replace default(T) with null and add a class T constraint to the generic.


If it's not a performance problem: Why not add everything and then remove what you don't need?


public void addToDict(string ?myObj, Dictionary<,> myDict) {
        if (myObj != null)
              myDict.Add("...", myObj);
}

addToDict(myObject.whatever, myDict);
addToDict(myObject.somethignElse, myDict);

etc


Need Your Help

Inserting values into a SQL Server database using ado.net via C#

c# sql sql-server ado.net executenonquery

I have created a simple program to insert values into the table [regist], but I keep getting the error

placeholder for input type date html5

html5 date placeholder

placeholder does not work for input type date directly.