Csharp + ASP.NET series tutorial (3)

zhaozj2021-02-16  49

Mike old cat comes from: the ideal of the old cat

This tutorial references to the C # and the ASP.NET programming tutorial, what is wrong, please point out, or in the ideal blog of the old cat. This time we first explain the type conversion, we often use type conversions when writing programs, and special rules. I am roughly explained here. Implicit conversion is the system default, no need to declare the conversion.

1. Implicit numerical conversion

Implicit numerical conversion is actually converted from low-precision numerical types to high precision numerical types.

Byte x = 255; ushort y = x; y = 65535; float z = y; / / all from low precision to high precision, it will produce overflow

There are too many types of implicit numerical conversion, I will not introduce, remember the above principles. Detailed rules can view MSDN

2. Implicit enumeration conversion

Implicit enumeration conversion is used to convert the decimal integer 0 into any enumeration type, and the corresponding other integers do not exist.

Using system;

ENUM Color

{

Red, Green, Blue

}

Class Mikecat

{

Static void main ()

{

Color C; // Declare the variable C of Color;

C = 0; // convert 0 to RED;

Console.Writeline ("value of C) is {0}", c); // Result: The value of c is RED; if c = 0 is changed to c = 1, the compiler will give an error.

}

}

3. Implicit reference conversion

From any reference type to Object conversion.

From class type A to class type B conversion, where class A is derived from class B.

From class type A to interface type B conversion, where class A implements interface B.

The conversion from the interface type A to the interface type B, where the interface A is derived from the interface B.

Transition from any array type to System.Array.

Conversion from any delegate type to System.deLegate.

Conversion from any array type or delegate type to System.icloneable.

From empty types (null) to any converted type. Display conversion is also called forced conversion, which requires the user to explicitly specify the type of conversion.

CHAR C = (char) 65; // a

INT i = (int) 'a'; // 65

The display conversion contains all implicit conversions, ie any system allowed to write to display conversions are allowed.

INT i = 300;

Long L = (long) i;

Another example:

Using system;

Class Mikecat

{

Static void main ()

{

Long longvalue = int64.maxvalue;

INT INTVALUE = (int) longage;

Console.writeLine ("(int) {0} = {1}", longue, intValue;

}

}

Type long conversion to int is explicit conversion, which uses forced conversion expressions. The output is:

(int) 9223372036854775807 = -1 This is because there is an overflow.

Display numerical conversion

Display numerical conversion refers to the conversion from a numerical type to another when there is no corresponding implicit conversion. The type of conversion is also very cumbersome, just remember the conversion rules, check out the MSDN. Since the display numerical conversion may lose information or trigger an exception, the conversion is processed as follows: Simply, the high-precision display conversion is low-precision, an OverflowException, and the value of the source variable is obtained by rounding. The closest value is the result of the conversion. Abnormal situation in detail, check MSDN

/*test.cs/

Using system;

Public Class Mikecat

{

Public static void main () {

Ushort u = 65535;

BYTE B = (byte) u;

Console.writeline ("The value of B is {0}", b);

}

}

The compilation status is as follows:

E: /> CSC Test.cs

Microsoft (R) Visual C # .NET compiler version 7.10.3052.4

Used for Microsoft (R) .NET Framework version 1.1.4322

Copyright (C) Microsoft Corporation 2001-2002. all rights reserved.

E: /> TEST.EXE

B value is 255

E: /> CSC / Checked Test.cs /// Checked [ | -] Generate Overflow Check

E: /> TEST.EXE

Unprocessed exception: System.OverflowException: Arithmetic operation results in overflow.

At mikecat.main ()

E: /> CSC / Checked- Test.cs

E: /> TEST.EXE

B value is 255

2. Display enumeration conversion

The display enumeration conversion is actually implicitly displayed between the element type of the enumeration type and the corresponding type. For example, an enumerated type E of element type INT E, when performing a display enumeration transfer from e to byte, actually executed from int to byte display numerical conversion.

Using system;

ENUM Color

{

Red, Green, Blue

}

Public Class Mikecat

{

Static void main ()

{

Color C; // Declare the variable C of Color;

c = (color) 4; // Digital 3 to display enumeration conversion

Console.writeline ("The value of C is {0}", c);

}

}

RESULTS: The value of C is 4

Convert class

Convert a basic data type to another basic data type.

This class return value is equivalent to the value of the value of the specified type. The supported base type is Boolean, CHAR, SBYTE, BYTE, INT16, INT32, INT64, UINT16, UINT32, UINT64, SINGLE, DOUBLE, DECIMAL, DATETIME, and STRING.

There is a conversion method that converts each of the base types to each other base type. However, the actual conversion operation performed is divided into three categories:

The type of transition from a type to its own is returned. No conversion is not actually implemented.

The conversion that cannot generate meaningful results triggers InvalidCastException. No conversion is not actually implemented. The following conversions will trigger an exception: converted from char to Boolean, Single, Double, DECIMAL, or DATETIME, and converted from these types to Char. The following conversions will trigger an exception: convert from DateTime to any type other than String, and from any type (except String) to DateTime.

Any base type (except for the base type described above) can be converted to any other basic type.

If the digital type conversion causes the accuracy loss (ie some minimum significant bit loss), no exception is caused. However, if the result exceeds the range of the return value of the particular conversion method, exceptions will be thrown. Let's introduce the packing and type conversion related packages.

The box is an implicit conversion of any interface type that the value type to the Object type or the value implemented by the value type. Assign a value of the value of the value to an object instance and copy the value into the new object.

Please see the following value type variable:

INT i = 123;

The following statement is implicitly applied to the variable i:

Object o = i;

The result of this statement is to create an object O on the stack, and the object references the value of the int type on the heap. This value is a copy of the value type value assigned to the variable I. The figure below illustrates the difference between the two variables I and O. Packing conversion

On the stack

On the pile

i 123

INT i = 123;

o

(Put i)

Object o = i;

INT 123

It is also possible (but not necessarily) explicitly performing a box as shown in the following example:

INT i = 123;

Object O = (Object) i;

Example

This example converts the integer variable I to the object O by the packing box. Thus, the value stored in the variable I is changed from 123 to 456. This example shows the original copy of the content, ie 123.

// boxing.cs

// boxing an integer variable

Using system;

Class testboxing

{

Public static void main ()

{

INT i = 123;

Object o = i; // iMPlicit Boxing

I = 456; // Change the Contents of i

Console.writeline ("The value-type value = {0}", i);

Console.writeline ("The Object-Type Value = {0}", O);

}

}

Output

The value-type value = 456

The Object-Type Value = 123

Unpack

The unpacking box is an explicit conversion from an Object type to a value type or from an interface type to a value type that implements the interface. Cancel packing operations include:

Check the object instance to make sure it is a packing value of a given value type.

Copy this value from an instance to a value type variable.

The following statement also describes the packing and unpacking operation:

INT i = 123; // a value type

Object Box = i; // boxing

INT j = (int) box; // unboxing

The figure below shows the results of the above statement. Cancel the box conversion

On the stack

On the pile

i 123

INT i = 123;

o

(Put i)

Object o = i;

INT 123

J 123

INT j = (int) O;

In order to make the rendering of the given value type to success during runtime, the value of the source parameter must be a reference to an object, and the object is previously created by the value of the value type. If the source parameter is NULL or a reference to an incompatible object, INVALIDCASTEXCEPTION is triggered.

Example

The following example interprets the case where the box is not removed, that is, the wrong unpacking box results in InvalidCastException. Error messages are displayed by using try and catch.

Using system;

Public class unboxingtest

{

Public static void main ()

{

INT INTI = 123;

// boxing

Object o = inti;

// Reference to incompatible Object Produces InvalidCastException

Try

{

INT INTJ = (Short) O;

Console.writeline ("Unboxing OK.");

}

Catch (InvalidCastexception E)

{

Console.writeline ("{0} error: incorrect unboxing.", E);

}

}

}

Output

System.invalidcastException

At unboxingtest.main () error: incorrect unboxing.

If the following statement:

INT INTJ = (Short) O;

change to:

INT INTJ = (int) O;

The conversion will be executed, and you will get the output "UNBoxing OK".

转载请注明原文地址:https://www.9cbs.com/read-23414.html

New Post(0)