版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、class Employee class Employee publicpublic Int32 Age; Int32 Age; -Employee e = new Employee();Employee e = new Employee();e.Age = -5;e.Age = -5;class Employee class Employee privateprivate Int32 Age; Int32 Age; publicpublic Int32 GetAge() return Age; Int32 GetAge() return Age; publicpublic void SetA
2、ge(Int32 value) void SetAge(Int32 value) if(value 0) . if(value 0) . Age = value; Age = value; -Employee e = new Employee();Employee e = new Employee();e.SetAge(-5); e.SetAge(-5); /可进行错误数据检验可进行错误数据检验Console.WriteLine(e.GetAge();Console.WriteLine(e.GetAge();class Employee class Employee privateprivat
3、e Int32 age; Int32 age; public Int32 public Int32 AgeAge /一般大写字母开头一般大写字母开头 getget return age; return age; setset if( if(valuevalue 0) . 0) . age = age = valuevalue; ; -Employee e = new Employee();Employee e = new Employee();e.Age = -5; e.Age = -5; /可进行不合理数据检验可进行不合理数据检验class EmployeeGroup class Emplo
4、yeeGroup private String m_EmployeeName; private String m_EmployeeName; IndexName(“Employee”) IndexName(“Employee”) public String public String thisthisInt32 EmployeeID .Int32 EmployeeID . /编译器生成编译器生成get_Employeeget_Employee和和set_Employeeset_Employee方法方法internal class NewMailinternal class NewMailEve
5、ntArgsEventArgs : : EventArgsEventArgs private readonly String m_from, m_to, m_subject; private readonly String m_from, m_to, m_subject; public NewMailEventArgs(String from, String to, String public NewMailEventArgs(String from, String to, String subject) subject) m_from = from; m_to = to; m_subject
6、 = subject; m_from = from; m_to = to; m_subject = subject; public String From get return m_from; public String From get return m_from; public String To get return m_to; public String To get return m_to; public String Subject get return m_subject; public String Subject get return m_subject; internal
7、class MailManager internal class MailManager public eventpublic event EventHandler EventHandler NewMail;NewMail; -/System.EventHandler/System.EventHandler的定义原型:的定义原型:public public delegatedelegate void EventHandler void EventHandler( (ObjectObject sender, TEventArgs e) sender, TEventArgs e) wherewhe
8、re TEventArgs: TEventArgs: EventArgs;EventArgs;/where表示对泛型声明中定义的类型参数的约束表示对泛型声明中定义的类型参数的约束-/所需事件处理方法的签名必须如下:所需事件处理方法的签名必须如下:void void 方法名方法名( (ObjectObject sender,NewMailEventArgs e); sender,NewMailEventArgs e);internal class MailManager internal class MailManager protected virtualprotected virtual v
9、oid void OnNewMail(NewMailEventArgs e) OnNewMail(NewMailEventArgs e) / / 为了线程安全,把为了线程安全,把delegate fielddelegate field存到临时变量存到临时变量 EventHandler temp = EventHandler temp = NewMail;NewMail; / / 如果有对象订阅了这个事件,则发出通知如果有对象订阅了这个事件,则发出通知 if (temp != null) temp( if (temp != null) temp(thisthis, e);, e); intern
10、al class MailManager internal class MailManager public void SimulateNewMail(String from, public void SimulateNewMail(String from, String to, String subject) String to, String subject) / / 构建一个事件信息对象类的实例构建一个事件信息对象类的实例 NewMailEventArgs e = new NewMailEventArgs e = new NewMailEventArgs(from, to, subjec
11、t);NewMailEventArgs(from, to, subject); / / 通知所有订阅了事件的对象通知所有订阅了事件的对象 OnNewMail(e);OnNewMail(e); / public event EventHandler NewMail/ public event EventHandler NewMail被被C#C#编译器翻译为以下编译器翻译为以下3 3部分:部分:/ 1. / 1. 初始为初始为nullnull的私有的私有delegate fielddelegate fieldprivate private EventHandlerEventHandler NewM
12、ail = null; NewMail = null;/ 2. / 2. 公有公有“add_add_事件名事件名”方法用来订阅方法用来订阅MethodImpl(MethodImplOptions.Synchronized)MethodImpl(MethodImplOptions.Synchronized)public void public void add_NewMailadd_NewMail(EventHandler (EventHandler value) value) NewMail = (EventHandler) NewMail = (EventHandler) Delegate.
13、 Delegate.CombineCombine(NewMail, value);(NewMail, value); / 3. / 3. 公有公有“remove_remove_事件名事件名”方法用来退订方法用来退订MethodImpl(MethodImplOptions.Synchronized)MethodImpl(MethodImplOptions.Synchronized)public void remove_NewMail(EventHandler public void remove_NewMail(EventHandler value) value) NewMail = (Even
14、tHandler) NewMail = (EventHandler) Delegate. Delegate.RemoveRemove(NewMail, value);(NewMail, value); internal sealed class QQ internal sealed class QQ public QQ(MailManager mm) public QQ(MailManager mm) /构造函数,进行订阅构造函数,进行订阅 mm.NewMail mm.NewMail +=+= QQMsg; QQMsg; / / 新邮件到来时要执行的方法新邮件到来时要执行的方法 private
15、 void QQMsg(Object sender,NewMailEventArgs e) private void QQMsg(Object sender,NewMailEventArgs e) / QQ/ QQ弹出消息并显示弹出消息并显示e.From, e.To, e.Subjecte.From, e.To, e.Subject等内容等内容 public void Unregister(MailManager mm) public void Unregister(MailManager mm) /退订退订 mm.NewMail mm.NewMail -=-= QQMsg; QQMsg; C
16、haractersChar c;Char c;Int32 n;Int32 n;c = c = (Char)(Char) 65; 65;Console.WriteLine(c); Console.WriteLine(c); / A/ An = n = (Int32)(Int32) c; c;Console.WriteLine(n); Console.WriteLine(n); / 65/ 65c = unchecked(Char) (65536 + 65);c = unchecked(Char) (65536 + 65);Console.WriteLine(c); Console.WriteLi
17、ne(c); / “A”/ “A”c = (c = (IConvertibleIConvertible) 65).ToChar(null);) 65).ToChar(null);Console.WriteLine(c); Console.WriteLine(c); / A/ An = (n = (IConvertibleIConvertible)c).ToInt32(null);)c).ToInt32(null);Console.WriteLine(n); Console.WriteLine(n); / 65/ 65Char c;Char c;Int32 n;Int32 n;c = c = C
18、onvertConvert.ToChar(65);.ToChar(65);Console.WriteLine(c); Console.WriteLine(c); / A/ An = n = ConvertConvert.ToInt32(c);.ToInt32(c);Console.WriteLine(n); Console.WriteLine(n); / 65/ 65try try / / 范围检查范围检查 c = Convert.ToChar(70000);c = Convert.ToChar(70000); Console.WriteLine(c); Console.WriteLine(c
19、); / / 异常异常 catch (OverflowException) catch (OverflowException) Console.WriteLine(“ Console.WriteLine(“无法转换无法转换.);.); String s1 = new String(“Hi there.”); String s1 = new String(“Hi there.”); / / 错误!错误!String s2 = “Hi there.”; String s2 = “Hi there.”; / / 正确正确string s3 = “Hinthere”; string s3 = “Hin
20、there”; /特殊字符的转义机制特殊字符的转义机制/建议用建议用System.Environment.NewLineSystem.Environment.NewLine属性替代转义的属性替代转义的rnrn,为什么?,为什么?string s4 = “Hi” + “ ” + “there”;string s4 = “Hi” + “ ” + “there”;String file1 = C:WindowsSystem32Notepad.exe;String file1 = C:WindowsSystem32Notepad.exe;/ / 逐字方式逐字方式verbatim stringverba
21、tim stringString file2 = C:WindowsSystem32Notepad.exe;String file2 = C:WindowsSystem32Notepad.exe;static Boolean static Boolean EqualsEquals(String a, String b,(String a, String b,StringComparison comparisonType)StringComparison comparisonType)static Int32 static Int32 CompareCompare(String strA, St
22、ring strB,(String strA, String strB,StringComparison comparisonType)StringComparison comparisonType)public enum public enum StringComparisonStringComparison CurrentCulture = 0, CurrentCulture = 0, CurrentCultureIgnoreCase = 1, CurrentCultureIgnoreCase = 1, InvariantCulture = 2, InvariantCulture = 2,
23、 InvariantCultureIgnoreCase = 3, InvariantCultureIgnoreCase = 3, Ordinal = 4, Ordinal = 4, OrdinalIgnoreCase = 5 OrdinalIgnoreCase = 5 /较快较快 StringBuilder StringBuilder strbstrb = new StringBuilder(); = new StringBuilder();String s = sString s = strtrb.AppendFormat(0 1, Jeffrey, b.AppendFormat(0 1,
24、Jeffrey, Richter).Replace( , -) .Remove(4, 3).Richter).Replace( , -) .Remove(4, 3).ToString()ToString(); ;Console.WriteLine(s);Console.WriteLine(s); / Jeff-Richter / Jeff-RichterString s = String s = String.FormatString.Format(On 0, 1 is 2 years old.,(On 0, 1 is 2 years old.,new DateTime(2012, 4, 22
25、, 14, 35, 5), Aidan, 3);new DateTime(2012, 4, 22, 14, 35, 5), Aidan, 3);Console.WriteLine(s);Console.WriteLine(s);/在简体中文文化下输出:在简体中文文化下输出:On On 2012-2012-4 4- -22 22 1414:35:05, Aidan is 3 :35:05, Aidan is 3 years old.years old.public static Int32 public static Int32 ParseParse(String s, NumberStyles
26、 style,(String s, NumberStyles style,IFormatProvider provider);IFormatProvider provider);Int32 x1 = Int32.Parse(123, NumberStyles.None, null);Int32 x1 = Int32.Parse(123, NumberStyles.None, null);Int32 x2 = Int32 x2 = Int32.Parse( 123, NumberStyles.AllowLeadingWhite, null);Int32.Parse( 123, NumberSty
27、les.AllowLeadingWhite, null);Unicode Transformation Formatusing System;using System;using System.Text;using System.Text;String s = “Hi there.”; String s = “Hi there.”; /待编码的字符串待编码的字符串Encoding encodingUTF8 = Encoding encodingUTF8 = Encoding.UTF8Encoding.UTF8; ;/ Encode a string into an array of bytes
28、./ Encode a string into an array of bytes.Byte encodedBytes = encodingUTF8.Byte encodedBytes = encodingUTF8.GetBytesGetBytes(s);(s);Console.WriteLine(Encoded bytes: +Console.WriteLine(Encoded bytes: +BitConverter.ToString(encodedBytes);BitConverter.ToString(encodedBytes);/输出:输出:Encoded bytes: 48-69-
29、20-74-68-65-72-65-2EEncoded bytes: 48-69-20-74-68-65-72-65-2E/ Decode the byte array back to a string./ Decode the byte array back to a string.String decodedString = String decodedString = encodingUTF8.encodingUTF8.GetStringGetString(encodedBytes);(encodedBytes);/ Show the decoded string./ Show the
30、decoded string.Console.WriteLine(Decoded string: + decodedString);Console.WriteLine(Decoded string: + decodedString);/输出输出Decoded string: Hi there.Decoded string: Hi ernal internal enumenum Color Color White,Red,Green,Blue White,Red,Green,Blue / / 值依次值依次0 0到到3 3 /C#/C#编译器把上述代码编译器把上述代码“当作当作”
31、以下代码以下代码internal struct Color : internal struct Color : System.EnumSystem.Enum public const Color White = public const Color White = (Color)(Color) 0; 0; public const Color Red = (Color) 1; public const Color Red = (Color) 1; public const Color Green = (Color) 2; public const Color Green = (Color) 2
32、; public const Color Blue = (Color) 3; public const Color Blue = (Color) 3; public Int32 value_; public Int32 value_; Color colors = (Color) Enum.Color colors = (Color) Enum.GetValuesGetValues(typeof(Color);(typeof(Color);Console.WriteLine(“Console.WriteLine(“元素个数元素个数: + colors.Length);: + colors.Le
33、ngth);foreach (Color c in colors) foreach (Color c in colors) / / 显示每个符号的数字格式和通用格式显示每个符号的数字格式和通用格式. . Console.WriteLine(0,5:Dt0:G, c); Console.WriteLine(0,5:Dt0:G, c); /输出输出: :元素个数元素个数: 4: 40 White0 White1 Red1 Red2 Green2 Green3 Blue3 BlueColor c = (Color) Enum.Color c = (Color) Enum.ParseParse(typ
34、eof(Color), “Blue, true);(typeof(Color), “Blue, true);/ c/ c被初始为被初始为3 3Color c1 = (Color)Enum.Color c1 = (Color)Enum.ParseParse(typeof(Color),Orange,true);(typeof(Color),Orange,true);/ / 未定义橙色,抛出异常未定义橙色,抛出异常Enum.TryParse(1, false, out c); Enum.TryParse(1, false, out c); / / 创建值为创建值为1 1的枚举实例的枚举实例Console.WriteLine(Enum.Console.WriteLine(Enum.IsDefinedIsDefined(typeof(
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年上海市黄浦区网格员招聘考试备考题库及答案解析
- 河南省许昌市禹州市2025-2026学年八年级下学期期中生物试卷(含答案)
- 常用输出设备教学设计中职专业课-计算机原理-计算机类-电子与信息大类
- 第15课 草字头教学设计小学书法练习指导四年级下册西泠版
- 初中英语人教新目标 (Go for it) 版八年级下册Section A第4课时教学设计
- 2026学年九年级道德与法治上册第三单元名校密卷单元测评含答案及解析
- 青岛版 (六三制)三年级下册四 绿色生态园-解决问题教学设计
- 2026杭州市富阳区卫健系统事业单位公开招聘36人笔试参考题库及答案解析
- 2026贵州遵义医科大学附属医院第十四届贵州人才博览会引进急需紧缺专业人才2人笔试模拟试题及答案解析
- 2026北京航空航天大学可靠性与系统工程学院聘用编科研助理F岗招聘1人考试备考题库及答案解析
- 2025年四川省从“五方面人员”中选拔乡镇领导班子成员考试历年参考题库含答案详解
- 2026高端航空装备技术创新中心(四川)有限公司春季社会招聘17人笔试历年参考题库附带答案详解
- GB/T 17498.6-2026室内固定式健身器材第6部分:跑步机附加的特殊安全要求和试验方法
- 2025市政院设计岗笔试试题及官方参考答案
- Costco开市客数据应用研究
- 2026宁夏农垦酒业有限公司社会招聘3人备考题库及答案详解(名校卷)
- 2026年考消控证试题及答案
- 高低压开关柜投标文件技术标
- 巾帼工作室工作制度
- 新高考教学教研联盟(长郡二十校)2026届高三年级4月第二次联考英语试卷(含答案详解)
- 基于组态王停车场智能监控方案介绍
评论
0/150
提交评论