In C #, assignment is an operation that can be used in expressions. In an expression called a multiple assignment (i), the list of variables is assigned the same value. For example:
x = y = z = w = (u + v + w)/(u-v-w);
When you assign variables of different types, type conversion is performed. The compiler attempts to convert the type of the variable on the right to the type of the variable on the left.
Assignment of the variable on the left (type T), the value of the variable or the result of evaluating the expression (type T1) is possible only in the following cases:
• if the types T and T1 are the same;
• if type T is the parent (parent) type for type T1 (in accordance with type inheritance);
• if there is an implicit conversion of type T1 to type T;
• If an explicit conversion of type T1 to type T is specified;
• If an explicit conversion to type T is given in the T1 type declaration (see Section 4.9).
Since all classes in C # - built-in and user-defined - by definition are descendants of the Object class, it follows that you can assign expressions of any type to the Object class.
Implicit Type Conversion
For types, there may be implicit or an explicit transformation. For example, for built-in arithmetic types, there is an implicit conversion scheme, shown in Fig. 3.4.
Fig. 3.4. Implicit conversion of built-in types
If there is a path (arrows) from type A to type B in this diagram, this means that there is an implicit conversion from type A to type B. For example, from short to float. All other transformations between subtypes of arithmetic type exist, but can be specified only explicitly, for example from float to int.
Explicit type conversion
To specify an explicit type conversion, use the cast to ( casting ) operation, which has the highest priority and the following view:
(type) & lt; expression & gt;
It specifies an explicit conversion of the type specified by by the expression to the type specified in parentheses. For example: int i = (int) 2.99; // i = 2;
In this case, the constant type 2.99 (the default is double) is converted to int, by truncating the fractional part. Or, for example, if the user types T and P are described and an explicit conversion to type P is described for type T, then the following entry is possible:
P x = new P (); y = (T) x;
It should be noted that there are explicit conversions within an arithmetic type, but there is, for example, no explicit conversion of an arithmetic type to a bool type. For example:
double a = 5.0; int p = (int) a;
bool b = (bool) a; // error, no such conversion!
In some cases, you can specify the required conversion explicitly using special conversion methods defined in the System.Convert class that convert one type value to another type values (including string-type values to embedded-type values).
Also We Can Offer!
- Argumentative essay
- Best college essays
- Buy custom essays online
- Buy essay online
- Cheap essay
- Cheap essay writing service
- Cheap writing service
- College essay
- College essay introduction
- College essay writing service
- Compare and contrast essay
- Custom essay
- Custom essay writing service
- Custom essays writing services
- Death penalty essay
- Do my essay
- Essay about love
- Essay about yourself
- Essay help
- Essay writing help
- Essay writing service reviews
- Essays online
- Fast food essay
- George orwell essays
- Human rights essay
- Narrative essay
- Pay to write essay
- Personal essay for college
- Personal narrative essay
- Persuasive writing
- Write my essay
- Write my essay for me cheap
- Writing a scholarship essay