TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# Enum ToString: Convert Enum to String

Convert an enum into a string with the ToString method. Inspect the ToString implementation.
Enum.ToString. This converts an enum into a string. This string can be used to display the name of the enum. It can be stored in a string variable or field.
An example. To declare an enum type we use the enum keyword. The enumerated constants can be any identifier, and their actual values are automatically incremented.

Next: In this program, the Priority.None enum will have value of 0. And the Priority.Critical enum will have value of 4.

Note: The ToString virtual method will look into the assembly metadata to get the string value to return.

Virtual

Tip: The first enumerated constant in an enum should be a "None" or "Zero" value so it can be correctly tested against zero.

Finally: We print the 5 string values in the enum. The GetNames method returns the same strings.

C# program that converts enumeration to string using System; enum Priority { None, Trivial, Normal, Important, Critical } class Program { static void Main() { // Write string representation for Important. Priority priorityValue = Priority.Important; string enumValue = priorityValue.ToString(); // Loop through enumerations. // ... Write string representations. Console.WriteLine("::FOR::"); for (Priority p = Priority.None; p <= Priority.Critical; p++) { string value = p.ToString(); Console.WriteLine(value); } Console.WriteLine("::GETVALUES::"); foreach (Priority p in Enum.GetValues(typeof(Priority))) { string value = p.ToString(); Console.WriteLine(value); } } } Output ::FOR:: None Trivial Normal Important Critical ::GETVALUES:: None Trivial Normal Important Critical
Reflection. The base.GetType() call, the GetValueField call, and InternalGetValue use reflection. They acquire the string representation from the enum constant from your source metadata.

Warning: This has a performance impact. But it also reduces the complexity the source code. You do not need to store the strings yourself.

Implementation of enumeration ToString public override string ToString() { Type type = base.GetType(); object obj2 = ((RtFieldInfo)GetValueField(type)) .InternalGetValue(this, false); return InternalFormat(type, obj2); }
A discussion. There is a static Enum.GetNames method that receives one Type parameter in the Enum class. This method provides a clearer way to get an array of all of the enum strings.

Often: It is preferable to use the GetNames method instead of the for-loop construct as shown. There are drawbacks to both approaches.

Enum.GetName

Credit: Thanks to Mike for pointing out the importance of using GetValues for clear C# code.

A summary. ToString uses reflection. It returns an enum's string representation. We saw an example of the output of calling ToString on all values in an enumerated type.Enum
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf