#读书笔记#Illustrated C# 2012第17章 Generics泛型(2)

发布时间:2020-06-24 12:23:38 作者:lsieun
来源:网络 阅读:520

=================

4、泛型方法Generic Methods 

Unlike the other generics, a method is not a type but a member. You can declare generic methods in both generic and nongeneric classes, and in structs and interfaces.

#读书笔记#Illustrated C# 2012第17章 Generics泛型(2)

4.1、Declaring a Generic Method

Generic methods have a type parameter list and optional constraints. 

 Generic methods have two parameter lists: 

 To declare a generic method, do the following: 

Place the type parameter list immediately after the method name and before the method parameter list

Place any constraint clauses after the method parameter list.

#读书笔记#Illustrated C# 2012第17章 Generics泛型(2)

Note:Remember that the type parameter list goes after the method name and before the method parameter list.


4.2、Invoking a Generic Method 

To invoke a generic method, supply type arguments with the method invocation

#读书笔记#Illustrated C# 2012第17章 Generics泛型(2)

4.3、Extension Methods with Generic Classes 

Extension methods work just as well with generic classes. They allow you to associate a static method in one class with a different generic class and to invoke the method as if it were an instance method on a constructed instance of the class. 


As with nongeneric classes, an extension method for a generic class must satisfy the following constraints: 

  1.  It must be declared static. (方法是静态的)

  2.  It must be the member of a static class. (类是静态)

  3.  It must contain as its first parameter type the keyword this, followed by the name of the generic class it extends. 


=================

5、泛型结构Generic Structs

Like generic classes, generic structs can have type parameters and constraints. The rules and conditions for generic structs are the same as those for generic classes. 


=================

6、泛型委托Generic Delegates 

Generic delegates are very much like nongeneric delegates, except that the type parameters determine the characteristics of what methods will be accepted. 

 To declare a generic delegate, place the type parameter list in angle brackets after the delegate name and before the delegate parameter list.(位置)

 

 Notice that there are two parameter lists: the delegate formal parameter list and the type parameter list. (两处parameter list)

 The scope of the type parameters includes the following: 

 The return type 

 The formal parameter list 

 The constraint clauses


=================

7、泛型接口Generic Interfaces 

Generic interfaces allow you to write interfaces where the formal parameters and return types of interface members are generic type parameters. Generic interface declarations are similar to nongeneric interface declarations, but have the type parameter list in angle brackets after the interface name. 


generic type declaration + type arguments --> a constructed type


推荐阅读:
  1. Linux性能优化读书笔记01
  2. MCSE笔记 Web Farm与NLB

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

rat 读书笔

上一篇:HMC 从V7R3.3.0升级到V7R7.4.0、VAC/VACPP Runtime Environment下载安装

下一篇:OSI、MAC地址以及路由器的原理和配置方式

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》