DotNet技术培训.ppt

上传人:caoka****i456 文档编号:154556443 上传时间:2020-12-07 格式:PPT 页数:52 大小:263.50KB
返回 下载 相关 举报
DotNet技术培训.ppt_第1页
第1页 / 共52页
DotNet技术培训.ppt_第2页
第2页 / 共52页
DotNet技术培训.ppt_第3页
第3页 / 共52页
DotNet技术培训.ppt_第4页
第4页 / 共52页
DotNet技术培训.ppt_第5页
第5页 / 共52页
点击查看更多>>
资源描述

《DotNet技术培训.ppt》由会员分享,可在线阅读,更多相关《DotNet技术培训.ppt(52页珍藏版)》请在金锄头文库上搜索。

1、第4章 C#高级特性,异常,调试,唐大仕 ,本章内容,4.1 程序的组织 4.2 委托与事件 4.3 操作符重载 4.4 异常处理 4.5 Attribute 4.6 C#语言中的其他成分 4.7 程序的调试,4.1 程序的组织,名字空间程序的逻辑组织 嵌套类型类中嵌套类型 程序集程序的物理组织,名字空间,1名字空间的概念 逻辑划分;避免名字冲突 2名字空间的声明 namespace xxx.xxxx 可嵌套 3名字空间的导入 using xxx.xxxx; 4使用别名 using 别名 = 名字空间或类名;,NamespaceUsing.cs,嵌套类型,嵌套类型的概念 类型中的类型 clas

2、s A public class B public struct C new A.B.C(); 嵌套类型的可访问性 受各个层次的限制,NestedAccessibility.cs,程序集,模块(module) 程序集(assembly) 在VS.NET上引用程序集 在项目上点右键,添加引用,csc /target:mod /out:Add.mod Add.cs csc /target:mod /out:Multi.mod Multi.cs al /target:library /out: MyLibrary.dll Add.mod Multi.mod csc /target:exe /out:

3、MyClient.exe /reference:MyLibrary.dll MyClient.cs,4.2 委托与事件,大致上: 委托-函数指针 事件-回调函数,委托,委托的声明 public delegate double MyDelegate ( double x ); 委托的实例化 MyDelegated d2 = new MyDelegate( obj.myMethod ); 委托的调用 委托变量名(参数列表 ) d2(8.9),DelegateIntegral.cs,委托示例,DelegatePlotFun.cs,委托的合并,委托的合并-多播MultiCastDelegate 一个委

4、托实例中可以“包含”多个函数 调用委托,就是调用其中多个函数 多个函数间的先后顺序是没有意义的 运算符 + - += -= 动态地增减其中的函数 提高了程序的灵活性,DelegateMultiTest.cs,Delegate温度.cs,委托的转换与相等,委托的转换 按声明的名称判断 以下两个不能互相转换或加减 delegate void D( int a ); delegate void E( int a ); 委托的相等 按内容(即其中“包含的函数”)来判断,DelegateEquals.cs,事件,事件的声明 public event 委托名 事件名; 事件的注册与移除 事件名 += 或

5、-= 在事件所在类的外面,只能用以上两个运算符 事件的发生 事件名(参数列表) 相当于回调所注册的函数,事件与委托的关系,事件有点像委托类型的实例 事件一定有相关的委托类型 与委托实例一样,事件也“包含”多个函数 事件的运算符受更多限制(+=或-=) 事件比委托实例更复杂:事件存取器 修饰符 event 委托类型名 事件名 add e += value; remove e -= value; ,EventButtonForm.cs,使用事件的例子,网络爬虫程序中通知调用者下载开始、结束及下载进度,EventWhenDownload.cs,4.3 操作符重载,操作符重载的概念,操作符有时比方法名

6、更直观 如 两个复数用 a+b 比 a.Add(b)更直观 但要慎用 操作符重载有一些限制 如成对,如类型要求,如有的不能重载 更详细的内容,请参见文档,操作符的声明,一元操作符声明的形式如下: public static 类型 operator 一元操作符 ( 类型 参数名 ) 二元操作符声明的形式如下: public static 类型 operator 二元操作符 ( 类型 参数名,类型 参数名) 类型转换操作符声明的形式如下: public static implicit operator 类型 ( 类型 参数名) public static explicit operator 类型

7、( 类型 参数名) ,OperatorComplex.cs,4.4 异常处理,异常处理,异常可以以两种不同的方式引发: throw语句无条件,即时的抛出异常。 C#语句和表达式执行过程中激发了某个异常的条件,使得操作无法正常结束,从而引发异常。例如整数除法操作分母为零时将抛出一个异常。 首先介绍一下throw语句。throw语句抛出一个异常的语法为: throw expression 带有表达式的throw语句抛出的异常是在计算这个表达式时产生的。异常由try语句来处理的 try语句提供了一种机制来捕捉执行过程中发生的异常。以下是它的三种可能的形式: try-catch try-finally

8、 try-catch-finally,异常的概念,C#中的异常处理 try catch(Exception e) finally System.Exception类 public Exception(); public Exception(string s); Message属性 StackTrace属性,几种常用的异常类,System.OutOfMemoryException System.StackOverflowException System.NullReferenceException System.TypeInitializationException System.Invalid

9、CastException System.ArrayTypeMismatchException System.IndexOutOfRangeException System.MulticastNotSupportedException System.ArithmeticException System.DivideByZeroException System.OverflowException,捕获和处理异常,抛出异常 throw new SomeException(); 捕获异常 try catch(AException e1) catch(BException e2) catch(更一般的

10、Exception e) finally 注:catch表示捕获所有种类的异常,ExceptionIndexOutOf.cs,ExceptionSimple.cs,创建用户自定义异常类,从Exception或ApplicationException继承 重抛异常 throws; 异常链接 throw new Excepiton( “msg”, e ); 这里e称为内部异常 InnerException属性 使得外部能进一步知道内部的异常原因,ExceptionInner.cs,算术溢出与checked,对溢出进行检查 对整个程序 csc /checked XXXX.cs 对部分程序 针对表达式

11、: checked(表达式) 及 uncheckd(表达式) 针对块语句: checked 及 uncheckd 对溢出异常进行捕获 try catch( OverflowException e ) ,CheckedTest.cs,4.5 Attribute,Attribute,HelpUrl(“http:/SomeUrl/APIDocs/SomeClass”) class SomeClass WebMethod void GetCustomers() string Test(SomeAttr string param1) ,Attribute是与类、结构、方法等元素相关的额外信息, 是对元信

12、息的扩展。 通过Attribute可以使程序、甚至语言本身的功能得到增强。,使用系统定义的Attribute,使用Attribute的一般方式 在程序集、类、域、方法等前面用表示 可以省略“Attribute”几个字母,只写xxxxx 可以带参数 位置参数 (相当于构造方法带的参数) 命名参数(域名或属性名=值) 示例 在Main()方法使用STAThread 使用“过时”:AttributeObsolete.cs 使用“条件”:AttributeConditional.cs 在结构上、枚举上使用:StructLayout,Flag 在程序集级别应用Attribute assembly: As

13、semblyCompany(),自定义Attribute,1声明Attribute类 2使用Attribute类 3通过反射访问属性,AttributeHelp.cs,Attributes are classes Attached to types and members Attributes can be queried at runtime,class HelpUrlAttribute : System.Attribute public HelpUrlAttribute(string url) ,HelpUrl(“http:/SomeUrl/APIDocs/SomeClass”) clas

14、s SomeClass ,Type type = Type.GetType(“SomeClass”); object attributes = type .GetCustomAttributes();,4.6 C#语言中的其他成分,编译预处理,1标识符声明 #define定义一个标识符; #undef“取消定义”一个标识符。 2条件处理 #if, #elif, #else, #endif 3信息报告 #error 和 #warning 4行号标记 #line 行号 文件名,unsafe及指针,1unsafe 用于修饰类、方法等 2fixed及指针 fixed( 类型 * 指针名 = 表达式 )

15、 语句 3sizeof运算符 sizeof( 简单或结构类型名 ) 4stackalloc 在栈上分配的内存,而不是在堆上,因此不会担心内存被垃圾回收器自动回收。,UnsafeCopy.cs,C# And Pointers,Developers sometime need total control Performance extremes Dealing with existing binary structures Advanced COM Support, DLL Import C# “unsafe” = a limited “inline C” Pointer types, point

16、er arithmetic Unsafe casts Declarative pinning (fixed statement) C# developers have headroom,Unsafe Example,class FileStream: Stream int handle; dllimport(kernel32, SetLastError=true) static extern unsafe bool ReadFile(int hFile, void* lpBuffer, int nBytesToRead, int* nBytesRead, Overlapped* lpOverlapped); public unsafe int Read(byte buffer, int index, int count) int n = 0; fixed (byte* p = buffer) ReadFile(handle, p + index, count, ,其他关

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 企业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号