C ා basic knowledge series – 1 data type


Common data types

The types of C ාValue typereference typeThere are two types.
Instances of value types are stored in the stack, and reference types place a pointer to a piece of content in the heap.
C ා has built in several data types for us to use:

Key words abbreviation Full name of the corresponding class (click to view the corresponding API) Value range explain
bool System.Boolean true、false There are only two values of this type, which are used to judge and indicate "yes" and "no"
sbyte System.SByte -128 ~ 127 (-27~27-1) This type is stored in memory with 8 bits and is signed.<br/>According to the highest bit as the sign bit, the actual range of sbyte is - 128 ~ 127
byte System.Byte 0~28-1 8-bit unsigned bit
short System.Int16 -215~215-1 Short is a 16 bit short integer, and its specific values are - 32768-32767
ushort System.UInt16 0~216-1 USHORT is an unsigned 16 bit short integer in the range of 0-65535
int System.Int32 -215~215-1 Int is a common data type, its data range is - 2147483648 ~ 2147483647.<br/>As can be seen, it is - 200 million to 200 million, basically meeting the data needs.
uint System.UInt32 0~232-1 Uint is an unsigned integer. The maximum value is about twice that of int, but there is no negative number.<br/>If there is no negative value in the calculation, it can be used.<br/>It is not recommended because it is easier to overflow when subtracting
long System.Int64 -216~216-1 The actual values are - 9223372036854775808 ~ 9223372036854775807.<br/>Long takes more bytes in memory than int and is twice as long as int.<br/>So it can express more dramas. In database, it is often used as the primary key of big data table.
ulong System.UInt64 0~264-1 In other integer, long is longer than the other integer.
char System.Char utf-16 In. Net, char represents the encoding unit of utf-16, so most characters can be represented by char, including Chinese characters.
float System.Single -3.402823e38~3.402823e38 A 32-bit single precision floating-point type, commonly referred to as a number with a decimal point
double System.Double 64 bit double precision floating-point type, which can represent more decimal places than float.<br/>The actual value is -1.79769313486232e308 ~ 1.79769313486232e308
decimal System.Decimal 128 bit high precision floating point value.<br/>It is often used in places where rounding is not possible or where it is sensitive to the requirements of the number after the decimal point.<br/>In 128 bits, one bit is the sign bit (indicating positive and negative), 96 bits are the value itself (n), and 8 bits are the scaling factor (k).<br/>So the actual value of decimal should be ±Nx10kWhere - 28 ≤ K ≤ 0
string System.String A string variable that represents an array of characters. String is not variable. That is, the string cannot be modified. Any modification to the string will generate a new string.

There are two other special types:dynamic object
Dynamic represents dynamic type, which is supported by C ා since 4.0,dynamicKeyword to declare that the variable name is a dynamic variable. For specific use, refer to python, JS and other dynamic languages. howeverdynamicDeclared variables do not support adding attributes, but this is not absolute. You can refer to the following dynamic articles to introduce this part.

object The parent class of all types, C ා all classes are subclasses of object. However, the direct parent class described in the above table is ValueType (representing value type), but the parent class of ValueType is still object.

For double and float, there is a problem because of the way of storing data: the value of 0 or near 0 cannot be well expressed. Because floating-point variables are represented in memory as 1 / 2nSo there are two values of + 0 and – 0. When a floating-point judge is equal to 0, it can be judged normally. But when it comes to comparing the results of mathematical operations with 0, there will be problems, such as0.1+0.2 != 0.3This judgment istrue。 So the standard way of judging should beMath.Abs(0.1+0.2 - 0.3)< ?In this way, where? Represents the error range accepted by the system.
The accuracy of decimal is much higher than that of double and float. At least 0.1 + 0.2! = 0.3 will not occur. So decimal is usually used to calculate the amount of money.

Type conversion

Type conversion refers to the conversion of the value of one type to another by some means.
There are two ways of type conversionDefault type conversionCast

Default type conversion

The default type conversion is triggered in the following situations:

  • When a subclass wants to convert to its parent class,
  • When short precision to high precision conversion.
    For example, byte, int, long, float, double


int i = 1;
double d = i;
float f = i;
d = f;
uint ui = 1;
long l = ui;
d = l;
f = l;

Here are a few things to pay special attention to:

  • If the number of signed digits is the same as that between signed bits, the conversion cannot be default.
  • All integers of decimal can be converted todecimal, i.e. divisiondoublefloatAll digital types are OK.


Data type judgmentis

C ා has a built-in keyword to determine whether a variable is of a certain type

class A
class B : A
class C : A
class Program
    static void Main()
        B b = new B();
        A a = b;
        //In this case, a is a reference to B pretending to be a
        Console.WriteLine ("a is B? {0}", a is b); // result: true
        Console.WriteLine("a is A ? {0}", a is A); // true
        Console.WriteLine("a is C ? {0}", a is C); // false

thatisWhat’s the use? Before casting, check whether it can be converted to the target type. If the return value is false and still needs to be converted, an error will be reported.


There are two types of casts:

  1. This method is the same as C / C + + and Java. The specific method is as follows:

    C C1 = (c) a; // code continued
  2. useaskeyword,asIndicates that the variable x is treated as type y

    C C2 = a as C; // code continues

The difference between the two ways:

  1. When the transformation fails, an error is thrown to terminate the code directly
  2. Set C2 to null when the transformation fails. The current step does not throw an exception. If the null reference is not handled, thefollow-upThrow an exception with a null reference in.

Type conversion of value types

In the previous content, we mentioned that small precision to large precision can be converted by default. However, in the process of normal use, we will encounter various situations of conversion, then we will use the cast at this time, and the loss of precision is acceptable for us.
Common conversion methods include:

  1. Use type coercion conversion example: long lval = 100; int i = (int)lval;
  2. use System.Convert Class. useConvert.ToXXX()In whichXXXRepresents the target object of transformation.
//Convert sample code
long lval = 19293;
var i = Convert.ToInt32(lval);
double d = 10.091;
var dc = Convert.ToDecimal(d);
var dt = Convert.ToDateTime("2019-03-30");

It is worth noting that:ConvertWhere XXX uses the C ා type name instead of a keyword.ConvertIs a very useful class, in our development work will be a large number of use of this class for value type conversion.
Of course, we will give a more in-depth introduction to it later.

Please pay attention to my blog for more information

C ා basic knowledge series - 1 data type

Recommended Today

Regular expression sharing for checking primes

This regular expression is shown as follows: Regular expressions for checking prime numbers or not To use this positive regular expression, you need to convert the natural number into multiple 1 strings. For example, 2 should be written as “11”, 3 should be written as “111”, 17 should be written as “11111111111”. This kind of […]