Assignment operations, Implicit type conversions...

Assignment operations

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.

Implicit Conversion of Embedded Types

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:

T y

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).

thematic pictures

Also We Can Offer!

Other services that we offer

If you don’t see the necessary subject, paper type, or topic in our list of available services and examples, don’t worry! We have a number of other academic disciplines to suit the needs of anyone who visits this website looking for help.

How to ...

We made your life easier with putting together a big number of articles and guidelines on how to plan and write different types of assignments (Essay, Research Paper, Dissertation etc)