mpen mpen - 23 days ago 17
C# Question

How to allow implicit conversion?

Given some code like this:

class Json
{
}

class Program
{
static void Main(string[] args)
{
}

Json MyAction()
{
return "{\"json\": 1}";
}
}


Is there anything I can add to the
Json
class to make this compile? Something to let the compiler know that it can implicitly cast a string to a Json?

Answer

The implicit operator declaration in c# allows you to specify:

{type to convert to} ({type to convert from} variablename)

Here's a simple example:

class Json
{
    private Json(string json)
    {
        //logic to parse string into object
    }

    public static implicit operator Json(string input)
    {
        return new Json(input);
    }
}

Some notes:

  • First, I wouldn't necessarily go this route, since most strings in your application are not semantically equivalent to a JSON string. The purpose of a conversion operator is to say two types always represent semantically equivalent information (or close enough to semantically equivalent to be usefully treated as such). I'd recommend implementing a static Json Parse(string input) or maybe even a static bool TryParse(string input, out Json json) method on your Json class instead. The callsite, generally, ought to know whether it expects its own string to contain Json.

  • Typically if my class offers implicit type conversion from a type, I find it best to make any parsing or ctor logic for that same type private. This ensures there's only one way for consumers to do a particular thing, not two ways (ctor and conversion) - hence the private ctor in the example above. There can be exceptions to this but it's been a good general rule for me.

  • The implicit conversion also allows some interesting things with comparison operators. For example, now that you can implicitly convert from string to json, you can also do: if(myJson == "blah") and it will do the conversion and then call the == operator on your Json object, which by default will do a reference equality comparison.

Comments