版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 技术管理岗述职报告(范文3篇)
- 财务运营述职报告(3篇)
- 2025年考研西医综合真题及参考答案
- 2026年新发传染病或流行病的防控知识试题及答案
- 国家林业局高级花卉园艺师试题库及答案
- 2026年四川省专业技术人员公需科目培训试题答案
- 2026年低轨卫星星座建设:星间链路技术创新与通信延迟优化方案
- 2026年骑行服运动心率区间提醒
- 危险源辨识的方法、程序与内容培训
- 公司监理人员工作纪律培训
- 2026年泌尿外科出科试卷及答案
- 2026小学数学北师大版新教材培训:四至六年级教材解析
- AI原生数据平台研究报告(2026年)(2026.6)
- 2026年成都玉林紫荆初一入学数学分班考试真题含答案
- 工程预应力张拉与灌浆质量控制措施
- 一氧化二氮的理化性质与危险特性培训
- 2026年江苏省安全员C2证(土建安全员)考试题库及答案
- DB11-T 1610-2026 民用建筑信息模型深化设计建模细度标准
- 保安员监控岗工作制度
- (正式版)DB36∕T 1297-2020 《城市消防物联网大数据应用平台物联设施设备接口规范》
- GB/Z 46984.3-2026光伏电池第3部分:双面光伏电池电流-电压特性的测量
评论
0/150
提交评论