4 out of 5 rated this helpful - Rate this topic

How to: Create a New Method for an Enumeration (C# Programming Guide)

You can use extension methods to add functionality specific to a particular enum type.

In the following example, the Grades enumeration represents the possible letter grades that a student may receive in a class. An extension method named Passing is added to the Grades type so that each instance of that type now "knows" whether it represents a passing grade or not.


using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace EnumExtension
{
    // Define an extension method in a non-nested static class.
    public static class Extensions
    {        
        public static Grades minPassing = Grades.D;
        public static bool Passing(this Grades grade)
        {
            return grade >= minPassing;
        }
    }

    public enum Grades { F = 0, D=1, C=2, B=3, A=4 };
    class Program
    {       
        static void Main(string[] args)
        {
            Grades g1 = Grades.D;
            Grades g2 = Grades.F;
            Console.WriteLine("First {0} a passing grade.", g1.Passing() ? "is" : "is not");
            Console.WriteLine("Second {0} a passing grade.", g2.Passing() ? "is" : "is not");

            Extensions.minPassing = Grades.C;
            Console.WriteLine("\r\nRaising the bar!\r\n");
            Console.WriteLine("First {0} a passing grade.", g1.Passing() ? "is" : "is not");
            Console.WriteLine("Second {0} a passing grade.", g2.Passing() ? "is" : "is not");
        }
    }
  }
}
/* Output:
    First is a passing grade.
    Second is not a passing grade.

    Raising the bar!

    First is not a passing grade.
    Second is not a passing grade.
 */


Note that the Extensions class also contains a static variable that is updated dynamically and that the return value of the extension method reflects the current value of that variable. This demonstrates that, behind the scenes, extension methods are invoked directly on the static class in which they are defined.

To run this code, copy and paste it into a Visual C# console application project that has been created in Visual Studio. By default, this project targets version 3.5 of the .NET Framework, and it has a reference to System.Core.dll and a using directive for System.Linq. If one or more of these requirements are missing from the project, you can add them manually. For more information, see How to: Create a LINQ Project.

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
VB.Net Example
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Linq

Namespace EnumExtension
  ' Define an extension method in a non-nested static class.
  Public Module Extensions
    Public minPassing As Grades = Grades.D
 
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Passing(ByVal grade As Grades) As Boolean
      Return grade >= minPassing
    End Function
  End Module

  Public Enum Grades
    F = 0
    D = 1
    C = 2
    B = 3
    A = 4
  End Enum

  Friend Class Program
    Shared Sub Main(ByVal args() As String)
      Dim g1 As Grades = Grades.D
      Dim g2 As Grades = Grades.F
      Console.WriteLine("First {0} a passing grade.", If(g1.Passing(), "is", "is not"))
      Console.WriteLine("Second {0} a passing grade.", If(g2.Passing(), "is", "is not"))

      Extensions.minPassing = Grades.C
      Console.WriteLine(Constants.vbCrLf & "Raising the bar!" & Constants.vbCrLf)
      Console.WriteLine("First {0} a passing grade.", If(g1.Passing(), "is", "is not"))
      Console.WriteLine("Second {0} a passing grade.", If(g2.Passing(), "is", "is not"))
    End Sub
  End Class
End Namespace

' Output:
'    First is a passing grade.
'    Second is not a passing grade.
'
'    Raising the bar!
'
'    First is not a passing grade.
'    Second is not a passing grade.
Advertisement