Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# parameter implicit conversion

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" ?

like image 941
Buda Gavril Avatar asked Jul 27 '15 20:07

Buda Gavril


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language basics?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.


1 Answers

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.

like image 80
Jon Skeet Avatar answered Oct 12 '22 19:10

Jon Skeet