Adds item to the dictionary if item is not null. - CSharp System.Collections

CSharp examples for System.Collections:IDictionary

Description

Adds item to the dictionary if item is not null.

Demo Code

/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections;
using System;//  www  .j av  a 2s.  c o  m

public class Main{
        //--------------------------------------------------------------------------
    /// <summary>
    /// Adds item to the dictionary if item is not null.
    /// </summary>
    /// <param name="dictionary">dictionary to add item to</param>
    /// <param name="key">key to add to dictionary</param>
    /// <param name="value">value to add to dictionary</param>
    static public void AddIfNotNull(IDictionary dictionary, object key, object value)
    {
      if (dictionary != null && key != null && value != null)
      {
        dictionary[key] = value;
      }
    }
        //-------------------------------------------------------------------------
    /// <summary>
    /// Adds item to the list if item is not null.
    /// </summary>
    /// <param name="list">list to add item to</param>
    /// <param name="item">item to add to list</param>
    /// <returns>index of the added item or -1 if item was not added</returns>
    static public int AddIfNotNull(IList list, object item)
    {
      return list != null && item != null ? list.Add(item) : -1;
    }
}

Related Tutorials