Question regarding implicit conversions in the C# language specification

前端 未结 4 858
渐次进展
渐次进展 2021-02-08 08:15

Section 6.1 Implicit conversions defines an identity conversion thusly:

An identity conversion converts from any type to the sam

4条回答
  •  不思量自难忘°
    2021-02-08 08:23

    An identity conversion converts from any type to the same type. This conversion exists such that an entity that already has a required type can be said to be convertible to that type.

    This says that in C#-land, 1==1; a Spade is a Spade. This is the basis of assigning an object reference to a variable of the same type; if a variable typed T1 and one typed T2 are in reality both Spades, it is possible to assign one to the other without having to explicitly cast one as a Spade. Imagine a C# variant where assignments had to look like this:

    Spade mySpade = new Spade();
    Spade mySpade2;
    
    mySpade2 = (Spade)mySpade; //explicit cast required
    

    Also, an "identity" in mathematics states that an expression that evaluates to a result given a set of inputs is equivalent to another expression that produces the same result given the same inputs. In programming, this means that an expression or function that evaluates to an instance of a type is equivalent to that type, without explicit conversion. If that didn't hold, the following code would be required:

    public int myMethod() { /*Do something*/ }
    ...
    int myInt = (int)myMethod(); //required even though myMethod() evals to an int.
    ...
    int myInt = (int)(1 + 2); //required even though 1, 2, and 1+2 eval to an int.
    

    The second rule basically says that a value type can be assigned to a member variable on a class if, in part, the member variable (a boxed type by definition, as its container is a reference type) is declared to be the same type. If this rule were not specified, theoretically, a version of C# could exist in which pure value types would have to be explicitly converted to their reference analog in order to be stored as a member variable on a class. Imagine, for example, a version of C# in which the blue keyword types (int, float, decimal) and the light blue class names (Int32, Float, Decimal) referred to two very different, only-explicitly-convertible types, and int, float, decimal etc. were not legal as member variable types because they were not reference types:

    public class MyClass
    {
      Int32 MyBoxedValueType; //using "int" not legal
    }
    
    ...
    
    MyClass myClass = new MyClass();
    int theInt = 2;
    
    myClass.MyBoxedValueType = (Int32)theInt; //explicit cast required
    

    I know it sounds silly, but at some level, these things must be known, and in computers, you have to specify EVERYTHING. Read the USA Hockey rulebook for ice hockey sometime; the very first rule in the book is that the game shall be played on an ice surface. It's one of the ultimate "well duhs", but also a fundamental truth of the game that must be understood in order for any other rule to make sense.

提交回复
热议问题