I need to use an Enum for readability and as Type string.

Ronald Rex 1,666 Reputation points
2023-11-07T21:02:06.4533333+00:00

I have this Enum that I am using along with an extension method. And I was just wondering if I should be able to assign an Error Level to a string variable like so..... string id = ErrorLevel.Low. For some reason I am getting a compile error... A value of type 'ErrorLevelExtensions.ErrorLevel' cannot be used as a default parameter because there are no standard conversions to type string. I thought my extension method returns a string?!?

public enum ErrorLevel
{
  None,
  Low,
  High,
  SoylentGreen
}

public static class ErrorLevelExtensions
{
  public static string ToFriendlyString(this ErrorLevel me)
  {
    switch(me)
    {
      case ErrorLevel.None:
        return "Everything is OK";
      case ErrorLevel.Low:
        return "SNAFU, if you know what I mean.";
      case ErrorLevel.High:
        return "Reaching TARFU levels";
      case ErrorLevel.SoylentGreen:
        return "ITS PEOPLE!!!!";
      default:
        return "Everything is Fine";
    }
  }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,108 questions
0 comments No comments
{count} votes

Accepted answer
  1. P a u l 10,746 Reputation points
    2023-11-07T21:48:24.7766667+00:00

    An extension method is just a method that you can call on a type, not a method that does an implicit conversion to another type.

    You can call it explicitly though:

    string id = ErrorLevel.Low.ToFriendlyString();
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.