Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow implicit conversion?

Tags:

c#

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?

like image 232
mpen Avatar asked Jan 02 '11 19:01

mpen


People also ask

What is the example of implicit conversion?

Implicit conversions: No special syntax is required because the conversion always succeeds and no data will be lost. Examples include conversions from smaller to larger integral types, and conversions from derived classes to base classes.

What two commands will perform implicit conversion?

Answer: Cast and Convert.

Which conversion can be performed implicitly?

We cannot perform implicit type casting on the data types which are not compatible with each other such as: Converting float to an int will truncate the fraction part hence losing the meaning of the value. Converting double to float will round up the digits.


2 Answers

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.

like image 59
Rex M Avatar answered Sep 21 '22 03:09

Rex M


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.

like image 41
Oded Avatar answered Sep 17 '22 03:09

Oded