Removes the given value from the enumeration. - CSharp System

CSharp examples for System:Enum

Description

Removes the given value from the enumeration.

Demo Code

/**/*from w  w w. j  a  v a2  s. c o 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>
      /// Removes the given value from 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 removed.</returns>
      public static T Remove<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 remove value from enumerated type '{0}'.", typeof (T).Name), ex);
         }
      }
}

Related Tutorials