Effective_C#学习笔记
- 格式:docx
- 大小:427.17 KB
- 文档页数:17
英语(一)(专升本)1. (单选题) This method is ______ which saves us half of the time and effort.(本题4.0分)A、efficientB、effectiveC、influentialD、fruitful学生答案:A标准答案:A解析:得分:42. (单选题) In one aspect, the seven-day National Day Holiday ______ the students’distraction from study.(本题4.0分)A、as a resultB、as a result ofC、results fromD、results in学生答案:D标准答案:D解析:得分:43. (单选题) Don’t write _______ pencil(本题4.0分)A、inB、withC、atD、by学生答案:A标准答案:A解析:无.得分:44. (单选题) I sometimes go to the pub _____ Friday.(本题4.0分)A、inB、onC、at学生答案:B标准答案:B解析:得分:45. (单选题) Your voice sounds different _______ the phone.(本题4.0分)A、onB、inC、fromD、by学生答案:A标准答案:A解析:无.得分:46. (单选题) Let's drink _______ Dicks' success in business.(本题4.0分)A、ofB、forC、toD、with学生答案:C标准答案:C解析:得分:47. (单选题) Japan lies _______ the east of China.(本题4.0分)A、onB、inC、atD、to学生答案:D标准答案:D解析:无.得分:48. (单选题) There is a big tree ________ the classroom building.(本题4.0分)A、beforeB、in front ofC、besidesD、in the front of学生答案:B标准答案:B解析:无.得分:49. (单选题) I will see you again _______ a week’s time.(本题4.0分)A、forB、inC、onD、after学生答案:B标准答案:B解析:无.得分:410. (单选题) They live _______ 10 Nanjing Road.(本题4.0分)A、atB、inC、overD、of学生答案:A标准答案:A解析:无.得分:411. (单选题) Mrs. Black has worked in a hospital in New York _______ 1990.(本题4.0分)A、inB、tillC、forD、since学生答案:D标准答案:D解析:无.得分:412. (单选题) ( ) Some friends of _______ went to attend the meeting last week.(本题4.0分)A、my fatherB、my father'sC、My fathers'D、my fathers学生答案:B标准答案:B解析:得分:413. (单选题) –Good night and thanks for everything.–________.( )(本题4.0分)A、You can’t say that.B、No, no. It’s what I can do.C、How can you say that?D、Good night.学生答案:D标准答案:D解析:得分:414. (单选题) At first she had a little trouble ________ the lectures. ( )(本题4.0分)A、followsB、followedC、followingD、follow学生答案:C标准答案:C解析:得分:415. (单选题) ( ) Which is the most popular _______ at the moment?(本题4.0分)A、piece of musicB、musicC、 a musicD、 a piece of music.学生答案:A标准答案:A解析:得分:416. (单选题) –Hi. I’m your neighbor. My name is Steve Edwin. –(本题4.0分)A、What are you from?B、Oh.C、Yes, I know.D、Hi. It’s good to see you. I’m Jane Peterson.学生答案:D标准答案:D解析:得分:417. (单选题) September is the _______ month of the year? ( )(本题4.0分)A、nineB、ninthC、the nineD、the ninth学生答案:D标准答案:B解析:得分:018. (单选题) ( ) 1. Jay spends all his free time_________.(本题4.0分)A、paintB、paintingC、paintedD、paints学生答案:B标准答案:B解析:得分:419. (单选题) ( ) The bookshelf over there is _______.(本题4.0分)A、Mary's and Jane'sB、Mary's and JaneC、Mary and Jane'sD、Mary and Jane学生答案:C标准答案:C解析:得分:420. (单选题) ( ) Mr.s Smith raises many _______ on his farm.(本题4.0分)A、deersB、sheepsC、mousesD、geese学生答案:C标准答案:D解析:得分:021. (单选题) As days _________, a deep friendship began to develop between them. (本题4.0分)A、went byB、went inC、went passD、went out学生答案:A标准答案:A解析:得分:422. (单选题) ( ) One day, the man upstairs heard the _______ of children _______ in the park.(本题4.0分)A、noise; to playB、sound; to playC、noise; playD、sound; playing学生答案:D标准答案:D解析:得分:423. (单选题) ( ) John: Jack, come and meet my friend Bob. _______.Jack: Nice to meet you.Bob: Same here.(本题4.0分)A、I am very happy.B、Bob, this is Jack.C、Nice to meet you.D、It’s my pleasure.学生答案:C标准答案:C解析:得分:424. (单选题) ( ) I prefer ________ than ________.(本题4.0分)A、reading; writingB、read;writeC、reads; writesD、read; wrote学生答案:A标准答案:A解析:得分:425. (单选题) ( ) Did the Smith buy _______ when they moved into the new house?(本题4.0分)A、many new furnituresB、much new furnituresC、many new furnitureD、much new furniture学生答案:D标准答案:D解析:得分:4。
Effective C++阅读笔记Effective C++阅读笔记 (1)原则3:尽可能使用const (4)原则5:了解C++默默编写并调用哪些函数 (6)原则6:若不想使用编译器自动生成的函数,就应该明确拒绝 (8)原则7:为多态基类声明virtual析构函数 (11)原则8:别让异常逃离析构函数 (15)原则9:绝不在构造和析构过程中调用virtual函数 (16)原则10:令operator=返回一个reference to *this (19)原则11:在operator=中处理“自我赋值” (19)原则12:复制对象时勿忘其每一个成分 (21)原则13:以对象管理资源 (22)原则14:在资源管理类中小心COPYING行为 (24)原则15:在资源管理类中提供对原始资源的访问 (25)原则16:成对使用new和delete时要采用相同形式 (27)原则17:以独立语句将newed对象置入智能指针 (28)原则18:让接口容易被正确使用,不易被误用 (29)原则19:设计class犹如设计type (30)原则20:宁以引用传递代替值传递 (31)原则21:必须返回对象时,别妄想返回其引用 (32)原则22:将成员变量声明为private (33)原则23:宁以非member、非friend替换member函数 (34)原则24:若所有参数皆需要类型转换,请为此采用非member函数 (35)原则25:考虑写出一个不抛出异常的swap函数 (37)原则26:尽可能延后变量定义式的出现时间 (39)原则27:尽量少做类型转换动作 (40)原则28:避免返回handles指向对象的部成分 (43)原则29:为“异常安全”而努力是值得的 (45)原则30:透彻了解inline(联)的里里外外 (48)原则31:将文件间的变异依存关系降至最低 (50)原则32:确定你的public继承塑造出了IS-A关系 (53)条款33:避免屏蔽继承而来的名字 (54)原则34:区分接口继承和实现继承 (55)原则35:考虑virtual函数以外的其他选择 (57)原则36:决不能重新定义继承而来的非virtual函数 (60)原则37:绝不重新定义继承而来的缺省参数值 (62)原则38:通过复合塑造出HAS-A关系或者根据某物实现出来 (63)原则39:明知而审慎地使用PRIVATE继承 (64)原则40:明智而审慎地使用多重继承 (69)原则41:了解隐式接口和编译期多态 (71)原则42:了解typename的双重意义 (72)原则43:学习处理模版化基类的名称 (74)原则44:将与参数无关的代码抽离templates (76)原则45:运用成员函数模版接受所有兼容类型 (78)原则46:需要类型转换时请为模版定义非成员函数 (80)原则47:请使用traits classes表现类型信息 (82)原则48:认识template元编程 (85)原则49:了解new-handler的行为 (87)原则50:了解new和delete的合理替换时机 (90)条款51:编写new和delete时需固守常规 (92)原则52:写了placement new也要写placement delete (94)原则54:不要忽视编译器的警告 (95)原则54:让自己熟悉包括TR1在的标准程序库 (96)原则55:让自己熟悉Boost (97)本来是写在百度空间的,但是不知道咋回事百度博客中图片看不到了,所以百度博客的不稳定性可见一斑。
C笔试题目及答案c语言笔试题目及答案c笔试题目及答案:c语言笔试题目及答案c笔试题目汇总篇11.谋下面函数的返回值(谷歌)intfunc(x){intcountx=0;while(x){countx++;x=x&(x-1);}returncountx;}假定x=。
答案:8思路:将x转变为2十进制,看看所含的1的个数。
2.什么是“引用”?申明和使用“引用”要注意哪些问题?请问:提及就是某个目标变量的“别称”(alias),对应用领域的操作方式与对变量轻易操作方式效果完全相同。
言明一个提及的时候,切勿必须对其展开初始化。
提及声明完后,相等于目标局部变量存有两个名称,即为该目标原名称和提及名,无法再把该提及名做为其他局部变量的别称。
声明一个提及,不是崭新定义了一个变量,它只则表示该提及名就是目标局部变量的一个别称,它本身不是一种数据类型,因此提及本身不占到存储单元,系统也不给提及分配存储单元。
无法创建数组的提及。
3.将“引用”作为函数参数有哪些特点?(1)传达提及给函数与传达指针的效果就是一样的。
这时,被调函数的形参就沦为原来主调函数中的实参变量或对象的一个别称去采用,所以在被调函数中对形参变量的操作方式就是对其适当的目标对象(在主调函数中)的操作方式。
(2)使用引用传递函数的参数,在内存中并没有产生实参的副本,它是直接对实参操作;而使用一般变量传递函数的参数,当发生函数调用时,需要给形参分配存储单元,形参变量是实参变量的副本;如果传递的是对象,还将调用拷贝构造函数。
因此,当参数传递的数据较大时,用引用比用一般变量传递参数的效率和所占空间都好。
(3)采用指针做为函数的参数虽然也能够达至与采用提及的效果,但是,在被调函数中同样必须给形参分配存储单元,且须要重复使用"*指针局部变量"的形式展开运算,这很难产生错误且程序的写作性极差;另一方面,在主调函数的调用点处,必须用变量的地址做为实参。
高二英语可持续发展策略单选题40题1. We need to take ______ measures to protect the environment and achieve sustainable development.A. effectiveB. efficientC. affectiveD. deficient答案:A。
本题考查形容词辨析。
A 选项“effective”表示“有效的”,采取有效的措施来保护环境实现可持续发展,符合语境。
B 选项“efficient”侧重于“效率高的”,强调完成任务的速度和效果;C 选项“affective”意思是“情感的”,与保护环境的措施无关;D 选项“deficient”是“不足的,有缺陷的”,不符合需要采取积极措施的意思。
2. The ______ use of natural resources is crucial for sustainable development.A. rationalB. irrationalC. nationalD. international答案:A。
“rational”意为“合理的”,合理使用自然资源对可持续发展至关重要。
B 选项“irrational”是“不合理的”;C 选项“national”是“国家的”;D 选项“international”是“国际的”,均不符合题意。
3. The government is making efforts to promote ______ growth in theeconomy.A. sustainableB. insustainableC. consumableD. inconsumable答案:A。
“sustainable”表示“可持续的”,政府努力促进经济的可持续增长。
B 选项“insustainable”意思是“不可持续的”;C 选项“consumable”是“可消费的”;D 选项“inconsumable”是“不可消费的”,都不符合句子表达的意思。
PC-lint测试C/C++实例实例1:test.cpp1 #include<string.h>2 class X3 {4 int *p;5 public:6 X()7 { p = new int[20]; }8 void init()9 { memset( p, 20, 'a' ); }10 ~X()11 { delete p; }12 };编译这个文件,VC6.0产生0 errors 0 warnings, 而lint程序产生了如下8条警告信息,有些还是很有用处的提示。
PC-lint 告警信息:test.cpp(12): error 783: (Info -- Line does not end with new-line)test.cpp(7): error 1732: (Info -- new in constructor for class 'X' which has no assignment operator)test.cpp(7): error 1733: (Info -- new in constructor for class 'X' which has no copy constructor) { memset( p, 20, 'a' ); }test.cpp(9): error 669: (Warning -- Possible data overrun for function 'memset(void *, int, unsigned int)', argument 3 (size=97) exceeds argument 1 (size=80) [Reference: test.cpp: lines 7, 9])test.cpp(7): error 831: (Info -- Reference cited in prior message)test.cpp(9): error 831: (Info -- Reference cited in prior message){ delete p; }test.cpp(11): error 424: (Warning -- Inappropriate deallocation (delete) for 'new[]' data)--- Wrap-up for Module: test.cpptest.cpp(2): error 753: (Info -- local class 'X' (line 2, file test.cpp) not referenced)error 900: (Note -- Successful completion,8 messages produced)根据错误提示修改后的程序如下:#include<string.h>class X /*lint -e753 *///只声明实现类X,没有写main()应用类x,故可以屏蔽。
1. 引言C语言在嵌入式领域应用非常广泛,其主要优点是灵活和高效,但若在使用过程中不加以规范,容易引入各种潜在的代码问题。
先来看一段C代码,这段代码中有多少C语言的使用错误?uint8_t vFunc(void){uint8_t index = 0;for (index = 0; index {if (index == 3){vFunc_1(&index);return true;}}}比较容易发现的问题有:●for循环结束后没有返回值不太容易发现的问题有:●index初值没有使用●index值可能在vFunc_2()内被清零,使得循环一直被执行为了在编码阶段找出这些问题,常用的分析方法有静态代码分析和动态代码分析。
有研究表明,使用动态代码分析找到的问题中的95%都可以用静态代码分析找到。
中小型嵌入式软件项目一般使用静态代码分析工具作为主要分析手段,其中最常见的工具是PClint。
PCLint是GIMPEL SOFTWARE 公司研发的C/C 软件代码静态分析工具,支持几乎所有流行的编辑环境和编译器。
PC-lint不但可以检测单个文件,也可以从整个项目的角度来检测问题。
支持Scott Meyes的名著(Effective C /More Effective C )中说描述的各种提高效率和防止错误的方法。
在我们的项目中,使用C语言作为PIC32单片机的编程语言,开发环境是MPLABX,代码查看工具是source insight。
本文的主要内容就是如何将PCLint 工具集成到上述两种工具中。
集成完成后,可通过快捷键调用lint工具,对目标代码进行静态分析,再根据分析结果优化代码。
2. 配置PCLint2.1. 安装PCLint下载PCLint并安装:升级到最新版本:,下载下列文件:●复制升级包和lpatch到安装路径(C:\lint)●按住shift 右键,打开快捷菜单,选择“在此处打开命令窗口”●输入命令并执行:lpatch l9-a-b.lp, …, 依次类推直到l9-K-L.lp(每次输入一条执行)●输入命令lint-nt -v,完成上述步骤后,显示当前版本是9.00L2.2. 下载xc32编译器共用的文件打开,下载Compiler Options files:●co-cci.lnt●co-xc32.lnt将上述文件存放在lint安装路径下的lnt文件夹中(c:\lint\lnt)。
Part 3101. The new policy on overtime compensation ______ addresses the complaints of the union.(A) effect (B) effective (C) effectively (D) effectiveness102. It is difficult for management both to satisfy ______ to discipline such a large staff.(A) if (B) and (C) such (D) but103. Mr. Smith ______ his weekly groceries every Friday, so tell him what you need.(A) order (B) orders (C) to order (D) ordered104. President Jacobson is expected to be in town ______ the meeting of industry leaders.(A) for (B) under (C) across (D) down105. The bookshelf is too big for Andrew and Jessie to carry by ______.(A) their (B) them (C) themselves (D) their own106. You will need your order tracking number to confirm the ______ of your gift on the website.(A) deliver (B) delivery (C) deliverer (D) delivered107. Janet Evans is expected to replace Mr. Bowers ______ CEO of Fabrics US following his retirement.(A) as (B) like (C) about (D) but108. For the renovation of the main stairway, please enter through either the elevator ______ the back stairwell.(A) or (B) with (C) into (D) not109. Most people shop for their home building supplies at Jay's Lumber Yard because they offer satisfaction guarantees on ______ orders.(A) any of (B) each (C) all (D) every110. ______ the Chinese buffet restaurant is in the center of the financial district, it is always busy with businessmen at lunch.(A) Therefore (B) But (C) Despite (D) Since111. Because the banquet budget was _____ calculated, the planning committee had to choose a less expensive catering service.(A) inaccuracy (B) inaccurate (C) inaccurately (D) inaccuracies112. After we have received authorization from the head office, we will send ______ the employment contract to sign.(A) she (B) her (C) hers (D) herself113. Rental prices of our condos are ______ to property availability and can change at the owner's discretion.(A) subject (B) public (C) plain (D) general114. Mrs. Dowry is ______ to receive the award of public relations officer of the year.(A) pleasure (B) pleased (C) pleasing (D) please115. Recent surveys of college undergraduates indicate ___ most students plan to major in business, economics, or engineering.(A) which (B) that (C) what(D) those116. In an unprecedented ____ of kindness, the local library donated hundreds of children's books to orphanages across the city.(A) act (B) progress (C) chance (D) number117. A newly implemented management system is responsible for ______ sales at the retail store.(A) increase (B) increases (C) increased (D) increasingly118. Check the batteries of your fire alarms ______ to keep up with safety regulations.(A) avoidably (B) regularly (C) lately (D) highly119. Any requests for vacation time must have the ______ of the general manager.(A) approve (B) approved (C) approval (D) approvingly120. For those who wish to join the summer internship, the ___ for turning in applications is April 16 at 10 a.m.(A) calendar (B) intention (C) admission (D) deadline121. ______ Brigg Electric has its headquarters in Los Angeles, it has branches throughout the continental US.(A) Although (B) Until now (C) Or else (D) Thus122. Our professional reception staff ______ happy to assist you with any problems you may have while staying at our hotel.(A) is being (B) are being (C) would be (D) have been123. Because of an urgent personal matter, Dr. Grey's talk will be ______ until next month's gathering.(A) belated (B) directed (C) presented (D) postponed124. The community college has a total of twenty academic departments ______ its institution.(A) between (B) because (C) within (D) as for125. Human resources offers orientation seminars to ______ hired staff.(A) approximately (B) recently (C) exactly (D) comparatively126. The model of the bicycle you want is available for order ______ gray, silver, and gold.(A) in (B) at (C) to (D) of127. The town's tourist information desks are ______ by their large, bright signs.(A) identify (B) identifies (C) identity (D) identifiable128. The hospital has high ______ for the incoming surgeon, Dave Willis, who specializes in cardiac diseases.(A) expecting (B) expected (C) expectation (D) expectedly129. It is vital that employees maintain physical health for their personal benefit ______ the company's success.(A) even though (B) in order to (C) whether (D) as well as130. Political analyst Rober Shaw has been commended for the refreshing ______ in his essays on capitalism.(A) clear (B) clearly (C) clarity (D) clarifies131. Celebrations for the store's tenth ______ will include major discounts on products, free samples of food stuff, and face painting for kids.(A) date (B) anniversary (C) origin (D) inception132. The doctor ______ discovered the vaccine for the common cold is taking time off from his research for personal reasons.(A) he (B) who (C) some (D) also133. Of all the candidates for head of accounting, Mr. Lee is by far the most ______ and experienced.(A) conditional (B) requisite (C) secured (D) qualified134. Flower deliveries are made within 24 hours ______ orders are called in by 2 p.m.(A) when (B) even (C) over (D) while135. The law firm's website has been ______ to increase usability for clients needing to track their cases.(A) progressed (B) relied (C) designed (D) notified136. ______ Cheryl leaves for Japan, she should be briefed on basic business culture etiquette.(A) Prior (B) Near (C) Before (D) Past137. During the winter owners must ensure that cars don't remain ______ for more than three days at a time.(A) void (B) idle (C) spare (D) null138. Marshall Appliances has chosen to make a contract with a new marketing company ______ continuing its relationship with Friendly PR.(A) out of (B) except for (C) as to (D) instead of139. Broadband Plus sent out an e-mail apology to its clients for the ______ in its wireless Internet service.(A) disruption (B) limitation (C) outbreak (D) controversy140. ______ a number of new computer programmers will be joining the team, the director will increase the number of projects for the next year.(A) If so (B) Rather than (C) Owing to (D) Given thatPart 4Questions 141 to 143 refer to the following notice.This is to inform you that the seminar for all department heads ______ place on Wednesday, July 15.141. (A) took(B) will take(C) will have taken(D) takingAll middle and lower management staff are ______ to attend unless they have previous commitments.142. (A) reported(B) favored(C) required(D) includedThe seminar will include topics ______ business and leadership skills, performance management143. (A) as long as(B) likewise(C) nevertheless(D) such asplanning, and the employee evaluation and feedback process. Please come to Siena room on the fifteenth floor of the Jameson Building. Don’t forget to bring the seminar materials to the session.Please contact Mr. Blake Mariano if you have any questions.Questions 144 to 146 refer to the following letter.I am writing in reply to your article on a French tour agency in Montpellier, France. In your article, the South France Adventures Agency was given an unfavorable review. You mentioned that its tours were conducted by inexperienced tour guides. ______, my experience with the tour agency was very good.144. (A) Consequently(B) However(C) Then(D) In additionThe guide had extensive knowledge about the history of South France, giving complete explanations and __145. (A) answer(B) answers(C) to answer(D) answering questions with specifics. I would definitely ______ taking a South France Adventures tour to anyone who wishes146. (A) mention(B) discourage(C) recommend(D) considerto visit the south of France.Questions 147 to 149 refer to the following letter.Thank you for purchasing the full kitchen remodeling package. Your ______ of our company is highly147. (A) patronage(B) supervision(C) predicament(D) cooperationappreciated. We hope you are delighted with the new kitchen facilities and are fully satisfied with the services and designs provided by Home Design, Inc.We have enclosed a customer survey form. Your comments will help us ______ your future needs.148. (A) wish(B) meet(C) please(D) thinkTo encourage you to take part in our survey, we will enter you in a special draw for a free set of home fixtures we recently launched. All you need to do is send in the completed form by November 30 in order to be eligible for the drawing. Winners ______ on December 3.149. (A) should notify(B) was notified(C) will be notified(D) notifiesThank you once again.Questions 150 to 152 refer to the following article.答案见下方截图:。
北京九中2024--2025学年度第一学期期中统练2024.11年级:高二科目:英语(考试时间 100 分钟满分 100 分)第一部分:知识运用(共30分)一、词汇(共20小题,每小题0.5分, 共10分。
从每题所给的A、B、C、D 四个选项中, 选出最佳选项。
)1.请选出与单词“access”匹配的释义。
A. 特点B. 主人C. 报告D. 进入2.请选出与单词“convince”匹配的释义。
A. 便利B. 自信C. 说服D. 控制3.请选出与单词“effective”匹配的释义。
A. 效率B. 有效的C. 效果D. 影响4.请选出与单词“employee”匹配的释义。
A. 雇员B. 雇主C. 雇佣D. 雇佣合同5.请选出与单词“depressed”匹配的释义。
A. 绝望的B. 沮丧的C. 拼命的D. 敏感的6.请选出与单词“guilty”匹配的释义。
A. 自私的B. 忧愁的C. 内疚的D. 烦恼的7.请选出与单词“competitor”匹配的释义。
A. 竞争B. 比赛C. 能力D. 选手8.请选出与单词“devotion”匹配的释义。
A. 宿舍B. 提升C. 奉献D. 信任9.请选出与单词“contrast”匹配的释义。
A. 合同B. 差异C. 控制D. 捐献10.请选出与单词“intensity”匹配的释义。
A. 强度B. 意图C. 影响D. 邀请11.请选出与释义“极好的;难以置信的”匹配的单词。
A. incredibleB. passionateC. worthwhileD. sensitive12.请选出与释义“热心的,热衷的”匹配的单词。
A. emotionalB. enthusiasticC. gratefulD. practical13.请选出与释义“偏好,爱好”匹配的单词。
A. passionB. presentationC. preferenceD. principle14.请选出与释义“最后的,最终的”匹配的单词。
原则1:始终能的使用属性(property),而不是可直接访问的Data Member当你想让你类内部的数据被外界访问到时(不管是public还是protected),一定要用Property。
对于序列和字典,使用indexer。
你类的data member永远应该是private,绝无例外。
使用Property,你可以得到如下好处:1.Data binding支持2.对于需求变化有更强的适应性,更方便的修改实现方法3.Property是用methods实现的,所以添加multi-threaded的支持是非常方便的。
4.Property可以被定义为virtual,你也可以把Property扩展为abstract,甚至成为interface的一部分。
你当然也可以扩展出const和nonconst版本的interface。
5.实现Property访问的方法get and set是独立的两个method,你可以给它们定义不同的访问级别,来更好的控制类成员的可见性。
原则2:为你的常量选择readonly而不是const对于常量,C#里有两个不同的版本:运行时常量和编译时常量。
两害相权取其轻,当我们不得不选择一个的时候,我们宁可选择一个运行慢一点但正确的那一个(运行时常量),而不是运行快一点但有错误的那个(编译时常量)。
使用运行时常量,你可以得到如下好处:1.一个编译时常量会被目标代码中的值直接取代。
运行时常量的值是在运行时确定的。
当你引用一个只读常量时(read-only)IL会为你引用一个运行时常量的变量,而不是直接使用该值。
2.编译时常量只能是基本类型(primitive types)(built-in integral and floating-poing types),枚举或者是字符串,运行时常量可以是任何类型的数据。
而且你必须在构造函数里对他们初始化,或者你可以用任何一个初始化函数来完成。
你可以添加一个DateTime结构的只读变量(--运行时常量),但你不能添加一个DateTime结构的(编译时)常量。
3.编译时常量生成的IL代码就跟直接使用数值时生成的IL是一样的,即使是在跨程序集时:一个程序集里的编译时常量在另一个程序集会保留着同样的值。
编译时常量和运行时常量的赋值方法对运行时的兼容性有所影响。
原则3:选择is或者as操作符而不是做强制类型转换无论何时,类型转化正确的选择是用as运算符进行类型转换。
因为比起盲目的强制转换它更安全,而且在运行时效率更高。
用as和is运算符进行转换时,并不是对所有的用户定义的类型都能完成的。
它们只在运行时类型和目标类型匹配的时候,转换才能成功。
它们决不会构造一个新的对象来满足(转化)要求。
使用as运算符,你可以得到如下好处:1.强制转换的方法为了检测转换是否把一个null的对象进行强制转换,而不得不添加一个捕获异常的结构。
null可以被转换为任意的引用类型,但as操作符就算是转化一个null的引用时,也会(安全的)返回一个null。
所以当你用强制类型转换时,就得用一个try/catch结构来捕获转换null时的异常。
用as进行转换的时就,就只用简单的检测一下转化后的实例不为null就行了。
2.强制转换与as转换最大的区别表现在如何对待用户定义类型的转换。
对一个用户定义类型的对象,转换操作只是在编译时,而不是在运行时。
如果待转换的对象属于是内置类型,那么它的转换是在运行时。
3.as运算符对值类型是无效的,你可以坚持用强制转化,同时可以用is语句取消可能因转换引发的异常。
is和as一样,都是类型转换安全的,它们在任何时候都不会在转换时发生异常,因此可以先用is来安全的判断一下数据类型。
与as 不同的时,is只是做类型检测并返回逻辑值,不做转换。
Is只应该在你无法用as进行转换时使用。
原则4:用条件属性而不是#if程序语言设计者有责任提供更好的工具,用于生成在不同运行环境下的机器代码。
C#就提供了条件属性(Conditional attribute)来识别哪些方法可以根据环境设置来判断是否应该被调用。
这个方法比条件编译#if/#endif更加清晰明白。
编译器可以识别Conditional属性,所以当条件属性被应用时,编译器可以很出色的完成工作。
条件属性是在方法上使用的,所以这就使用你必须把不同条件下使用的代码要写到不同的方法里去。
当你要为不同的条件生成不同的代码时,请使用条件属性而不是#if/#endif块。
使用条件属性,你可以得到如下好处:1.用条件属性,你可以在指定的编译环境下废弃一个类的部份函数,而这个环境可是某个变量是否被定义,或者是某个变量具有明确的值。
这一功能最常见的用法就是使你的代码具有调试时可用的声明。
2.条件属性可以由定义标记来灵活的控制。
你可以在编译命令行上定义,也可以在系统环境变量里定义,或者从源代码的编译选择里定义。
原则5:始终提供ToString()在.Net世界里,用得最多的方法之一就是System.Object.ToString()了。
你应该为你所有的客户写一个“通情达理”的类(译注:这里是指这个类应该对用户友好)。
要么,你就迫使所用类的用户,去使用类的属性并添加一些合理的易读的说明。
这个以字符串形式存在,关于你设计的类的说明,可以很容易的向你的用户显示一些关于对象的信息到:Windows Form里,Web Form里,控制台输出。
这些字符说明可以用于调试。
你写的任何一种类型,都应该合理的重写这个方法。
当你设计更多的复杂的类型时,你应该实现应变能力更强的IFormattable.ToString(). 承认这个:如果你不重写(override)这个常规的方法,或者只是写一个很糟糕的,你的客户将不得不为你修正它。
原则6:区别值类型数据和引用类型数据这不是一个简单的非此及彼的选择。
正确的选择取决于你希望你的新类型该如何使用。
值类型不具备多态性,但它们在你的应用程序对数据的存取却是性能有佳;引用类型可以有多态性,并且你还可以在你的应用程序中为它们定义一些表现行为。
考虑你期望给你的类型设计什么样的职能,并根据这些职能来决定设计什么样的类型。
结构存储数据,而类表现行为。
你创建的引用类型可能比值类型要多。
如果你对下面所有问题回答YES,你应该创建值类型数据。
1.类型的最基本的职责是存储数据吗?2.它的属性上有定义完整的公共接口来访问或者修改数据成员吗?3.我对类型决不会有子类自信吗?4.我对类型决不会有多太性自信吗?把值类型当成一个低层次的数据存储类型,把应用程序的行为用引用类型来表现。
你会在从类暴露的方法那取得安全数据的COPY。
你会从使用内联的值类型那里得到内存使用高率的好处。
并且你可以用标准的面向对象技术创建应用程序逻辑。
当你对期望的使用拿不准时,使用引用类型。
原则7:选择恒定的原子值类型数据恒定类型(immutable types)其实很简单,就是一但它们被创建,除非重新初始化以外它们(的值)就是固定的。
恒定类型是更简单,更容易维护的。
不要盲目的为你的每一个对象的属性创建get和set访问器。
你对这些类型的第一选择是把这些数存储为恒定类型,原子类型。
从这些实体中,你可以可以容易的创建更多复杂的结构。
如果你验证一些准备用于创建一个对象的参数,你知道它在验证状态从前面的观点上看。
你不能修改一个对象的内部状态使之成为无效的。
在一个对象被创建后,你必须自己小心翼翼的保护对象,否则你不得不做错误验证来禁止改变任何状态。
恒定类型天生就具有线程完全性的特点:多访问者可同时访问相同的内容。
如果内部状态不能修改,那么就不能给不同的线程提供查看不一致的数据视图的机会。
恒定类型可以从你的类上安全的暴露出来。
调用者不能修改对象的内部状态。
恒定类型可以很好的在基于哈希代码的集合上工作。
为了创建一个恒定类型,你须要确保你的用户没有任何机会来修改内部状态。
有三个策略,是在初始化恒定对象时应该使用的。
1.定义了一个构造函数。
用构造函数来确保其内部状态的正确性。
2.创建一个工厂方法来实现一个结构工厂使得创建一个通用的值型数据变得更容易。
.Net框架的Color类型就是遵从这一策略来初始化系统颜色的。
这个静态的方法Color.FromKnownColor()和Color.FromName()从当前显示的颜色中拷贝一个给定的系统颜色,返回给用户。
3.需要多步操作才能完成构造函数的恒定类型添加一个伴随类。
.Net框架里的字符串类就遵从这一策略,它利用了伴随类System.Text.StringBuilter。
你是使用StringBuliter类经过多步操作来创建一个字符串。
在完成了所有必须步骤生成一个字符串类后,你从StringBuilter取得了一个恒定的字符串。
原则8:确保0对于值类型数据是有效的.Net系统默认所有的对象初始化时都为0。
这并没有提供一个方法来预防其他程序员创建的值类型数据的实例在初始化是都是0。
请让你的数据类型默认值也是0。
为确保0对于值类型数据是有效的原则,以下原则是需要注意的地方:1.一个特殊情况是在枚举类型数据中。
决不要创建一个不包括0在内的枚举类型。
如果你的枚举类型采用的是以位(bit)模式,把0定义为其它属性不存在时的取值。
枚举类型在使用Flags特性时,必须把None的值设置为0。
2.当值类型中包含了引用类型(例如字符串)。
要尽量避免出现某一属性出现NULL引用的情况。
如果是字符串,则在构造函数时最好添加业务逻辑将其初始值设为string.Empty.原则9:明白几个相等运算之间的关系当你创建你自己的类型时(不管是类还是结构),你要定义类型在什么情况下是相等的。
C#提供了4个不同的方法来断定两个对象是否是相等的:1.public static bool ReferenceEquals (object left, object right);2.public static bool Equals (object left, object right);3.public virtual bool Equals(object right);4.public static bool operator==(MyClass left, MyClass right);c#语言让你可以为上面所有的4种方法创建自己的版本。
但是你或许从来不用重新定义前面两个方法,你经常遇到的是创建你自己实例的Equals()方法,来为你的类型定义语义;或者你偶而重载运==运算符,但这只是为了考虑值类型的性能。
和C#里其它大多数复杂元素一样,这个(对相等的比较运算)也遵守这样的一个事实:C#充许你同时创建值类型和引用类型。