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();
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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";
}
}
}
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();