C # Data types

Source: Internet
Author: User

C # Data types can be divided into 3 categories: numeric types, reference types, pointer types. Pointer types are only used in unsafe code.
Value types include simple types (such as character, float, and Integer), collection type, and structural type. Reference types include class types, interface types, and representative types and array types.

The difference between value types and reference types is that variable values of value types directly contain data, whereas variables of reference types store their references in objects. For a variable of a reference type, it is entirely possible to have two different variables referencing the same object, so that the manipulation of one of the variables affects the object that is referenced by another variable. For variables of value types, each variable has its own numeric value, so the operation on one of the variables cannot affect the other variable.
1 Value types
All value types implicitly declare a common parameterless constructor, which is called the default constructor. The default constructor returns an instance of a value type that is initially zero, which is called the default value.
For Sbyte,byte,short,ushort,int,uint,long,ulong, the default value is 0.
For char, the default value is ' \x0000 '
For float, the default value is 0. 0F
For double, the default value is 0. 0D
For decimal, the default value is 0. 0M
for bool, the default value is False
For an enumeration type, the default value is 0
For a struct type, the default setting is to set the fields of all value types to their respective default values, assigning all fields of the reference type to be empty
1.1 Simple types
C # provides a set of predefined struct types called simple types. Simple types are defined with reserved words, which are merely aliases of the predefined struct types in the System namespace. For example, int is a reserved word, System. Int32 is a predefined type in the System namespace. A simple type is exactly the same as the struct type of its pseudonym, that is, write int and write system. Int32 is the same. Simple types are mainly integer, floating-point type, Decimal type, Boolean type, character type
1.1.1 Integral type
9 integer types are supported in C #: Sbyte,byte,short,ushort,int,uint,long,ulong and Char.
Sbyte: Represents a signed 8-bit integer value ranging from 128 to 127
Byte: Represents an unsigned 8-bit integer with a range of values from 0~255
Short: Represents a signed 16-bit integer range from 32768 to 32767
UShort: Represents a signed 16-bit integer range from 32768 to 32767
Int: Represents a signed 32-bit integer ranging from 2147483648 to 2147483648
UINT: Represents unsigned 32-bit integers ranging from 0 to 4294967295
Long: Represents a signed 64-bit integer range from 9223372036854775808 to 9223372036854775808
Ulong: Represents unsigned 64-bit integers ranging from 0 to 18446744073709551615.
Char: Represents an unsigned 16-bit integer with a range of values from 0~65535.
The possible values for the char type correspond to the Uniform character encoding standard (Unicode) character set.
The char type differs from the other integer types in the following two points:
A, there is no implicit conversion of other types to char types. The implicit conversion of sbyte,byte and ushort to char does not exist even for types such as Sbyte,byte and ushort that can use the char type entirely to represent its value.
Constants of type B,char must be written as characters, and if they are in integer form, they must be prefixed with a type conversion. For example (char) 10 in the form of an assignment of three:
Char chsomechar= "A";
Char chsomechar= "\x0065"; Hexadecimal
Char chsomechar= "\u0065; Unicode notation
The following escape characters are in the character type:
1,\ ' is used to denote single quotes
2,\ "is used to denote double quotation marks
3,\\ is used to indicate a backslash
4,% = null character
5, \a used to denote exclamation marks
6, \b used to indicate backspace
7, \f used to represent a page change
8, \ n is used to indicate line break
9, \ r is used to indicate carriage return
\ t is used to indicate horizontal tab
\v is used to denote vertical tab
1.1.2 Floating-point types
C # supports two floating-point types: float and double.
The range of values that can be represented by the float type can be approximately from 1.5*10-45~3.4* 10 38 to 7 digits after the decimal point.
The range of values that can be represented by the double type can be approximately from 5.0*10-324~1.7* 10 308 to 15 or 16 digits after the decimal point.
If one of the operands in the two-dollar operation is a floating-point type, the other operand is an integer or floating-point type, with the following rule:
A, if one of the operands is an integral type, the operand is converted to the floating-point number type of the other operand;
b, if one of the operands is double, the other operand is also converted to a double type, the operation is made of the precision and range of the double type, and the resulting result is also a double type;
C, otherwise, the operation will be at least the value range and precision of the float type, and the result is also float type.
1.1.3 Decimal (decimal) type
Decimal types are well suited for financial and monetary operations. The values range from 1.0*10-28~7.9* 10 28 to 28 digits after the decimal point. If one of the operands in the two-dollar operation is a decimal type, the other one from the operand is an integer or decimal type. The integer is converted to the number of decimal types before the operation. If the arithmetic operation of a decimal type produces a value that is too small for the format of the decimal type, the result of the operation becomes 0. An overflow error is triggered if the arithmetic operation of a decimal type produces a value that is too large for the format of the decimal type. Decimal types are more accurate than floating-point types, but the range of values is relatively small. Converting a number of floating-point types to a number of decimal types results in overflow errors, which translates into a loss of precision when converting a number of decimal types to a number of floating-point types. Therefore, there is no implicit or explicit conversion of the two types. Boolean: The value is true or false. There is no standard to implement Boolean types and other types of conversions.
1.2 Enum types
An element of an enumeration type can only use a type of long,int,short,byte. The default type is int. The value of the default first element is 0, and each successive element is incremented by 1. You can assign values directly to an element. Such as:

[CSharp]View PlainCopyprint?
    1. Enum MonthNames
    2. {
    3. January=1,
    4. February,
    5. March=31
    6. };
    7. You can force other types to be defined, such as:
    8. Enum MonthNames: byte
    9. {January,
    10. February,
    11. March
    12. };
Enum MonthNames {january=1,february, march=31}; You can force other types to be defined, such as: Enum Monthnames:byte{january, February,march};

1.3 Structure Type
A struct type is also a value type that is used to create small objects that save memory. The following example represents an IP address that uses a byte type of 4 fields.

[CSharp]View PlainCopyprint?
  1. Using System;
  2. struct IP //Declaration structure
  3. {
  4. Public byte B1,b2,b3,b4;
  5. }
  6. Class Test
  7. {
  8. public static void Main ()
  9. {
  10. IP MyIP;
  11. myip.b1=192;
  12. myip.b2=168;
  13. Myip.b3=1;
  14. myip.b4=101;
  15. Console.Write ("{0}.{ 1}.  ", MYIP.B1, MYIP.B2);
  16. Console.Write ("{0}.{  1} ", myip.b3, myip.b4);
  17. }
  18. }
Using System; struct IP//declaration structure {public byte b1,b2,b3,b4;} Class test{public  static void Main ()  {    IP MyIP;    myip.b1=192;    myip.b2=168;    Myip.b3=1;    myip.b4=101;    Console.Write ("{0}.{ 1}. ", MYIP.B1, MYIP.B2);    Console.Write ("{0}.{ 1} ", myip.b3, MYIP.B4);}}

2 reference types
Reference types include class types, interface types, and representative types and array types.
2.1 Class Type
A class type defines a data structure that consists of members (such as constants, fields, and events), function members (such as methods, properties, indexes, operations, constructors, destructors, and so on) and nested types. Support inheritance.
2.2 Object Types
The object type is the final underlying type for all other types. Each type in C # is directly or indirectly derived from the class type of object.
2.3 String Type
A string type is a sealed class that inherits directly from object. The value of string type can be written in the form of a string literal.
2.4 Interface Type
An interface declares a reference type with only an abstract member, and the interface only has a method flag, but no code is executed. When a class is defined, a class can derive from a multi-interface if it derives from the interface, but if the class derives from a class, it is derived from only one class.
Declare the method as an example:

[HTML]View PlainCopy print?
    1. Interface Iface
    2. {
    3. void Showmyface ();
    4. }
Interface Iface{void showmyface ();}

2.5 delegate Type
Represents a static method or an instance of an object that references an instance method of the object. It is close to pointers in C/s + +, but pointers can only access static functions, representing methods that can access both static and instance methods.
2.6 Arrays
An array is a data structure that contains a series of variables. Array variables are also called array elements, and they have the same type, which is also known as the array element type. The element type of an array can be any type, including the array type. The array uses subscripts to determine the index number of each element. Only an array of subscripts is called a one-dimensional array, and an array of more than one subscript is called
Multidimensional arrays.
Example: int[] a={0,2,4,6,8}; Equivalent to int[] a=new int[] {0,2,4,6,8};
This can also be initialized: a[0]=0; a[1]=2; a[2]=4; a[3]=6; a[4]=8;
Int[] A; One-dimensional array of type int
Int[,] A; Two-dimensional arrays of type int
Int[,,] A; Three-dimensional array of type int
Int[] []a; An array of arrays of type int
Int[][][]a; An array of arrays of type int
The length of each dimension of the array is not part of the array type, and the length of the dimension is specified in the array creation statement, not in the array type
Specified, for example:
Int[,,] a3=new int[10,20,30];
A3 is an array variable, int[, [] without specifying the length of the array, the array creation statement new INT[10,20,30] is specified.
The following example creates an array of arrays:
Int[][] J=new int[3][];
J[0]=new int[] {a-i};
J[1]=new int[] {1,2,3,4,5,6};
J[2]=new int[] {1,2,3,4,5,6,7,8,9};

C # Data types

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.