C# Enum.GetName

Enum type

An enum type has names. Names are the string representation of the enum values. The .NET Framework provides the Enum.GetName and Enum.GetNames methods. These methods require the use of the typeof operator. They return a string or a string array.

These C# example programs introduce the Enum.GetName and Enum.GetNames methods. They use the typeof operator.

Enum.GetName

Note

The first argument to the Enum.GetName method is a Type. 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. In this example, an int is provided (3) and the correct MedicationType is returned.

Typeof Operator
Program that uses Enum.GetName [C#]

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 optimization

Performance. This method has a lot 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 to the problem would be beneficial.

Enum.GetNames

Next, the Enum.GetNames method too requires the Type pointer for an enumerated type, which means you can pass it the typeof(Identifier) where Identifier is the identifier for your enumerated type.

The typeof operator invokes reflection and the Enum.GetNames method also internally accesses the metadata representation in memory. This program shows how an enumerated type of medication types is accessed in a string array.

Program that uses Enum.GetNames [C#]

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
Main method

Description. The program defines the Main entry point as well as the MedicationType enumerated type. In the Main method, the Enum.GetNames method is invoked. Enum.GetNames does not use an instance of an enumeration to be called—it is a static method. You must pass it a Type object as the parameter. The typeof expression returns that Type object.

Typeof operator

Typeof. 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. The typeof operator is a simple usage of reflection in that it accesses the program's metadata information.

ReflectionArray type

Displaying string array. Finally, the example program invokes the string.Join method to combine all the strings in the array into a single string separated by the space character. When you execute the program, it will output all the string representations of the enumerated constants on a single line.

String Join Method String Array

Summary

The C# programming language

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. If you need to repeatedly use enum names, storing them in memory as a string[] is sometimes best.

Enum ToString Method Enum Examples
.NET