Document directory
- C ++ Basic Data Type
- C # Basic Data Types
1. Type SYSTEM INTRODUCTION
When we understand transactions, we always like to divide the categories first, and then study each type of features. In programming languages, we also classify all the basic content into different categories.
The first thing you think of is the basic data types such as int and long, and then the various types of custom (class ). in this way, there are many advantages in different categories. first, you can allocate memory blocks of different sizes in the memory for different types of data, and second, you can reduce errors. different types of data have different features and can be operated differently. for example, the function parameter type will be checked. If the type does not match during the compilation period (static type) or Runtime (dynamic type), an error will be reported. or the combination of the two. in addition, it can also serve as a comment. The basic type may not be obvious, but the custom type is obvious, different types of names and function names can be used to understand some logical meanings.
Different types are the abstraction and encapsulation of the underlying 01 bytes. In the machine layer, there is no concept of any types, all of which are 0101.
Strong type (weak type), static type (dynamic type)
It is estimated that many people are confused by these concepts. I am also confused.
The first thing to understand is that these four concepts are not particularly specialized and specific terms. then, we do not strictly follow a certain standard. the implementation of various programming languages may also use these concepts together. therefore, it is not necessarily accurate to say that a language belongs to a strong or static type.
Strong and weak types:
The so-called strong type strictly means that after a variable type is defined, it can only be of this type and cannot be changed to another type. The opposite is the weak type. for example, if your ID card number is set, you cannot change it. in the strictest sense, C ++ and C # cannot calculate strong languages. but we also say they are. this term is used in fuzzy ways. values in C ++ and C # can be converted randomly (some of them are implicit and sometimes explicitly converted ). of course, most of the time we only consider it as a weak type from the perspective of implicit conversion if any type can be implicitly converted. for example, a = "3"; B = 2 + A; // If B can obtain result 5, this type is generally considered to be weak.
Static and Dynamic types
Static type refers to checking the type during compilation, so each variable must be specified with a type. for example, int num. The dynamic type is not checked during compilation and is determined only during running. for example, you can use num = 9;
Num = "99 ". it is easy to see that the advantage of static types is that you can perform some type checks during compilation, and the restrictions can be optimized according to the type information. however, the dynamic type does not see the type information during compilation, and naturally does not perform type checks and optimizations.
2. Basic Data Types
C ++ Basic Data Type
Variable naming rules
The C ++ variable name can contain letters, underscores, $, and numbers. However, the first character cannot be a number.
C # variable names can contain letters and underscores,@,Number. But the first character cannot be a number. If you use @ to appear in the variable name, it can only start at the beginning. It can only appear once.
C ++ and C # variable names are case-sensitive, while VB is case-insensitive.
**************************************** **************************************** **********************
Class bytes
Bool 1
Char 1
Wchar_t 2 (wide character type, storing Unicode code value. Usage wchart_t letter = L 'A ')
Short 2
Unsigned short 2 (unsigned indicates unsigned. It can only take non-negative numbers. Unsigned short num = 123u; // The u after the number can be added or not added)
Int 4 (the default integer is int, and the long type must be followed by L, for example, long lnumber = 123l; // l can be added or not)
Unsigned int 4 (abbreviated as unsigned)
Long 4 (it seems that different compilers are different, sometimes 8, and sometimes not sure)
_ Int64 8 (not standard, vs extended)
Unsigned long 4
Float 4
Double 8
Long Double 8 (in some cases, it seems to be 12, not standard, and expanded by)
**************************************** **************************************** **********************
C # Basic Data Types
**************************************** **************************************** **********************
Class bytes
Bool 1
Char 2 (equivalent to wchar_t in C ++)
Short 2
Ushort 2
Int 4 (the default integer is int, and the long type must be followed by L, for example, long lnumber = 123l)
Uint 4
Long 8
Ulong 8
Float 4
Double 8 (float type is double by default, declare float type to add F, such as float fnumber = 12.3f)
Byte 1 (only non-negative values are allowed)
Sbyte 1 (negative number can be obtained)
Decimal 16
**************************************** **************************************** **********************
Detailed explanation
Boolean Type
The boolean type in C ++ is actually an integer.. Other integers can also be used as bool values. However, the conversion between them is special.
True to 1 and false to 0.
Convert all non-0 Integers to bool is true. Convert all 0 to false
Therefore, if (true) And if (123), if (false) are equivalent to If (0.
Bool num= 123;
Int NO = num + 1; // The result is 2, because num is first converted to 1
Since other numbers can be converted to bool values, it is easy to make a mistake to convert a statement into a value assignment statement.
For example, int num = 123;
If (num = 100) // you want to write it as num = 100. the result is written incorrectly into a value assignment statement. no error occurs. this statement is equivalent to If (true ). although there is no error at this time, the meaning is completely different.
C # makes some improvements to avoid such errors. The bool type is no longer an integer and cannot be operated or converted with other integers.
Therefore, in C #, the statement int num = 123; If (num = 100) will no longer exist and will be treated as a syntax error.
Of course, if this happens, bool check = false; If (check = true) // No Way, of course, the best way is to directly judge a condition when a value is a Boolean value, without comparing it with true or false. write it as if (check.
Character Type
C # is equivalent to removing the char type in C ++. Only the wchar_t type is retained, and the char name is used in C.
Integer
First, C # simplifies unsigned in C ++ to U, which is much easier to write. ushort is equivalent to unsigned short.
Second, there is an extra byte in C #, which occupies one byte. In fact, this can correspond to char in C ++, Which is exactly a byte and is actually an integer.
In addition, the long type in C # occupies 8 bytes, and the long type in C ++ seems to be different depending on the compiler, sometimes 4 bytes sometimes 8
Floating Point Type
C # And C ++ float types have float and double types, but there is a small difference in writing.
In an integer, the C #, C ++ values are unsigned, or long. The suffix U and l after the number can be written or not written.
But in the C # Float Type, float num = 123.12f; // The suffix F must be added to the float value assignment. Of course, the lowercase F is the same.
Double Eno = 123d; // suffix D can be written or not written
Float and double in C ++ have no suffixes.
In C #, all the suffixes that focus on the right value (Literal Value) should be written. I think it is because C # has a type inference feature.. The variable can be named like this: var somevalue = 123f;
The keyword VaR can replace any variable type, but C # is not like JavaScript, but it is strongly typed. therefore, you must determine the specific type during compilation. Therefore, you must use the right value to deduce the specific type of the variable.
In this case, the suffix is very useful, because 123d, 123f, and 123 are three different types, representing the double, float, and INT types respectively.
C # determines the number without a suffix by default. If there is a decimal number, for example, 123.11 is treated as a double, and the integer 124 is int by default.
In a value such as float num = 123.12f; // if the value is set to 123.12, an error occurs. Because 123.12 is regarded as double by default, and double cannot be implicitly converted to float.
Long Lo = 123; // The result is that integer 123 can be implicitly converted to long
Float FL = 123; // integer 123 can be implicitly converted to float
In addition, C # has a floating point type, decimal, which occupies 16 bytes, indicating that decimals can be precise. It is mainly used in the financial field to calculate numbers related to money. because a few decimal places are the silver of white flowers, it is accurate. when the decimal variable is defined as decimal money = 123.12 m; // Add the suffix M. you can also omit
However, C ++ also has a long double. Some compilers contain 8 bytes, some are 12, and the accuracy is still not as high as decimal.
3. Value Overflow
In C ++, if the value of the assignment overflows, no error is reported, but an error value is returned. for example, short SH = 65534; // This value is out of the short range, but there is no error, but the value obtained by SH is-2.
In C #, if the value short SH = 65534 is assigned in this way, // The compilation will not pass and will be processed as an error.
However, during type conversion, C # performs some special processing on value overflow.
By default, C # is the same as C ++. No error is reported for the overflow value and an error result is returned.
For example, int num = 65534;
Short SH = (short) num; // The value of SH is-2;
However, if you want to perform an overflow check, you must add the keyword checked. The usage is very simple. Write the keyword checked and add a bracket. All the code in the brackets must check for overflow during type conversion.
Checked
{
Int num = 65534;
Short SH = (short) num;
}
In this case, there will be no errors during compilation, but there will be an overflowexception exception during runtime. If your code uses checked for overflow checks, it is best to use exception handling like try catch to catch the exception.
In addition, if you want to add the unchecked keyword if you do not want to overflow check in some of the Code included in the checked, then enclose the brackets in that part of the code.