Common Data Types
C # type is generally divided into value types , reference types are two types.
Examples of the type of the value stored in the stack, reference type places a pointer to a stack in a stack contents.
C # is that we built for our use several types of data:
Keywords shorthand | Corresponding class full name (Click to view the corresponding API) | Value range | Explanation |
---|---|---|---|
bool | System.Boolean |
true、false | 该类型只有两个值,用作判断,表示“是”、“否” |
sbyte | System.SByte |
-128 ~ 127 (-27~27-1) | 该类型在内存中使用8个bit进行存储,而且带有符号。 根据最高位作符号位,所以sbyte实际表示范围为 -128~127 |
byte | System.Byte |
0~28-1 | 8位的无符号bit |
short | System.Int16 |
-215~215-1 | short表示一个16位的短整形,其具体的值为-32768~32767 |
ushort | System.UInt16 |
0~216-1 | ushort表示无符号16位的短整型,具体的范围为0~65535 |
int | System.Int32 |
-215~215-1 | int是我们常用的一个数据类型,它的数据范围为: -2,147,483,648~ 2,147,483,647 。 可以看到,是-2亿到2亿,基本满足了数据需要。 |
uint | System.UInt32 |
0~232-1 | uint 无符号整形,最大值比 int大一倍左右,但是没有负数。 如果在计算中能保证没有负值,可以使用。 并不推荐,因为在做减法的时候,更、容易溢出 |
long | System.Int64 |
-216~216-1 | 实际取值为-9,223,372,036,854,775,808~ 9,223,372,036,854,775,807 。 long在内存中比int占用更多字节,长度为int的两倍。 所以能表达更多的数剧。在数据库中,经常被用来做大数据表的主键。 |
ulong | System.UInt64 |
0~264-1 | 64位无符号长整形,理同其他的无符号整形,在正整数中比long表达更多的数据。 |
char | System.Char |
utf-16 | 在.net 中char 表示 utf-16的编码单位,所以绝大多数的字符都可以用char表示,包括中文汉字。 |
float | System.Single |
-3.402823e38 ~ 3.402823e38 | 32位的单精度浮点型,也就是通常说的带小数点的数 |
double | System.Double |
64位双精度浮点型,比float能表示更多的小数位。 实际取值-1.79769313486232e308~1.79769313486232e308 |
|
decimal | System.Decimal |
128位高精度浮点值。 常用于不能四舍五入,或者对小数点后数字要求很敏感的地方。 在128位中,一位是符号位(表示正负),96位是值本身(N),8位是比例因子(k)。 所以decimal实际值应该是 ±Nx10k,其中-28≤k≤0.其余位暂时没有使用 |
|
string | System.String |
-- | 字符串变量,表示一组字符数组。字符串是不可变量。即字符串不能修改,任何针对字符串的修改都会生成一个新的字符串。 |
The other two special types: dynamic
object
.
Which including dynamic type, which is in C # 4.0 beginning supported dynamic
keyword to declare the variable name is a dynamic variable. Referring to the specific use Python, dynamic language Js like. But the dynamic
variable declaration does not support adding attributes, but this is not entirely absolute, can refer to a subsequent dynamic part of this chapter will be introduced.
object
All types of parent classes, C # classes are all subclasses of the object. However, the direct parent described in the above table is ValueType (a value indicating the type), but is still ValueType parent object.
Note:
for float and double data storage problems because of the way, there is a problem: For values of 0 or near 0 can not be well expressed. Because the float variables represented in memory as a 1/2 n- , so there will be a +0 and -0 two values. When a floating point judgment is not equal to 0 it can be judged normal. But when it comes to the results of mathematical operations is compared to zero, then there will be problems, such as0.1+0.2 != 0.3
the predicate Shitrue
. Therefore, the standard approach should be determinedMath.Abs(0.1+0.2 - 0.3)< ?
in this way, where? Represents an acceptable error range of the system.
The decimal accuracy in this regard would be much higher than the double and float. It does not appear at least 0.1 + 0.2! = 0.3 this problem. So decimal it is generally used in computing the amount of these places.
Type Conversion
It refers to a numeric type conversion type is converted into another type by some means.
Type conversions divided into two ways: the default type conversion , cast .
The default type conversions
It will trigger default type conversion in the following situations:
- When a subclass want to convert its parent,
- When the short conversion accuracy precision.
Such as: byte -> int -> long -> float -> double
reference:
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 places that require special attention:
- The same number of bits between the conversion between unsigned and signed, if the number of bits unsigned and signed by default if not converted.
- All decimal integer can be converted to default
decimal
, that is, exceptdouble
,float
except for all numeric types are possible.
Cast
Analyzing Data Type is
:
C # built a keyword is used to determine whether a variable is a type
class A
{
}
class B : A
{
}
class C : A
{
}
class Program
{
static void Main()
{
B b = new B();
A a = b;
// 这时候 a 是一个 假装自己是A的B的引用
Console.WriteLine("a is B ? {0}", a is B); // 结果: true
Console.WriteLine("a is A ? {0}", a is A); // true
Console.WriteLine("a is C ? {0}", a is C); // false
}
}
So is
what use is it? Prior to cast detect whether it can be converted to the target type, if you want to convert the return value is still false, then it will error.
Cast
Cast divided into two types:
- Before adding the variable target casts, this method with C / C ++, Java consistent manner, in particular:
C c1 = (C)a;// 代码接上
- Use
as
keywordas
represents the variable x as type YC c2 = a as C;// 代码接上
The difference between the two approaches:
-
When the transformation fails will throw an error, direct termination code
-
When the transformation fails to c2 is set to null. The current step does not throw an exception if not treated for a null reference, then will follow thrown empty cited.
Type conversion value type
Before we mention the contents of small to large precision accuracy can be converted by default. But we will meet converted in each case in the course of normal use, then this time we will use the cast, this time for us to loss of precision is acceptable.
Common conversion methods are:
- Example using type declarations to force:
long lval = 100; int i = (int)lval;
- Use System.Convert class. Use
Convert.ToXXX()
, whichXXX
represents the transformation of the target object.
//Convert 示例代码
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: Convert
the ToXXX where XXX is the use of C # type name, instead of keywords. Convert
Is a very useful class, this class will use a lot of type conversion value in our development work.
Of course, we will follow them more in-depth introduction.
View more content in my blog or