Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I define both implicit and explicit operators?

Why I cannot define both implicit and explicit operators like so?

    public class C     {         public static implicit operator string(C c)         {             return "implicit";         }          public static explicit operator string(C c)         {             return "explicit";         }     } 

You can do this hack though :)

    class Program     {         public class A         {          }          public class B         {             public static implicit operator A(B b)             {                 Console.WriteLine("implicit");                 return new A();             }         }          public class C : B         {             public static explicit operator A(C c)             {                 Console.WriteLine("explicit");                 return new A();             }         }          static void Main(string[] args)         {             C c = new C();              A a = c;             A b = (A) c;         }     } 

This prints:

implicit explicit 
like image 937
Prankster Avatar asked Apr 17 '09 21:04

Prankster


People also ask

What is implicit operator in C#?

The Implicit Operator According to MSDN, an implicit keyword is used to declare an implicit user-defined type conversion operator. In other words, this gives the power to your C# class, which can accepts any reasonably convertible data type without type casting.

Which operator can be used to do type conversions in C#?

Use the operator and implicit or explicit keywords to define an implicit or explicit conversion, respectively. The type that defines a conversion must be either a source type or a target type of that conversion. A conversion between two user-defined types can be defined in either of the two types.

How do you define a cast in C#?

The operator used to perform cast operation in C# is parentheses. To perform a cast operation, the destination data type is explicitly written in parentheses before the value to be converted. An example for cast operation can be the conversion of a variable of double or float type to an integer type.


2 Answers

Check this: Why can't coexist implicit and explicit operator of the same type in C#?

If you define an explicit operator, you will be able to do something like this:

Thing thing = (Thing)"value"; 

If you define an implicit operator, you can still do the above, but you can also take advantage of implicit conversion:

Thing thing = "value"; 

In short, explicit allows only explicit conversion, while implicit allows both explicit and implicit... hence the reason why you can only define one.

like image 61
Tom Sarduy Avatar answered Sep 18 '22 15:09

Tom Sarduy


I don't know the technical limitation that prevents this, but I think they would have done this to prevent people from shooting their own feet off.

string imp = new C(); // = "implicit" string exp = (string)new C(); // = "explicit" 

That would drive me bonkers and makes no sense, C should only cast to a string 1 way, not 2 different ways.

like image 25
Samuel Avatar answered Sep 18 '22 15:09

Samuel