Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Order of implicit conversions in c#

What is the order of implicit conversions done in Console.WriteLine(x) when x is an object from user-defined class:

    class Vector
    {
        public int x = 12;       

        public static implicit operator double(Vector v1)
        {
            return 3.14;
        }

        public static implicit operator int(Vector v1)
        {
            return 42;
        }

        public override string ToString()
        {
            return this.x.ToString();
        }

    }

    static void Main(string[] args)
    {
        Vector v11 = new Vector();
        Console.WriteLine(v11);
    }

Why I get 42, and not 3.14 or "12"? Why I can not add an additional implicit conversion to string /there is Compiler error on the ambiguity between CW(int) and CW(string)/:

        public static implicit operator string(Vector v1)
        {
            return "42";
        }

I know I should avoid using the implicit cast, but just for curiosity!

like image 452
Matija Lokar Avatar asked Dec 04 '18 14:12

Matija Lokar


People also ask

Which is the correct order of implicit type conversion?

Implicit Type Conversion is also known as 'automatic type conversion'. It is done by the compiler on its own, without any external trigger from the user. It generally takes place when in an expression more than one data type is present.

What is implicit type conversion in C language?

Implicit type conversion in C language is the conversion of one data type into another datatype by the compiler during the execution of the program. It is also called automatic type conversion.

How many types of conversion are there in C?

There are two types of conversion: implicit and explicit. The term for implicit type conversion is coercion.

What is implicit type conversion with example?

In implicit typecasting, the conversion involves a smaller data type to the larger type size. For example, the byte datatype implicitly typecast into short, char, int, long, float, and double. The process of converting the lower data type to that of a higher data type is referred to as Widening.


1 Answers

So what is printed is based entirely on what overload of Console.WriteLine is chosen. Which overload is chosen is based on section 7.5.3.2 of the specs on "betterness" for function members.

An overload is "better" than another, when it has a parameter that is "more specific" than another one. "more specific" means there's an implicit conversion from the more specific type to the less specific type, and no implicit conversion from the less specific type to the more specific type.

object is the least specific overload, as there's no implicit conversion from it to int, double, or string, but there is one from every type to it. int is more specific than double because there's an implicit conversion from int to double, but no conversion from double to int. int and string have no implicit conversions between each other, so neither is more specific, and so neither is better or worse than the other.

So if there's an implicit conversion from your object to string then the string overload is considered, and there's a tie for "best" overload, and you get an error. When it's missing there's a "most specific" type of all of the considered overloads (which is int), so it's "the best", and that overload is chosen.

like image 67
Servy Avatar answered Sep 25 '22 13:09

Servy