Before you experience the sharpness of C #, the mastery of the basics of language is an essential part. Since many of the C # Basic languages originate from C + +, where only a brief introduction is made of those similar to C + +, we will experience focusing on the key language basics that are different from traditional C + +.
Data type
The data types of the C # language are divided into two main categories: value types and reference types. Another data type "pointer" is set specifically for unsafe context programming, where the unsafe context refers to C # unmanaged code that unsafe the code to meet the direct operational requirements of the pointer to memory. This code will lose the CLR nature of the garbage collection of the Microsoft.NET platform, as we put it in the "COM Interop unmanaged programming and Exception handling" topic. The variables of a value type contain their data, and a variable of a reference type contains a reference or a handle to the memory block that contains the data. The difference between the two can be clearly seen from the following picture:
A possible problem with reference types is that when multiple variables refer to the same block of memory, modifications to any one of the reference variables cause the value of that object to change. A null value indicates that the reference type does not refer to any actual address.
Value types can be divided into struct types and enumeration types. struct types include simple types and user-defined structure types. Enumeration types and user-defined struct types we will elaborate on the topic "Nineth lecture structure, enumeration, array and string". Simple types can be divided into Boolean and numeric types. Boolean types in the C # language are strictly distinguished from numeric types, with only true and false two values, and no conversions between other types, as in C + +. Numeric types include integer values, floating-point and decimal three types. The integer value type has a total of nine Sbyte,byte,short,ushort,int,uint,long,ulong,char. In addition to the char type, the other 8 kinds of 221 groups are signed and unsigned respectively. Floating point values have float and double two species. Decimal is mainly used in financial, monetary and other computing environments with higher accuracy requirements. The following table is a detailed description of these simple types:
Simple Type |
Description |
Example |
SByte |
8-bit signed integer |
SByte val = 12; |
Short |
16-bit signed integer |
Short val = 12; |
Int |
32-bit signed integer |
int val = 12; |
Long |
64-bit signed integer |
Long val1 = 12; Long val2 = 34L; |
Byte |
8-bit unsigned integer |
byte val1 = 12; byte val2 = 34U; |
UShort |
16-bit unsigned integer |
ushort Val1 = 12; ushort Val2 = 34U; |
UInt |
32-bit unsigned integer |
UINT VAL1 = 12; UINT VAL2 = 34U; |
ULong |
64-bit unsigned integer |
ULONG Val1 = 12; ULONG val2 = 34U; ULONG Val3 = 56L; ULONG Val4 = 78UL; |
Float |
32-bit single-precision floating-point numbers |
float val = 1.23F; |
Double |
64-bit double-precision floating point numbers |
Double val1 = 1.23; Double val2 = 4.56D; |
L |
Boolean type |
bool Val1 = true; BOOL Val2 = false; |
Char |
Character type, Unicode encoding |
Char val = ' h '; |
Decimal |
28 128-bit Decimal types for valid digits |
Decimal val = 1.23M; |
Reference types are divided into four types: classes, interfaces, arrays, delegates. Class except that we can define our own types, we include two more specific types of object and string. Object is the inherited root class for all types in C #, including all value types and reference types. The string type is a sealed type (cannot be inherited), its instance represents a Unicode string, and the array type we will put in the Nineth lecture structure, enumerations, arrays, and strings. The interface type defines a contract for a method that we will describe in the seventh interface inheritance and polymorphism. A delegate type is a signature that points to a static or instance method, similar to a function pointer in C + +, which is described in the eighth delegate and event. In fact, we'll see from the later topics that these types are some form of wrapper for the class.
Each data type has a corresponding default value. The default value for a numeric type is 0 or 0.0, where char defaults to ' \x0000 '. The default value for a Boolean type is false. The default value for the enumeration type is 0. The default value for a struct type is to set the field of all its value types to the default value of the corresponding value type, and to set the field of its reference type to NULL. The default value for all reference types is null.
Different types of data can be converted between, C # type conversion has implied conversion, clear conversion, standard conversion, custom conversion A total of four ways. Implicit conversions, like explicit conversions and C + +, data from the "small type" to "large type" conversion for implicit conversion, from the "large type" to "small type" conversion to a clear conversion, a clear conversion requires such as "(type) data" general bracket conversion operator. Standard conversions and custom transformations are for both system-built and user-defined transformations, both for custom types such as classes or structs.