第三章 C#基础(二).ppt

  1. 1、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。。
  2. 2、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  3. 3、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
第三章 C#基础(二)

所有类型和类型成员都具有可访问性级别,用来控制是否可以在您程序集的其他代码中或其他程序集中使用它们。VS2010中修改了protected internal的定义 * * 演示 Notice that ToString is not explicitly used in the program. It is invoked by default by the WriteLine calls. * * * 让学员手写 * 回顾时,可以让学员自己说一个方法的例子,再引入方法定义的形式。 Void, 要么没有return,要么只有不带任何参数的return; * * 参数值传递 * 通过一个值传递无法实现的功能,引出按引用传递参数 * 常见错误:调用时不使用ref,可以给学员看一下错误信息 x=15,y=55 * The main difference between the ref and the out keywords is that, if you pass an argument with out, you must initialize it (give it a value) in the method, whether you will use the argument in the method or not out 关键字会导致参数通过引用来传递。这与 ref 关键字类似,不同之处在于 ref 要求变量必须在传递之前进行初始化,或者说out必须在方法中初始化。 * * * * 委托与事件 委托 (1)声明委托 [attributes] [delegate-modifiers] delegate result-type indentifier([formal-parameter-list]) 其中: 1)attributes:表示属性,可选项,往往可以省略。 2)delegate-modifiers:修饰符,在C#中委托可有new、public、protected、internal、private等修饰符。其中new修饰符表示子类委托可以实现对父类委托的重载,其他4个修饰符表示委托的访问级别。 3)delegate:声明委托的关键字。 4)result-type:返回数据的类型。 5)indertifier:由合法标识符组成的委托名称。 6)formal-parameter-list:委托的参数列表,可以省略。 1)delegate int d(); //声明指向int类型无参数函数的委托 2)delegate int d(int i); //具有参数的委托声明 3)public delegate int(int i); //具有修饰符的委托声明 4)[A(“temp”)]delegate int d(int i); //具有属性的委托声明 事件是类的成员,事件为类和类的实例提供了向外界发送通知的能力。定义事件要使用委托(delegate)类型。 委托的例子 namespace Delegate_Test { delegate double MathAction(double num); // Declare delegate class DelegateTest { static double Double(double input) // Regular method that matches signature: { return input * 2; } static void Main() { // Instantiate delegate with named method: MathAction ma = Double; // Invoke delegate ma: double multByTwo = ma(4.5); Console.WriteLine(multByTwo); // Instantiate delegate with anonymous method: MathAction ma2 = delegate(double input) { return input * input; }; double square = ma2(5)

文档评论(0)

2266670 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档