Essential C # [two] data types

This chapter describes the C # language to the basic data types, as well as some of the mode of operation of the string.

Basic data types

C # language basic data types include eight kinds of integer type, and two for binary floating-point type science of computing a decimal floating-point type of financial calculations a Boolean type and one for character types (including combinations the String) .

Integer type

There are several integer types, BCL what is it? In another article I was introduced to the blog, in fact, it is the basic CLR type definition for full platform language. To use your own C # type instead of BCL type! Use string instead of String , which explains the many years of confusion, why there are C # String and string, in fact, is the same keyword, but it is recommended to use C # own keywords.
Here Insert Picture Description

Floating-point type

Consists of two floating-point types, and in fact most of us come into contact with the language are similar, float, and double, worth noting: precision floating-point number is determined by the number of significant digits, rather than a fixed value.
Here Insert Picture Description

decimal type

decimal is 128 decimal precision floating point types , although it is more accurate than the float, but is smaller than the floating-point range .
Here Insert Picture Description

Boolean

Represents a true or false in conditional expressions and conditional statements, the allowed values ​​include keywords true and false. It is represented by 8 bits.

Character Types

Character type is char, is represented by 16 bits. Note that it is inserted in the form of special characters:
Here Insert Picture Description
top to bottom, respectively: single and double quotation marks, as well as a backslash Null .
Here Insert Picture Description
From top to bottom are: Alert, backspace, feed, line feed, carriage return, horizontal tab , vertical tab, hexadecimal character Unicode, Unicode character hexadecimal (variable length version), Unicode escape sequences (for creating a surrogate pair).

C#冷知识

1,在构造字符串这件事上,string类型是不可变的,而StringBuilder不仅可以提供和string类似的方法,例如:Append、AppendFormat、Insert、Remove、Relpace,还能保证方法可以直接修改StringBuilder本身的数据而不是返回一个新的字符串。

2,赋值为null表示变量已赋值,但无任何值变量已赋值,但无任何值,这和赋值为“”以及不赋值是有区别的,区别如下:
Here Insert Picture Description

3,可以使用System.WriteLine()和System.Environment.NewLine()这两个方法来保证跨平台的换行符兼容性。

4,字面值表示源代码中的固定值,直接将值放到代码中叫硬编码,只要改值就需要重新编译代码,可以考虑将值通过配置文件读取,这样就不需要重新编译代码了。

5,默认情况下,输入小数会被当成double,输入整数会被当成int,如果整数超过int范围会被当成long。而C#还支持各种字面值的显示

 private static void Main()
        {
            Console.WriteLine(0b1000010); //二进制字面值
            Console.WriteLine(0x0002AFE); //十六进制字面值
            Console.WriteLine(6.02E23F); //指数计数法:E标识指数,F表示为浮点
            Console.WriteLine($"0x{42:x}"); //将十进制格式化为十六进制:0x2a,如果是大写X,则为0x2A
        }

6,通过R格式化来保留值的精度:

private static void Main()
        {
            const double number = 1.618033988749895;

            var text = $"{number}";
            var result = double.Parse(text); //精度丢失
            System.Console.WriteLine($"{result == number}"); //返回false

            text = $"{number:R}";
            result = double.Parse(text);  //保留原值
            System.Console.WriteLine($"{result == number}");  //返回true
        }

7,字符串驻留技术,虽然字符串不可变,赋值操作只会重新创建一个指向新内存的引用,所以改变str2的值操作并不会改变str1的值:

string str1 = "tml";
string str2 = str1;
str2 = "hhh" //并不会改变str1的值

公共语言运行库通过维护一个表来存放字符串,该表称为拘留池,它包含程序中以编程方式声明或创建的每个唯一的字符串的一个引用。因此,具有特定值的字符串的实例在系统中只有一个

string str1 = "CharlesChen";
string str2 = "CharlesChen";
System.Object.Equals(str1,str2)  //返回True

当我们调用时候,返回值是True,按道理说是应该返回为false,str1和str2应该指向不同的内存空间才对。怎么会返回为true呢?这里就引入了"字符串驻留技术"。

其实这里CLR使用了字符串驻留技术,当CLR初始化时,会创建一个内部的散列表(Hash表),其中的键位字符串,值为指向托管堆中字符串的引用。刚开始,散列表为空,JIT编译器编译方法时,会在散列表中查找每一个文本字符串常量(这里是"CharlesChen"),首先会查找"CharlesChen",并且因为没找到,编译器会在托管堆中构造一个全新的指向"CharlesChen"的对象引用,然后将"abc"字符串和执行该对象的引用添加到散列表中去。

当string str2=“CharlesChen"时候,由于前面已经在散列表中加了该"CharlesChen"字符串,所以编译器不会执行任何分配内存空间的操作。首先编译器会在内部的散列表中查找"CharlesChen”,并且会找到,这样指向先前创建的String对象的引用就会被找到,并且这里Str2就指向找到的那个引用。因此Str1和Str2就指向了内存中同一个地址的引用。所以System.Ojbect.Equals(str1,str2)就返回为true了。

C#新知识

1,类型转换这件事:所有数值类型都能通过Parse()方法将对应的string类型转为数值类型。然而这种转换是不安全的!推荐使用TryParse方法

private static void Main()
       {
            const string tml1 = "1234568"; //变量未赋值,编译不能通过
            var flag = int.TryParse(tml1, out var output);

            Console.WriteLine(output);
        }

C # 7.0 from the start, OUT parameters can be used in the form of inline without requiring a prior statement! In addition, also recommended System.Convert () method.

2, case-insensitive string comparison: string.Compare(temp1,temp2)not case-sensitive string comparisons: string.Compare(temp1,temp2,true),

Published 240 original articles · won praise 114 · views 180 000 +

Guess you like

Origin blog.csdn.net/sinat_33087001/article/details/103265396