Adds the given value to the enumeration. - CSharp System

CSharp examples for System:Enum

Description

Adds the given value to the enumeration.

Demo Code

/**/*from w w  w  .  j  av a2  s . co m*/
 * Peter
 * Created by: Peter Development Team
 *    http://peter.codeplex.com/
 * 
 * GNU General Public License version 2 (GPLv2)
 *    http://peter.codeplex.com/license
 *
 *  This code is provided on an AS IS basis, with no WARRANTIES,
 *  CONDITIONS or GUARANTEES of any kind.
 *
 * Adopted from: http://stackoverflow.com/a/417217
 **/
using System;

public class Main{
        /// <summary>
      /// Adds the given value to the enumeration.
      /// </summary>
      /// <typeparam name="T">Enumeration type.</typeparam>
      /// <param name="enumeration">The Enumeration object.</param>
      /// <param name="value">Value to check.</param>
      /// <returns>New enumeration object with value added.</returns>
      public static T Add<T> (this Enum enumeration, T value)
      {
         try
         {
            return (T)(object)(((int)(object)enumeration | (int)(object)value));
         }
         catch (Exception ex)
         {
            throw new ArgumentException (
                string.Format ("Could not append value from enumerated type '{0}'.", typeof (T).Name), ex);
         }
      }
}

Related Tutorials