« Enum » : différence entre les versions

De Banane Atomic
Aller à la navigationAller à la recherche
Ligne 73 : Ligne 73 :
= Description =
= Description =
<kode lang='cs'>
<kode lang='cs'>
var name = Enum.GetName(typeof(MyEnum), 1); // Un
var name = MyEnum.Un.ToString(); // Un
var description = EnumExtension.GetDescriptionOtherwiseName(typeof(MyEnum), 1); // Un !
var description = MyEnum.Un.GetDisplayName(); // Un !
var description = MyEnum.Un.GetDescriptionOtherwiseName(); // Un !
var description = EnumExtension.GetDisplayName(typeof(MyEnum), 1); // Un !


enum MyEnum
enum MyEnum
Ligne 87 : Ligne 87 :
public static class EnumExtension
public static class EnumExtension
{
{
     // static method
     // extension method
     public static string GetDescriptionOtherwiseName(Type enumType, object value)
     public static string GetDisplayName(this Enum source)
     {
     {
         var name = Enum.GetName(enumType, value);
        var enumType = source.GetType();
         var name = Enum.GetName(enumType, source);


         var fieldInfo = enumType.GetField(name);
         var fieldInfo = enumType.GetField(name);
Ligne 98 : Ligne 99 :
     }
     }


     // extension method
     // static method
     public static string GetDescriptionOtherwiseName(this Enum source)
     public static string GetDisplayName(Type enumType, object value)
     {
     {
        var enumType = source.GetType();
         var name = Enum.GetName(enumType, value);
         var name = Enum.GetName(enumType, source);


         var fieldInfo = enumType.GetField(name);
         var fieldInfo = enumType.GetField(name);

Version du 8 juin 2021 à 17:37

Bases

Csharp.svg
enum MyEnum { Un = 1, Deux = 2 }

// Compter le nombre d’élément d’une énumération.
int count = Enum.GetValues(typeof(MyEnum)).Length;

// Retrieves the name of the constant that has the specified value.
Enum.GetName(typeof(MyEnum), 1);  // → "Un"

// Get the name of the constant.
MyEnum.Un.ToString();  // "Un"

// Liste des noms des éléments de l'énumération.
Enum.GetNames(typeof(MyEnum));  // → "Un"  |  "Deux"

// Liste des valeurs de l'énumération.
Enum.GetValues(typeof(MyEnum));  // → MyEnum.Un  |  MyEnum.Deux

Convertions

int / byte

Cs.svg
byte unEntier = 1;
MyEnum myEnum = (MyEnum)unEntier;  // Deux

byte unEntier = (byte)MyEnum.Deux;  // 1

string

Cs.svg
// Parser un string en enum. Génère un ArgumentException en cas d'échec.
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), "un", true);
// 3ème arg: IgnoreCase

MyEnum myEnum;
if (Enum.TryParse("un", true, out myEnum)) { }

Flags

Csharp.svg
[Flags]
public enum Font {
    Bold = 1,             // 1
    Italic = 1 << 1,      // 2
    Underlined = 1 << 2,  // 4
    All = Bold | Italic | Underlined
}

// gras et italique
Font font = Font.Bold | Font.Italic;

// ajout de souligné
font |= Font.Underlined;

// suppression de souligné
font &= ~Font.Underlined;

// test si font contient Underlined
if (font.HasFlag(Font.Underlined)) { }
if ((font & Font.Underlined) == Font.Underlined) { }

Console.WriteLine(Font.Bold | Font.Italic | Font.Underlined); // All

Default value

Cs.svg
int i = default(MyEnum);
// retourne toujours 0, même si 0 n'est pas une valeur valide

Description

Cs.svg
var name = MyEnum.Un.ToString(); // Un
var description = MyEnum.Un.GetDisplayName(); // Un !
var description = EnumExtension.GetDisplayName(typeof(MyEnum), 1); // Un !

enum MyEnum
{
    [Description("Un !")]
    Un = 1,
    [Description("Deux !")]
    Deux = 2
}

public static class EnumExtension
{
    // extension method
    public static string GetDisplayName(this Enum source)
    {
        var enumType = source.GetType();
        var name = Enum.GetName(enumType, source);

        var fieldInfo = enumType.GetField(name);
        var attributes = fieldInfo.GetCustomAttributes<DescriptionAttribute>();

        return attributes.SingleOrDefault()?.Description ?? name;
    }

    // static method
    public static string GetDisplayName(Type enumType, object value)
    {
        var name = Enum.GetName(enumType, value);

        var fieldInfo = enumType.GetField(name);
        var attributes = fieldInfo.GetCustomAttributes<DescriptionAttribute>();

        return attributes.SingleOrDefault()?.Description ?? name;
    }
}