6

Having this code:

    class Program
    {
        static void Main(string[] args)
        {
            Check(3);
            Console.ReadLine();
        }

        static void Check(int i)
        {
            Console.WriteLine("I am an int");
        }

        static void Check(long i)
        {
            Console.WriteLine("I am a long");
        }

        static void Check(byte i)
        {
            Console.WriteLine("I am a byte");
        }    
    }

Why this code prints "I am an int" and not "I am a long" ?

Buda Gavril
  • 21,409
  • 40
  • 127
  • 196
  • Try `Check(3L)`. The L indicates to the compiler that you want the literal to be a `long` instead of an `int` – juharr Jul 27 '15 at 20:17
  • possible duplicate of [C# Method Resolution, long vs int](http://stackoverflow.com/questions/6125585/c-sharp-method-resolution-long-vs-int) – Ben N Aug 10 '15 at 22:35

1 Answers1

14

Why this code prints "I am an int" and not "I am a long" ?

Because the compiler goes through the rules of overload resolution, which are in the C# 5 spec, starting at section 7.5.3.

Both of those are applicable function members (i.e. they'd both be valid for the argument list) but the Check(int) method is "better" than the Check(long) method (section 7.5.3.2) because the type of the argument is int, and an identity conversion is "better" than a widening conversion (section 7.5.3.3).

Given an implicit conversion C1 that converts from an expression E to a type T1, and an implicit conversion C2 that converts from an expression E to a type T2, C1 is a better conversion than C2 if at least one of the following holds:

  • E has a type S and an identity conversion exists from S to T1 but not from S to T2
  • ...

Here E is int, T1 is int, and T2 is long. There's an identity conversion from int to int, but not from int to long... therefore this rule applies, and the conversion from int to int is better than the conversion from int to long.

Community
  • 1
  • 1
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon, I've updated my code and I've added another Check method with byte parameter, but the code prints again "I am an int". why 3 is considered an int and not a byte? – Buda Gavril Jul 27 '15 at 20:32
  • and where can I find these rules on MSDN site? – Buda Gavril Jul 27 '15 at 20:32
  • @BudaGavril: I wouldn't use MSDN - I'd download the complete specification: http://www.microsoft.com/en-us/download/details.aspx?id=7029 – Jon Skeet Jul 27 '15 at 20:33
  • can you please explain me what does identity conversion means? Here is what I've found on msdn :?An identity conversion converts from any type to the same type. This conversion exists only such that an entity that already has a required type can be said to be convertible to that type." but it's still not very clear for me – Buda Gavril Jul 27 '15 at 20:46
  • @Buda: As it says - it's a conversion from a type to itself... In this case from int to int. – Jon Skeet Jul 27 '15 at 20:48
  • but why couldn't be from byte to byte? Why is that special int type? – Buda Gavril Jul 27 '15 at 20:51
  • @Buda: Because the type of the literal 3 is int. Again, check the spec. – Jon Skeet Jul 27 '15 at 21:02