开发者

How to allow implicit conversion?

开发者 https://www.devze.com 2023-02-02 06:44 出处:网络
Given some code like this: class Json { } class Program { 开发者_JAVA技巧 static void Main(string[] args)

Given some code like this:

class Json
{
}

class Program
{
   开发者_JAVA技巧 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?


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.


Add an implicit operator:

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

The implicit keyword is used to declare an implicit user-defined type conversion operator. Use it to enable implicit conversions between a user-defined type and another type, if the conversion is guaranteed not to cause a loss of data.


You can have an implicit converter but take care you don't lose data:

By eliminating unnecessary casts, implicit conversions can improve source code readability. However, because implicit conversions can occur without the programmer's specifying them, care must be taken to prevent unpleasant surprises. In general, implicit conversion operators should never throw exceptions and never lose information so that they can be used safely without the programmer's awareness. If a conversion operator cannot meet those criteria, it should be marked explicit.

see MSDN

0

精彩评论

暂无评论...
验证码 换一张
取 消