The overview of ecma c # language specialisation (2)

zhaozj2021-02-08  233

8.2 Type C # Support two types: value and reference. Value types include simple types such as CHAR, INT, and FLOAT, enumerations, and structures. References include classes, interfaces, proxy and matrices. Value and references differ in the value of the value directly contains its data, and referenced variables are just references to the object. Use references to make two variables simultaneously reference an object, so that the operation of a variable will affect another variable that references the same object. But each value has its own data copy, which does not affect each other between variables.

Using system; class class1 {public int value = 0;} Class test {static void main () {int val1 = 0; int val2 = val1; val2 = 123; Class1 Ref1 = new class1 (); class1 ref2 = ref1; REF2 .Value = 123; console.writeline ("VALUES: {0}, {1}", VAL1, VAL2); Console.Writeline ("Refs: {0}, {1}", ref1.value, ref2.value) }}

Output:

VALUES: 0, 123REFS: 123, 123

Because Val1 and Val2 are valued variables (int types), each variable has its own storage space, so the assignment of the local variable VAL1 does not affect the value of VAL2. Conversely, assignment statements ref2.Value = 123; two references affect REF1 and REF2. These two lines

Console.writeline ("Values: {0}, {1}", VAL1, VAL2); Console.writeline ("REFS: {0}, {1}", ref1.value, ref2.value;

Display some behaviors formatted by Console.Writeline string, which uses variables as parameters. The first parameter is a string that can have multiple placeholders such as {0} and {1}. Each placeholder points to a variable, {0} points to the second parameter, {1} points to the third: each placeholder is replaced by the corresponding parameters. There will be further explanation later.

Developers can define new value types with enumerations and structures, or declare new types with reference types such as classes, interfaces, and proxy.

Using system; public enum color {red, blue, green} public struct point {public int x, y;} public interface ibase {void f ();} public interface idherive: ibase {void g ();} public class a { Protected Virtual Void H () {Console.Writeline ("ah");}} public class b: a, idherd {public void f () {console.writeline ("bf, importation of iderived.f");} public void G () {Console.writeline ("BG, Implement OF IDERIVED.G");} Override Protected Void H () {Console.WriteLine ("BH, OVERRIDE OF AH");}}}} public delegate void EmptyDelegate ();

The above is an example of several statements. The type of statement will be described in detail in the section.

8.2.1 Predefined Types

C # provides a set of predefined types, most of which are very kind to C / C developers. The predefined types include symbols and unsigned integers, floating point, Boolean, characters, and decimal numbers. The number of symbols is Sbyte, Short, Int and long; no symbols are byte, ushort, uint, and ulong; floating points are float and double. Bool type representation: The value can only be True or False. This makes writing and self-building, and avoiding incorrectly using "==" when using "==".

INT i = ...; f (i); if (i = 0) // bug: The test shouth be (i == 0) g ();

The above code triggers a compilation error because i = 0 results are int types, and if the IF requires a BOOL result. The char type represents the Unicode character, and a char variable represents a 16-bit Unicode character. The decimal number is suitable for a variety of calculations, and the carry errors that fill the floating point number and the unacceptable representation. Common examples are financial aspects of calculations, such as the calculation of the tax rate and the conversion of currency. The decimal number is a 28-bit number. All predefined types and examples are listed below:

Type Description Example Object O = NULL; String Unicode String Type String S = "Hello"; Sbyte 8 A Symbol Byte Sbyte Val = 12; Short 16 is Symbol Short Val = 12; int 32 Based on symbol integer int val = 12; long 64 is a symbol long set long Val1 = 12; Long Val2 = 34L; Byte 8-bit unsigned Byte Byte Val1 = 12; ushort 16-bit unsigned short USHORT VAL1 = 12 UINT 32-bit unsigned integer uint val1 = 12; uint val2 = 34u; Ulong 64 is unsigned long ULONG VAL1 = 12; Ulong Val2 = 34u; ulong val3 = 56L; ulong val4 = 78ul; float has symbolic single Precision floating point number float val = 1.23F; Double Double Val1 = 1.23; Double Val2 = 4.56D; Bool Boolean type; only True or false bool val1 = true; Bool Val2 = false; char Characters A Unicode Character CHAR VAL = 'H'; Decimal 28 Accurate Decimal Val = 1.23M; Both predefined types are simplified representation of a system. For example, int is a thumbnail form of the System.Int32 structure, and the former will be more easily used.

Predefined types, such as int, except for a small part of special use, most of which are in general. Operator overload can define a new type of structure similar to predefined types. For example, an named DIGIT structure can provide and predefined digital types of mathematical operations, and can define a rule that mutually conversion. THE PREDEFINED TYPARITORS == AND! = Have Different Semantics For Different Predefined

The predefined type itself also uses overload. For example, comparison operators == and! = Have different semantics together with different predefined characters:

· Two expressions of type int are considered equal if they represent the same integer value. · Two expressions of type object are considered equal if both refer to the same object, or if both are null. · Two expressions of type string are considered equal if The String Instances Have Identical Length CHARACTER POSITION, OR IF Both Are Null. • Two int types use their values ​​to determine whether they are equal.

• Two Object check whether the objects they reference are determined to be equal.

• Two string use their length and whether the elements on each position is the same as equal or equal, or both are NULL.

Using system; class test {static void main () {string s = "test"; string t = string.copy (s); console.writeline (s == T); console.writeline ((Object) s == ( Object) T);}}

Output:

Truefalse

The first comparison is a String type, and the second comparison is the object type.

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

New Post(0)