Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

4
  • 2
    You should be careful with .First(). This will throw an exception for example if your enum name is "Equals" Commented Jun 5, 2018 at 14:03
  • 2
    I understand the "danger" with First(). In this particular case it doesn't seem an issue. Because it is an extension method where this must be a valid (not null) Enum value. Otherwise calling the method would already throw (which is a responsibility of the calling code). This makes that GetType() will for sure provide the correct Enum Type in which enumvalue for sure will be a member. But GetCustomAttribute might return a null value so I provided a non-exceptional version of the method to return null when the chain of method calls has a null return value somewhere. Thanks! Commented Jun 6, 2018 at 14:33
  • 3
    For the second variant of your code, it seems like there is no need to use null-conditional operator after GetMember because this method always returns an array of MemberInfo and never returns null. And for me it seems that it is better to use FirstOrDefault instead of just First. Then the using of null-conditional operator after FirstOrDefault will be seen consistent. Commented Jul 23, 2018 at 10:27
  • You should consider check if the ResourceType is not null, in order to load the resource string. In Multilanguage applications, the display name is loaded from resource. Commented Dec 24, 2020 at 16:46