1

I am node.js developer, and currently i change my qualification on .net, and at the moment doing my pet-project.

In node.js, i can collect enums:

enum CustomExceptions = {
  UserNotFound = "User not found.",
  InternalException = "Internal Exception."
}

But in C# enums only can have int value :

enum CustomExceptions = {
  UserNotFound = 0,
  InternalException = 1
}

Can you explain me plz, how i can do it more usefull and good?

I read about [Description], but how i look, it is not the best choice, because i will need a new class for getting this property, however i want to avoid this way.

  • 1
    What are you trying to do in the first place? What is this `enum` supposed to be used for and why do you want to have `string` values for the enumeration? – UnholySheep May 09 '23 at 09:38
  • @UnholySheep It is a simple enumeration of properties in the first place, for returning a **string** value. For example - new Exception(CustomExceptions.UserNotFound) But in c# it will return **int** – perezvonish' May 09 '23 at 09:43
  • You can't. C# doesn't have syntax for that. Use this: https://stackoverflow.com/questions/26424853/enum-display-name-use-variable – rene May 09 '23 at 09:45
  • 1
    That doesn't really look like a use-case for enumerations, this looks more like something you'd achieve by having a `static class CustomExceptions { public const string UserNotFound = "User not found."; }`, etc. – UnholySheep May 09 '23 at 09:46
  • @UnholySheep yes, I already thought about statics, but due to insufficient implementation experience, I doubted such a solution. How often is this solution used in practice? – perezvonish' May 09 '23 at 09:48
  • I have used such solutions a few times, though it's always something I evaluate on a case by case basis – UnholySheep May 09 '23 at 09:50
  • Seldomly. Often its a combination of an enum and a mapping of the enum to a text. Have you thought about the need of localizing the text? That is often the driver to do it differently. – Ralf May 09 '23 at 09:50
  • @Ralf okey, but what solution is a correct for this? – perezvonish' May 09 '23 at 09:55
  • Ähm using the enum for internal logic? And when it gets to the point where a text needs to be represented to a user have a facility that maps the enum to the fitting localized text only then? How that facility looks is dependent on the system architecture. It could be some kind of mapper (Automapper) when you use domain models or a feature of the client side UI technology. Web stuff has different ways of localization then desktop stuff etc. – Ralf May 09 '23 at 10:13

1 Answers1

3

Pretty much this will give you what you need:

public static class CustomExceptions
{
    public const string UserNotFound = "User not found.";
    public const string InternalException = "Internal Exception.";
}
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
  • 2
    Additional info. Public const might lead to unexpected results if you change them without a complete rebuild of all using assemblies. Often "static readonly" is used instead of "const" because of this. – Ralf May 09 '23 at 10:26