分享
 
 
 

[c#]8.2.1 Predefined types(cont')

王朝c#·作者佚名  2006-01-10
窄屏简体版  字體: |||超大  

results in a compile-time error because the expression i = 0 is of type int,

and if statements require an

expression of type bool.

The char type is used to represent Unicode code units. A variable of type

char represents a single 16-bit

Unicode code unit.

The decimal type is appropriate for calculations in which rounding errors

caused by floating point

representations are unacceptable. Common examples include financial

calculations such as tax computations

and currency conversions. The decimal type provides 28 significant digits.

The table below lists the predefined types, and shows how to write literal

values for each of them.

Type Description Example

object The ultimate base type of all other types object o = null;

string String type; a string is a sequence of Unicode code

units

string s = "hello";

sbyte 8-bit signed integral type sbyte val = 12;

short 16-bit signed integral type short val = 12;

int 32-bit signed integral type int val = 12;

long 64-bit signed integral type long val1 = 12;

long val2 = 34L;

byte 8-bit unsigned integral type byte val1 = 12;

ushort 16-bit unsigned integral type ushort val1 = 12;

uint 32-bit unsigned integral type uint val1 = 12;

uint val2 = 34U;

ulong 64-bit unsigned integral type ulong val1 = 12;

ulong val2 = 34U;

ulong val3 = 56L;

ulong val4 = 78UL;

float Single-precision floating point type float val = 1.23F;

double Double-precision floating point type double val1 = 1.23;

double val2 = 4.56D;

bool Boolean type; a bool value is either true or false bool val1 = true;

bool val2 = false;

char Character type; a char value is a Unicode code unit char val = 抙?

decimal Precise decimal type with 28 significant digits decimal val = 1.23M;

Each of the predefined types is shorthand for a system-provided type. For

example, the keyword int refers

to the struct System.Int32. As a matter of style, use of the keyword is

favored over use of the complete

system type name.

Predefined value types such as int are treated specially in a few ways but

are for the most part treated

exactly like other structs. Operator overloading enables developers to

define new struct types that behave

much like the predefined value types. For instance, a Digit struct can

support the same mathematical

operations as the predefined integral types, and can define conversions

between Digit and predefined

types.

The predefined types employ operator overloading themselves. For example,

the comparison operators ==

and != have different semantics for different predefined types:

?Two expressions of type int are considered equal if they represent the

same integer value.

?Two expressions of type object are considered equal if both refer to

the ame object, or if both are null.

?Two expressions of type string are considered equal if the

string instances have identical lengths and

identical characters in each character position, or if both are

null.

The example:

using System;

class

Test

{

static void Main()

{

string s = "Test";

string t = string.Copy(s);

Console.WriteLine(s == t);

Console.WriteLine((object)s == (object)t);

}

}

produces the output

True

False

because the first comparison compares two expressions of type string,

and the second comparison

compares two expressions of type object. (Note that when the

Standard Library produces a string

representation of a Boolean value, as is the case with

System.WriteLine above, it uses .True. and

.False., while the corresponding C# language Boolean literals are

spelled true and false.)

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有