C#核编之内建数据类型

时间:2022-04-24
本文章向大家介绍C#核编之内建数据类型,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

这个随笔中的重点之一是说明:C#中所提供的关键字都是相应系统类型的简化符号(如int是System.Int32类型的简化符号)

一、内建数据类型与层级结构

所有的C#内建数据类型都支持默认的构造函数,简而言之,这个特性允许我们使用new关键字来创建变量,他将变量自动设置为其默认值。

1、bool类型设置为false;

2、数值类型设置为0;

3、char类型设置为单个空字符;

4、float设置为0.0;

5、BigInteger变量设置为0;

6、DateTime类型设置为1/1/0001 12:00:00 AM;

7、对象引用(包括string)设置为null;

下面是使用new来创建基本数据类型变量的代码,尽管显得很笨重,但是确实可行的代码:

            Console.WriteLine("Using new to create variables:");
            bool b = new bool();
            int i = new int();
            double d = new double();
            DateTime dt = new DateTime();
            float f = new float();
            Console.WriteLine("{0},{1},{2},{3},{4}", b, i, d, dt, f);//输出:False,0,0,0001/1/1 0:00:00,0

二、数据类型的层次结构

在C#中,每一个基本数据类型都有一个类层次结构,处于类层次顶端的类型会为派生类提供一些默认的行为。核心的系统类型之间的关系如下图所示:

以上所有的类型都派生自System.Object,它定义了一组.NET基础类库中所有类型都具有的方法(如:ToString()、Equals()、GetHashCode()等),下面通过一段简单的代码来了解一些这些基础方法:

     Console.WriteLine("12.GetHashCode={0}", 12.GetHashCode());//12.GetHashCode=12
     Console.WriteLine("12.Equals(23) is {0}", 12.Equals(23));//12.Equals(23) is False
     Console.WriteLine("12.ToString() is {0}", 12.ToString());//12.ToString() is 12
     Console.WriteLine("12.GetType() is {0}", 12.GetType());//12.GetType() is System.Int32

三、数值数据类型的的成员

1、.net的数值类型支持MaxValue和MinValue属性,这两个属性说明了给定的类型可以存储的范围;

Console.WriteLine("Max of int is {0}", int.MaxValue);//输出:Max of int is 2147483647
Console.WriteLine("Min of int is {0}", int.MinValue);//输出:Min of int is -2147483648
Console.WriteLine("Max of double is {0}", double.MaxValue);//输出:Max of double is 1.79769313486232E+308
Console.WriteLine("Min of double is {0}", double.MinValue);//输出:Min of double is -1.79769313486232E+308

除了MaxValue和MinValue属性之外,一个给定的系统数据类型还可能定义其他更有用的成员,例如,可以用System.Double类型获取正无穷大和无穷小数:

Console.WriteLine("Min of double is {0}", double.PositiveInfinity);//输出:正无穷大
Console.WriteLine("Min of double is {0}", double.NegativeInfinity);//输出:无穷小
Console.WriteLine("Min of double is {0}", double.Epsilon);//输出:大于零的最小正System.Double值4.94065645841247E-324

四、System.Boolean的成员

1、TrueString   这个属性返回true

2、FalseString  这个属性返回false

Console.WriteLine("bool.TrueString is {0}", bool.TrueString);//输出:bool.TrueString is True
Console.WriteLine("bool.FalseString is {0}", bool.FalseString);//输出:bool.FalseString is False

五、System.Char的成员

C#中的文本数据使用string和char关键字来表示的,他们是System.String和System.Char的简化符号,string表示一组连续的字符如"hello",而char则表示单个字符如'a',System.Char除了表示单个字符外,还保留了大量的功能,使用System.Char的静态方法,可以判定一个字符是否是数字、字母、标点符号或者其他;

char mychar = 'a';
Console.WriteLine("char.IsDigit('a'):{0}", char.IsDigit(mychar));//输出:char.IsDigit('a'):False  判断指定的 Unicode 字符是否属于十进制数字类别。
Console.WriteLine("char.IsLetter('a'):{0}", char.IsLetter(mychar));//输出:char.IsLetter('a'):True  判断指定的 Unicode 字符是否属于 Unicode 字母类别。
Console.WriteLine("char.IsWhiteSpace(' '):{0}", char.IsWhiteSpace(' '));//输出:char.IsWhiteSpace(' '):True   ---判断目标字符是不是空字符
Console.WriteLine("char.IsWhiteSpace('hello world', 5):{0}", char.IsWhiteSpace("hello world", 5));//输出:char.IsWhiteSpace('hello world', 5):True    ---判断"hello world"字符串中的第五个字符是不是空格
Console.WriteLine("char.IsWhiteSpace('hello world', 6):{0}", char.IsWhiteSpace("hello world", 6));//输出:char.IsWhiteSpace('hello world', 5):False   ---判断"hello world"字符串中的第六个字符是不是空格
Console.WriteLine("char.IsPunctuation('?'):{0}", char.IsPunctuation('?'));//输出:char.IsPunctuation('?'):True   ---判断'?'是不是标点符号类别

六、从字符串数据中获取字符串数值,并解析成C#系统数值

.NET数据类型提供了一种能力,即通过给定文本(字符串)生成相应的底层类型的变量。这种技术把用户输入的数据转换成一个数值。---简而言之、数据类型转换从字符串到数值

Console.WriteLine("Data Type Parsing:");
bool b = bool.Parse("true");
Console.WriteLine("Value of b is {0}", b);//输出:Value of b is True
double d = double.Parse("666.666");
Console.WriteLine("Value of d is {0}", d);//输出:Value of d is 666.666
int i = int.Parse("666");
Console.WriteLine("Value of i is {0}", i);//输出:Value of i is 666
char c = char.Parse("c");
Console.WriteLine("Value of c is {0}", c);//输出:Value of c is c

七、System.DateTime和System.TimeSpan

在System命名空间中定义了很多有用的数据类型,对于这些数据类型,没有C#关键字,比如DateTime和TimeSpan结构

Console.WriteLine("DateTime and TimeSpan");
DateTime dt = new DateTime(1994,9,18);
Console.WriteLine("The Day of {0} is {1}", dt.Date, dt.DayOfWeek);//输出:The Day of 1994/9/18 0:00:00 is Sunday
dt = dt.AddMonths(2);
Console.WriteLine("Daylight savings:{0}", dt.IsDaylightSavingTime());//输出:Daylight savings:False   ---判断当前月份是不是夏天
TimeSpan ts = new TimeSpan(6, 6, 6);
Console.WriteLine(ts);//输出:06:06:06
Console.WriteLine(ts.Subtract(new TimeSpan(0, 2, 0)));//输出:06:04:06

八、System.Numberics.dll程序集之BigInteger类型

在System.Numberics命名空间中定义了BigInteger结构。顾名思义,BigInerger用来表示极大的数值,它没有固定的上下限。

尽管大多数应用程序都不需要使用BigInteger结构,但一旦需要定义较大的数值时,这个时候做的第一件事就是导入System.Numberics.dll程序集的引用,在添加using指令,之后就可以通过new来使用BigInterger,在其构造函数中,可以指定包括浮点函数在内的任何数值类型。但是当你定义一个整数(如500),运行时将其默认设为int数据类型,同样,当设置(66.66)运行时则将其默认设置为double类型.这是C#内部的自动识别数据类型机制,虽然你是用的是BigInterger来创建BigInterger变量,但当给他的构造函数传递的是其他类型的数据,他同样会找到对应的数据类型与之匹配,那么怎么创建BigInterger类型的数据呢,很简单!

上面六中提到的将文本字符串解析为系统数据类型的方法,通过Parse方法将大数值以字符串的形式传递给BigInteger构造函数,来创建一个BigInteger类型。代码如下:

BigInteger bi = BigInteger.Parse("999999999999999999999999999999999999999999999999999999999999999999");
Console.WriteLine("Value of the biggy is {0}", bi);//输出该字符串所代表的数
Console.WriteLine("Is biggy an even value?:{0}", bi.IsEven);//输出:Is biggy an even value?:False   --判断bi是否是偶数
Console.WriteLine("Is biggy a power of two?:{0}", bi.IsPowerOfTwo);//输出:Is biggy a power of two?:False   --判断bi是否是2的幂
BigInteger bi_1 = BigInteger.Parse("66666666666666666666666666666666666666666666666666666666666666666666");
BigInteger sum = bi + bi_1;
BigInteger multiply = bi * bi_1;
Console.WriteLine("sum is {0},Multiply is {1}", sum, multiply);//输出:sum is 67666666666666666666666666666666666666666666666666666666666666666665   乘积

根据上面的代码,发现BigInteger数据类型能够响应基本的数学操作符+、-、*,因此,在对两个大树执行生发运算时,不必调用Biteger.Multiply();