HomeSearch

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

These C# example programs use the Enum.GetName and Enum.GetNames methods. They 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.

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

In the Main method,

Enum.GetNames method is invoked. Enum.GetNames does not use an instance of an enumeration to be called. It is a static method. We pass it a Type object as the parameter. The typeof expression returns that Type object.Static Method

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.

Finally,

the example program invokes string.Join to combine all the strings in the array into a single string separated by the space character. When executed, it outputs all the string representations on a single line.Join

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
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.