2

Screenshot

If I assign the same number to the variables in the enum structure, which one will it call?

It varies according to the same number of values ​​I give, 2 same value or 3 same value. Sometimes first variable comes back, sometimes last.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
  • 4
    Please post code as *text* rather than images. But fundamentally, if you've got two names for the same value, it's still just one value. There is no "first variable" - perhaps you're referring to the result of calling `ToString()` on the value? If so, the documentation states: "If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return." If that's not what you mean, please clarify your question – Jon Skeet Aug 09 '23 at 09:42
  • 2
    See the duplicate. – Guru Stron Aug 09 '23 at 09:49
  • 1
    **Third advice:** Keep your variable names English. Use "Months" instead of "Aylar" **Fourth advice:** Never post your code as screen shot to Stackoverflow. It is not helping us to test it and nobody has time to rewrite your code. – Ozan BAYRAM Aug 09 '23 at 10:08
  • Thanks for your informations. This was my first question. – Emirhan Şen Aug 09 '23 at 10:33

2 Answers2

0

First advice: Do not use enumeration for months. If you need name of the month you may use something like this

// to get the full month name 
string _monthName = date.ToString("MMMM");

// to get the month name for 12th month according to locale
string _monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(12);

Second Advice: Prefer to use unique values for your enumerations. Otherwise you can only use them one way to resolve it. You may resolve value form string but if you have repeated values it is not possible to resolve string representation from value.

Ozan BAYRAM
  • 2,780
  • 1
  • 28
  • 35
  • "Always" is a stretch here, I think (as it usually is, IMO - I can't think of many pieces of advice which really have no situations where it doesn't make sense to violate that advice). There are times where it makes sense to overload the naming - and that's fine so long as you know the implications. You can write code to canonicalize the "value to string" conversion if you need to. – Jon Skeet Aug 09 '23 at 09:57
  • That's better. Although I'd also suggest following .NET naming conventions for variables (`monthName` instead of `month_name`). I'd remove the third and fourth bits as they're not really about the question topic - better as comments on the question IMO. – Jon Skeet Aug 09 '23 at 10:06
-1

Just to make things simpler, Think of values as a uniqueId. As unique ids can not be duplicate. So, you must pass different values to avoid ambiguity.

  • Except that if the values were *actually* meant to be unique, that would be enforced. It's absolutely fine to have multiple names for the same value... you just can't depend on the result of ToString being any specific name. – Jon Skeet Aug 09 '23 at 09:49
  • You can define enum members with the same value. – Guru Stron Aug 09 '23 at 09:49
  • @JonSkeet all i meant to say is that there is no need to assign same value to two different variables as it doesnt makes sense at all. – Deepankar Joshi Aug 09 '23 at 09:51
  • @JonSkeet Even though unique values are not enforced, it should be a common sense to give diiferent ids to avoid any problems in future – Deepankar Joshi Aug 09 '23 at 09:52
  • @DeepankarJoshi: They're not "ids" though - they're just names. And in some cases it absolutely makes sense to give different names to multiple values - while in other cases it doesn't. I don't think a broad generalization helps, nor does it answer the question. (And these aren't "two different variables" - there aren't variables involved here in the normal sense, and I believe using the term "variable" could easily cause confusion...) – Jon Skeet Aug 09 '23 at 09:55
  • @JonSkeet sorry cant help you – Deepankar Joshi Aug 09 '23 at 10:14
  • I wasn't looking for help - I was offering a critique of your answer... – Jon Skeet Aug 09 '23 at 10:17