0
private void CheckForNewItems()
    {
        var items = GetChangedItems();
        if (items != null)
        {
            foreach (var item in items )
            {
                var itemDB= GetItem(item.id);
                if (itemDB!=null)
                {
                    itemDB.somevalue= item.somevalue;
                    SaveToDatabase(itemDB);

                }
            }
        }
    }

I Write alot of code similar to the code above. Is there a smarter way to check for nulls in this scenario? Is "if(item!=null)" effecient? Do i even have to check for nulls?

Regards

2
  • Elvis operator? blogs.msdn.microsoft.com/jerrynixon/2014/02/26/… Commented Apr 19, 2017 at 11:41
  • 3
    If your GetChangedItems() method returns an empty array (and not null), you don't have to check if items is null before the foreach loop. Commented Apr 19, 2017 at 11:41

4 Answers 4

5

You can do it with some linq:

var items = GetChangedItems();

if (items == null)
    return;

var existingItems = items
    // create a new call that holds both objects
    .Select(i => new { ItemDB = GetItem(i.id), Item = i })
    // where the itemdb can be found.
    .Where(i => i.ItemDB != null);

foreach (var item in existingItems)
{
    item.ItemDB.somevalue= item.Item.somevalue;
    SaveToDatabase(item.ItemDB);
}

But.... I think the solution you already had, is more readable for everyone.

Sign up to request clarification or add additional context in comments.

Comments

4

Create an extension method NullOrEmpty that checks if the collection is null and returns empty:

public static IEnumerable<T> NullOrEmpty<T>(this IEnumerable<T> source)
{
    return source ?? Enumerable.Empty<T>();
}

Then use it:

foreach (var item in items.NullOrEmpty())
{
   ...
}

Comments

0

This is a reasonable solution, there isn't much to be changed. I would only change the first if around to prevent nesting:

private void CheckForNewItems()
{
    var items = GetChangedItems();
    if (items == null)
    {
        return;
    }
    foreach (var item in items )
    {
        var itemDB= GetItem(item.id);
        if (itemDB!=null)
        {
            itemDB.somevalue= item.somevalue;
            SaveToDatabase(itemDB);
        }
    }
}

Comments

0

You could use Null propagation. But then your SaveToDatabase Method would have to check for Null itself. It could also use Null propagation

private void CheckForNewItems()
    {
        var items = GetChangedItems();
        if (items != null)
        {
            foreach (var item in items )
            {
                var itemDB= GetItem(item.id);
                itemDB?.somevalue= item.somevalue;
                SaveToDatabase(itemDB);                   
            }
        }
    }

Have a look at : https://roslyn.codeplex.com/discussions/540883

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.