Home > OS >  How to get the month name using Dictionary in C#?
How to get the month name using Dictionary in C#?

Time:10-22

I have implemented the first three letters of the month to full name of the month in get Full Month function it is returned based on three letters . But how to implemented in Dictionary concept Any one simplify modify this code given below code:

  **public static string getFullMonth(string mthname)
        {
            string Mthname = "";
            switch (mthname.ToUpper())
            {
                case "JAN":
                    Mthname ="January";
                    break;
                case "FEB":
                    Mthname = "February";
                    break;
                case "MAR":
                    Mthname = "March";
                    break;
                case "APR:":
                    Mthname = "April";
                    break;
                case "MAY":
                    Mthname = "May";
                    break;
                case "JUN":
                    Mthname = "June";
                    break;
                case "JUL":
                    Mthname = "July";
                    break;
                case "AUG":
                    Mthname = "August";
                    break;
                case "SEP":
                    Mthname = "September";
                    break;
                case "OCT":
                    Mthname = "October";
                    break;
                case "NOV":
                    Mthname = "November";
                    break;
                case "DEC":
                    Mthname = "December";
                    break;

                default:
                    Console.WriteLine("Invalid grade");
                    break;
            }
            return Mthname;
        }**

CodePudding user response:

simplify this code given below code

Yes, don't use a dictionary at all:

public static string GetFullMonth(string englishShortMonthName)
{
    CultureInfo englishCulture = CultureInfo.InvariantCulture;
    if (DateTime.TryParseExact(englishShortMonthName, "MMM", englishCulture, DateTimeStyles.None, out DateTime dt))
        return dt.ToString("MMMM", englishCulture);
    return englishShortMonthName;
}

Read about the month ("M", "m") format specifier

CodePudding user response:

Look up Dictionary syntax. Ultimately you're looking for something like this:

var monthNames = new Dictionary<string, string>
{
    { "JAN", "January" },
    { "FEB", "February" },
    ...
}

CodePudding user response:

Please find complete solution

public static string getFullMonth(string mthname)
{
    var monthNames = new Dictionary<string, string>
    {
              { "JAN", "January" },
              { "FEB", "February" },
                 ...
    }
    return monthNames[mthname];
}

But above code looks weird So You should create dictionary globally and initialize in constructor.

Add only below line in getFullMonth function.

return monthNames[mthname];
  •  Tags:  
  • c#
  • Related