Visual Studio 2010 - Visual C# 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.

Example
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.

Compiling the Code
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.

See Also
|
Visual Studio 2010 - Visual C# Gewusst wie: Erstellen einer neuen Methode für eine Enumeration (C#-Programmierhandbuch) Mit Erweiterungsmethoden können Sie Funktionen hinzufügen, die für einen bestimmten Enumerationstyp spezifisch sind.

Beispiel
Im folgenden Beispiel stellt die Grades-Enumeration die möglichen Benotungen (Buchstaben) dar, die ein Student in einem Kurs erhalten kann. Eine Erweiterungsmethode mit dem Namen Passing wird dem Grades-Typ hinzugefügt, mit dem jede Instanz dieses Typs angewiesen wird, ob der Student mit dieser Benotung den Kurs besteht.
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.
*/
Beachten Sie, dass die Extensions-Klasse ebenfalls eine statische Variable enthält, die dynamisch aktualisiert wird und dass der Rückgabewert der Erweiterungsmethode den aktuellen Wert dieser Variable darstellt. Dies zeigt, dass Erweiterungsmethoden im Hintergrund direkt für die statische Klasse aufgerufen werden, in der sie definiert sind.

Kompilieren des Codes
Um diesen Code auszuführen, kopieren Sie ihn, und fügen Sie ihn in ein Visual C#-Konsolenanwendungsprojekt ein, das in Visual Studio erstellt wurde. Dieses Projekt gilt standardmäßig für .NET Framework Version 3.5 und hat einen Verweis auf System.Core.dll sowie eine using-Direktive für System.Linq. Wenn eine oder mehrere dieser Anforderungen im Projekt fehlen, können Sie sie manuell hinzufügen. Weitere Informationen finden Sie unter Gewusst wie: Erstellen eines LINQ-Projekts.

Siehe auch
|