HomeSearch

C# Enum.GetName, GetNames: Get String Array From Enum

Use the Enum.GetName and Enum.GetNames methods. Get strings and string arrays from an enum.
Enum.GetName. An enum type has names. These names are string representations of the enum values. The .NET Framework provides the Enum.GetName and Enum.GetNames methods. These methods require the typeof operator. They return a string or a string array.Enum
Example. The first argument to the Enum.GetName method is a Type. And this is easily provided with the result of the typeof operator. The second is an object: this object must be of the same type as the backing field for the enum.

Here: In this example, an int is provided (with the value 3) and the correct MedicationType is returned.

Typeof, nameof
C# program that uses Enum.GetName using System; class Program { enum MedicationType { Default, Antipyretic, Corticosteriod, Cytotoxic, Decongestant, Expectorant }; static void Main() { string name = Enum.GetName(typeof(MedicationType), MedicationType.Cytotoxic); Console.WriteLine(name); name = Enum.GetName(typeof(MedicationType), 3); Console.WriteLine(name); } } Output Cytotoxic Cytotoxic
Performance. This method has lots of internal checks in it that will inhibit its runtime performance. If you are calling Enum.GetName in a tight loop for some reason, using a cache or finding another approach would be beneficial.
Enum.GetNames. Next, the Enum.GetNames method too requires the Type pointer for an enumerated type. This means you can pass it the typeof(Identifier) where Identifier is the identifier for your enumerated type.Type

Tip: The typeof operator invokes reflection and the Enum.GetNames method also internally accesses the metadata representation in memory.

Next: This program shows how an enumerated type of medication types is accessed in a string array.

Main: Here Enum.GetNames() is invoked. Enum.GetNames does not use an instance of an enumeration to be called.

GetNames: This is a static method. We pass it a Type object as the parameter. The typeof expression returns that Type object.

Static
C# program that uses Enum.GetNames using System; class Program { enum MedicationType { Default, Antipyretic, Corticosteriod, Cytotoxic, Decongestant, Expectorant }; static void Main() { // Get all enum names as strings. var names = Enum.GetNames(typeof(MedicationType)); // Write the enum names separated by a space. Console.WriteLine(string.Join(" ", names)); } } Output Default Antipyretic Corticosteriod Cytotoxic Decongestant Expectorant
Typeof parameter. The typeof operator also uses a parameter, but the parameter is a Type identifier. The MedicationType identifier is the name of the enum and not an instance of the enum.Reflection

Typeof: The typeof operator is a simple usage of reflection in that it accesses the program's metadata information.

Summary. The Enum.GetName method provides a way to get one string value based on an object value. The Enum.GetNames method, on the other hand, allows you to the entire collection of string representations at once.

Tip: If you need to repeatedly use enum names, storing them in memory as a string[] is sometimes best.

Enum ToString
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
Home
Dot Net Perls