. Write a code in C# to implement an enumeration of 12 months named YEAR. Also identify that the given expressions will result in True or False.

 YEAR.May.ToString()="June"
 YEAR.August.ToString()="August"

Dani AI

Generated

The replies already hit the main problems: the original expressions use the assignment operator and therefore won't compile, and using an enum's ToString for logic is fragile. Below is a compact, correct implementation plus safe ways to compare and parse values.

enum YEAR
{
    January = 1,
    February,
    March,
    April,
    May,
    June,
    July,
    August,
    September,
    October,
    November,
    December
}

YEAR m = YEAR.May;
Console.WriteLine(m.ToString());                 // prints "May"
bool enumCompare = (m == YEAR.June);            // false — compare enums directly
bool stringCompare = m.ToString().Equals("May"); // true (case-sensitive)
int monthNumber = (int)m;                       // 5

For converting user input or case-insensitive checks, use Enum.TryParse:

if (Enum.TryParse<YEAR>("august", true, out YEAR parsed))
{
    // parsed == YEAR.August
}

Notes and best practices:

  • As observed, use == or .Equals for comparisons; a single = is assignment and will not compile when used with an expression like ToString().
  • Prefer comparing enum values (month == YEAR.June) rather than comparing ToString() results; ToString() returns the exact member name and is case-sensitive.
  • If numeric month numbering is required, set January = 1 (as above) and cast to int.
  • For localized month names, do not rely on enum names — use DateTime/CultureInfo to get culture-aware month names.
  • Style: the CLR convention is PascalCase (Year or Month) rather than all-caps YEAR, but use the name required by the assignment if needed.

This covers why the posted expressions fail to compile as written, what their truth values would be after fixing the operator, and safer, more robust patterns to work with month enums.

Recommended Answers

All 6 Replies

What have you done, besides posting your homework assignment?
Show that to us and pinpoint the errors you have. We will be more than happy to help.

Also, this isn't right:

YEAR.May.ToString()="June"

I'm sure whoever wrote it meant this:

YEAR.May.ToString()=="June"

gusano79 I think you just gave away the answer. :) The statement in the original post includes: "Also identify that the given expressions will result in True or False."

Once again, miriam_iqbal, can you provide us with what you've already done to attempt to solve this problem? :)

Tekkno

gusano79 I think you just gave away the answer. :) The statement in the original post includes: "Also identify that the given expressions will result in True or False."

I took it to mean "Here are some expressions, do they resolve to a) true, or b) false?" The expressions given don't do either; they won't compile.

Either way, that statement is not well written, and if I've accidentally answered part of the question, I humbly apologize and throw myself upon the mercy of the court...

LOL gusano79.
Well either way it's a good call :)

I humbly apologize and throw myself upon the mercy of the court...

I find you guilty and sentence you to ten lashes with a wet noodle. After that, you must endure...THE COMFY CHAIR! Muahahahaha!

commented: You're a cruel judge, with no compassion! +15
commented: I didn't expect th-- AAH NO, NO, NOT THE EXTREMELY SOFT CUSHIONS...! AAARGHHCHGBKL#BO%@B%$LB!!!#@$ +10
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.