C# data types

黄舟
Release: 2017-02-10 15:09:01
Original
1308 people have browsed it

C# data types can be divided into three categories: numerical types, reference types, and pointer types. Pointer types are only used in unsafe code.
Value types include simple types (such as character types, floating point types and integer types, etc.), collection types and structural types. Reference types include class types, interface types, representative types and array types.

The difference between value types and reference types is that variable values of value types directly contain data, while variables of reference types store their references in objects. For reference type variables, it is entirely possible to have two different variables refer to the same object. In this way, the operation on one variable will affect the object referenced by the other variable. For value type variables, each variable has its own value, so operations on one variable cannot affect another variable.
1 Value Type
All value types implicitly declare a public parameterless constructor, which is called the default constructor. The default constructor returns an instance of a value type that is initially zero, 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 structure type, the default value setting is to set the fields of all value types to their respective default values. Assign all fields of reference types to empty
1.1 Simple types
C# provides a set of predefined structural types called simple types. Simple types are defined using reserved words, which are merely pseudonyms for predefined structural 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 its aliased structural type, that is, writing int is the same as writing System. Int32 is the same. Simple types mainly include integers, floating point types, decimal types, Boolean types, and character types
1.1.1 Integer types
C# supports 9 integer types: sbyte, byte, short, ushort, int, uint, long, ulong and char.
Sbyte: represents a signed 8-bit integer, the value range is from -128 to 127
Byte: represents an unsigned 8-bit integer, the value range is from 0 to 255
Short: represents a signed 16-bit number Integer, ranging from -32768 ~ 32767
ushort: represents a signed 16-bit integer, ranging from -32768 ~ 32767
Int: represents a signed 32-bit integer, ranging from -2147483648 ~ 2147483648
uint : Represents an unsigned 32-bit integer, ranging from 0 to 4294967295
Long: Represents a signed 64-bit integer, ranging from -9223372036854775808 ~ 9223372036854775808
Ulong: Represents an unsigned 64-bit integer, ranging from 0 to 18446744073709551615.
char: represents an unsigned 16-bit integer, ranging from 0 to 65535.
Possible values of the Char type correspond to the Unicode character set.
The Char type has the following two differences compared with other integer types:
a, there is no implicit conversion from other types to the char type. Even for types such as sbyte, byte and ushort, which can completely use the char type to represent their values, the implicit conversion of sbyte, byte and ushort to char does not exist.
b, char type constants must be written in character form, and if integer form is used, they must have a type conversion prefix. For example, there are three assignment forms for (char)10:
char chsomechar="A";
char chsomechar="\x0065"; hexadecimal
char chsomechar="\u0065; unicode representation
The character type has the following escape characters:
1, \' is used to represent single quotes
2, \" is used to represent double quotes
3, \\ is used to represent backslash
4. \0 represents the empty character
5, \a is used to represent the exclamation mark
6, \b is used to represent the backspace
7, \f is used to represent the page feed
8, \n is used to represent line feed
9, \r to represent carriage return
10, \t to represent horizontal tab
11, \v to represent vertical tab
1.1.2 Floating point Type
C# supports two floating point types: float and double.
The value range that the Float type can represent can be approximately from 1.5*10 -45 to 3.4* 10 38, accurate to 7 digits after the decimal point.
The value range that the Double type can represent can be approximately from 5.0*10 -324 to 1.7* 10 308, accurate to 15 or 16 digits after the decimal point.
If one of the operands in the binary operation is of floating point type, then the other operand is of integer or floating point type, the operation rules are as follows:
a, if one of the operands is of integer type, then The operand is converted to the floating point type of the other operand;
b. If one of the operands is double, the other operand is also converted to the double type. The operation is performed with the precision and value range of the double type. , and the result obtained is also of type double;
c, otherwise, the operation will be performed at least with the value range and precision of type float, and the result obtained is also of type float.
1.1.3 Decimal type
The decimal type is well suited for financial and monetary operations. The value range is from 1.0*10 -28 to 7.9* 10 28, accurate to 28 digits after the decimal point. If one of the operands in a binary operation is of type decimal, then the other operand is of type integer or decimal. Integers are converted to decimal types before operations. If an arithmetic operation on a decimal type produces a value that is too small for the decimal type's format, the result of the operation will be 0. If an arithmetic operation on a decimal type produces a value that is too large for the format of the decimal type, an overflow error is triggered. The decimal type has greater precision than the floating point type, but the numerical range is relatively smaller. An overflow error will occur when converting a floating-point number to a decimal type, and a loss of accuracy will occur when converting a decimal number to a floating-point number. Therefore, there is no implicit or explicit conversion between the two types. Boolean type: the value is true or false. There is no standard for converting Boolean types to other types.
1.2 Enumeration type
The types used for elements of the enumeration type can only be long, int, short, and byte. The default type is int. The default value of the first element is 0, and each successive element is incremented by 1. You can assign values directly to elements. Such as:

enum monthnames { January=1, February, march=31 }; 可以强制定义其他类型,如: enum monthnames : byte {January , February, March };
Copy after login

1.3 Structural type
The structural type is also a value type. The purpose of using it is to create small objects to save memory. The following example represents an IP address using 4 fields of type byte.

using System; Struct IP //声明结构 { 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); } }
Copy after login

2 Reference type
Reference types include class types, interface types, representative types and array types.
2.1 Class type
The class type defines a data structure. This data structure contains data members (such as constants, fields and events, etc.), function members (such as methods, properties, indexes, operations, constructors and destructors, etc.) and nested types. Inheritance is supported.
2.2 Object type
The object type is the ultimate base type for all other types. Every type in C# is directly or indirectly derived from the object class type.
2.3 String type
The string type is a sealed class directly inherited from object. Values of type String can be written as string literals.
2.4 Interface type
An interface declares a reference type with only abstract members. The interface only has method flags, but no execution code. When defining a class, if the class derives from an interface, it can derive from multiple interfaces; but if the class derives from a class, it can only derive from one class.
Declaring methods, for example:

interface iface { void showmyface(); }
Copy after login

2.5 Representation type
Represents a reference to a static method or object instance, and refers to the instance method of the object. The closest thing to it is the pointer in c/c++, but the pointer can only access static functions, which means it can access both static methods and instance methods.
2.6 Array
An array is a data structure containing a string of variables. Array variables are also called array elements. They have the same type. This type is also called the array element type. The element type of an array can be any type, including array types. Arrays use subscripts to determine the index number of each array element. An array with only one subscript is called a one-dimensional array, and an array with more than one subscript is called a multi-dimensional array.
Example: int[] a={0, 2, 4, 6, 8}; is equivalent to int[] a=new int[] {0, 2, 4, 6, 8};
Also It can be initialized like this: a[0]=0; a[1]=2; a[2]=4; a[3]=6; a[4]=8;
int[] a; //int One-dimensional array of type
int[,] a; //Two-dimensional array of type int
int[,,] a; //Three-dimensional array of type int
int[] []a; / /Array of array of int type
int[][][]a; //Array of array of array of int type
The length of each dimension of the array is not part of the array type, the length of the dimension It is specified in the array creation statement, not in the array type
, for example:
int[,,] a3=new int[10, 20, 30];
a3 is a Array variables, int[,,] do not specify the length of the array, only the array creation statement new int[10, 20, 30] does.
The following example creates an array of arrays:
int[][] J=new int[3][];
J[0]=new int[] {1, 2, 3};
J[1]=new int[] {1, 2, 3, 4, 5, 6};
J[2]=new int[] {1, 2, 3, 4, 5, 6 ,7,8,9};
The above is the content of C# data type. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!