C # Variable Type (1): Reference Type and Value Type

zhaozj2021-02-16  52

C # is a type of security language. Each variable requires defined as a specific type and requires the value stored in the variable to only the value of this type.

Variables can save the value type or save the reference type, and can also be a pointer. This lesson will tell the first two types, and we will carry out in the next lesson.

Below is an incorporated of different points for value types and reference types:

If a variable V is stored, it stores the object containing data directly. Any other variable V 'cannot directly store objects stored by V, although V' can store objects that are stored in the same value with the same value. (Translation: This means that the objects stored by V and V 'are not coheed, and any change in one of the stored objects will not affect the object of another variable storage.)

If the variable V is stored a reference type, it stores a reference to the object, and there may be another variable V ', which also stores a reference to this object. (Translation: This means that the object changed to change V 'reference will also affect the object of V reference, and vice versa.)

Value type

In C # You can define your own value type by declaring enumeration types or structural types. C # processes the user-defined type and C # predefined value type type in the same manner, but the C # compiler may be better than the deforens. The table below lists some information for predefined value types in C #. Because all the basic value types in C # are developed from the Object Type (final base class), the System type in the .NET framework corresponding to these predefined types is also displayed in the table below.

C # type

.NET frame type

Have a sign

Dominant

Ranges

Sbyte

System.sbyte

Yes

1

-128 to 127

Short

System.Int16

Yes

2

-32768 to 32767

int

System.Int32

Yes

4

-2147483648 to 2147483647

Long

System.Int64

Yes

8

-9223372036854775808 to 9223372036854775807

Byte

SYSTEM.BYTE

no

1

0 to 255

Ushort

System.uint16

no

2

0 to 65535

Uint

System.uint32

no

4

0 to 4294967295

Ulong

System.uint64

no

8

0 to 18446744073709551615

Float

System.single

Yes

4

Possible value from ± 1.5 x 10-45 to ± 3.4 x 1038, 7 valid numbers after decimal point

Double

System.double

Yes

8

Possible values ​​from ± 5.0 x 10-324 to ± 1.7 x 10308 After decimal points 15 to 16 effective numbers

Decimal

System.decimal

Yes

12

Possible value from ± 1.0 x 10-28 to ± 7.9 x 1028 Rule 28 to 29 valid numbers after the decimal point

charr

System.char

N / a

2

Any 16 unicode character

Bool

System.Boolean

N / a

1/2

True or False

In the code below, both variables are declared as shaping and they are assigned:

INT x = 10; int y = x; y = 20; // This statement is 10, the value of Y is 20;

Reference type

C # predefined reference types include Object and String types. As we mentioned above, the Object type is all other types of final base classes. User-defined reference types can be interface types, class types, and delegate types (item 12 will be specified). The reference type is actually saved a memory address to the object that it references. Two variables in the following code segment references the same object (in this case, assuming this object has a data member 'myvalue'):

Object x = new object (); x.myvalue = 10; Object Y = x; y.myvalue = 20; // This statement is executed, the value of X.MyValue and Y.MyValue is 20.

The above code demonstrates a feature of the reference type: Changing the properties of an object pointing to the reference point will also affect all other references to this object. However, the Strings type is also a reference type, but its work is more like value type. When a string is specified for another string, for example:

String S1 = "Hello"; string S2 = S1;

Both S2 and S1 references the same string type, but when the value of S1 changes, for example, S1 = "Goodbye"; S2 is still "Hello". This is because the value of the change S1 is created, and the new String object is created. S1 references the new String object; S2 still references the original String object. The cause of this behavior is that the String object is constant, that is, once a String object is created, its value can no longer modify, so when you change a string variable, it is just a new creation. Contains a new String object that modifies the content.

Side sequence and word string (Verbatim strings)

Some characters are not included in the variable in a usual manner when declaring a string variable. In order to solve this problem, C # provides two different methods.

The first method is to use the 'escape sequence'. For example, we want to get the following strings

Hello World

How are you

We can declare strings using the following statement: string a = "/" Hello World / NHow Are You / "". This statement is used "and the escape sequence of the newline. More characters' escape sequences can be found in the table below:

Character

Escape sequence

'

/ '

"

/ "

/

//

alarm

/ a

Retreatment

/ B

Change the page

/ f

Wrap

/ N

Enter

/ r

Tab

/ t

Vertical Tab

/ v

Unicode characters specified by numbers, such as / U2000

/ u

Unicode characters specified using hexadecimal, such as / xc8

/ x

Null value

/ 0 (ZERO

The second method is to use the 'check string' text. This method will be placed between @ "and". If we need to give C: / my documents / assignment to 'Path', we can use the escape sequence method: string path = "c: // my documents //" can also use the following statement: string path = @ " C: / myDocuments / ".

By using the string obtained by using the latter method can also span multiple rows without need to use '/ n'. The only string that needs to use the escape sequence is "" "(two tended double quotes). For example, I want to assign the THE WORD" Contains Three Letters. Text ', we can use the following statement: string text = @ "The word" "BIG" "Contains Three Letters." Packing

C # Allows you to convert any value type to a corresponding reference type, or convert the obtained reference type to a value type. The following code demonstrates this conversion - packing:

INT i = 123; Object Box = I; if (Box is int) {Console.write ("Box Contains An Int");} // This line will be printed.

When the code of the second line is executed, the object 'Box' will be initialized, and the value saved will be copied to this object. It is worth noting that the type of interest is the type of Box runtime will be the value of the box (Int in this case). The 'IS' operator will return the type of Box to int.

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

New Post(0)