Can I implement an implicit 'conversion' from string to boolean in C#?

There's any way to implement an implicit conversion from string to bool using C#?

E.g. I have the string str with value Y and when I try convert (cast) to boolean it must return true.

Jon Skeet
people
quotationmark

No. You can't create user-defined conversions which don't convert either to or from the type they're declared in.

The closest you could easily come would be an extension method, e.g.

public static bool ToBoolean(this string text)
{
    return text == "Y"; // Or whatever
}

You could then use:

bool result = text.ToBoolean();

But you can't make this an implicit conversion - and even if you could, I'd advise you not to for the sake of readability.

people

See more on this question at Stackoverflow