Site Logo
Looking for girlfriend > Russian > C# enum friendly name

C# enum friendly name

Site Logo

If you develop using C and the. Net ecosystem, chances are that you will need to use enums at a certain point. The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list. I put 0, 1, 2 as comments just to point out that enums are by default of type int.

SEE VIDEO BY TOPIC: Lesson 11: Debugging and Enumerators - C# Programming Course

Content:

Enum: Using the Name() and toString() Methods Correctly

Site Logo

An Enumeration or enum is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language. Sometimes you might need to loop on the elements of a particular enum and print its element names, however as you know by the language constraints, the enum element names must follow the naming convention, you cannot include spaces or other special characters.

Therefore printing the elements will lead to printing the defined non-friendly elements names. In this article, I will be explaining and showing to you by a code sample how you can define and display friendly names for enumeration elements using the Description annotation. If you need to learn about extension methods with interactive examples, check my tech.

To get the enumeration elements names, we will iterate over the names of the Enumeration type, append each name to a string builder and then return the built string, see the below code snippet:. As you might have noticed from the above code, it resulted in displaying the names, as defined in the ColorsEnum enumeration data type. With the same casing.

Sometimes we need to print out a given enumeration element name in a user-friendly format, maybe for logging purposes to be verbose when displaying the enum values or sometimes you might want to populate the enum elements into a dropb, in this case we will need to add an attribute decoration on each of the enumeration names.

This attribute comes from the namespace System. The below code shows the updated version of the ColorsEnum , having the new Description attribute decoration applied to the enum names:. The Description attribute decoration is used to define a descriptive name for a give property or event. It can be used with either enum elements or with any user defined data type property or event. The description decoration is defined under the System. Even though we defined friendly descriptions for each of our enumeration elements, using the normal ToString method will keep printing non-friendly names.

So, printing the descriptive friendly names that we defined requires an extension method. The code below includes the above-mentioned extension method, another function that calls this extension method while looping through the enum elements and then it returns a string with the friendly names description attributes of enum appended. Please note that the extension method that can be used by any enumeration data type, not particularly our example of ColorsEnum:. As you have noticed, we are using the GetDescription method as it is part of the ColorsEnum This is the power of the extension methods.

As you can see, we are now able to display the enumeration values in a user-friendly way instead of displaying them as their code names. The below exercise requires basic knowledge of generics, if you are new to this topic or you forgot the syntax of writing generic methods, then why not learn about generics in c with in-depth article including interactive sample codes and exercises.

An enumeration is a great way to define a set of constant values in a single data type. In this article, we defined description attribute decorations on an enum and implemented an extension method for enum to read these decorations and get the values from it. I hope that I was able to simplify and explain how to display friendly names of enumerations in C.

Please let me know if you like this article. Try to implement this tutorial and if you need any help just leave me a comment.

Article has been ported from CodingSonata. Employers: discover CodinGame for tech hiring. Log In Sign Up. Definition An Enumeration or enum is a data type that includes a set of named values called elements or members. Showing how you can iterate over names dynamically using reflection. Showing how you can iterate over enum names dynamically using reflection.

Create your playground on Tech. This playground was created on Tech. Suggested playgrounds. Understanding Extension Methods in. C Refresh. Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!

Online Participants. Keyboard Shortcuts:?

How to Display Friendly Names for Enumerations in C#?

Wherever possible we use enumerations in our applications. Often the name of the enumeration isn't what you want to print on the screen. It could be because the enumeration is 2 words that have been joined together, or you want the description to start with a number not that we do this You might have a enumeration for Colours that a user is allowed to select from. The values available for this enumeration may be hypothetically :.

An Enumeration or enum is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language.

In my previous article, we have learned that how to bind combobox directly with enum. In my previous article , we learned how to bind a combobox directly with the enum. So here, we will learn a way to show user-friendly enum names to end users. Please note that here, I have considered the implementation mentioned in the previous article, so please check that first before going further.

Show User Friendly Enum Value Into The Combobox

Comment 0. The Java Enum has two methods that retrieve that value of an enum constant, name and toString. The toString method calls the name method, which returns the string representation of the enum constant. In listing 1, the value returned by calling the name and toString on an Animal. So given that both methods return the same value, you might think that they can be used interchangeably, and in the majority of cases, this would be true. However, the difference between these two methods is important. The name method is final, so it cannot be overwritten. Meanwhile, conversely, the toString method is open and can be overwritten. In fact, overwriting the toString method is encouraged. It should be implemented and return a friendly version of the enum constant.

EnumHelper - Getting a Friendly Description from an Enum

NET platform as a whole. While covering the language in lots of detail, it also provides enough coverage of various popular. NET technologies and techniques such as debugging for the reader to be successful on the. NET platform.

For the first article, I will show you how friendly looking, always helpful, good guy enum can become an asshole. An asshole who will steal your precious time!

GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. Already on GitHub?

C# stuff nobody told you – enum is a b*tch!

By using our site, you acknowledge that you have read and understand our Cookie Policy , Privacy Policy , and our Terms of Service. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Enum value names must follow the same naming rules as all identifiers in C , therefore only first name is correct.

SEE VIDEO BY TOPIC: C# enums in arabic

Buy the print C 5. See inside the book for access code and details. While giving you those insights, you learn where and how to use the features to design various kinds of software. NET platform as a whole. The early chapters introduce the.

User Friendly Display Text For Enum in C#

An Enumeration or enum is a data type that includes a set of named values called elements or members. The enumerator names are usually identifiers that behave as constants in the language. Sometimes you might need to loop on the elements of a particular enum and print its element names, however as you know by the language constraints, the enum element names must follow the naming convention, you cannot include spaces or other special characters. Therefore printing the elements will lead to printing the defined non-friendly elements names. In this article, I will be explaining and showing to you by a code sample how you can define and display friendly names for enumeration elements using the Description annotation.

Jan 19, - Now, when we want to get the friendly name from the enum, all we do is call: ils1993.comcription(ils1993.comPink);. The code.

Sanity Free Coding Methods to the Madness. NET enums are a strongly typed flag-like object. Flags would normally defined as a macro, or constant variable.

.

.

.

.

.

.

Comments: 3
  1. Moogugis

    I consider, that you are not right. I suggest it to discuss. Write to me in PM, we will talk.

  2. Daishura

    It is remarkable, very useful phrase

  3. Gazahn

    I apologise, but it not absolutely that is necessary for me.

Thanks! Your comment will appear after verification.
Add a comment

© 2020 Online - Advisor on specific issues.