楼主: Lisrelchen
1061 3

【Marino Posadas】Mastering C# and .NET Framework [推广有奖]

  • 0关注
  • 62粉丝

VIP

已卖:4194份资源

院士

67%

还不是VIP/贵宾

-

TA的文库  其他...

Bayesian NewOccidental

Spatial Data Analysis

东西方数据挖掘

威望
0
论坛币
50288 个
通用积分
83.6906
学术水平
253 点
热心指数
300 点
信用等级
208 点
经验
41518 点
帖子
3256
精华
14
在线时间
766 小时
注册时间
2006-5-4
最后登录
2022-11-6

楼主
Lisrelchen 发表于 2017-1-29 11:33:09 |AI写论文
1论坛币
  • Mastering C# and .NET Framework
  • By: Marino Posadas

  • Publisher: Packt Publishing

  • Pub. Date: December 15, 2016

  • Web ISBN-13: 978-1-78588-540-2

  • Print ISBN-13: 978-1-78588-437-5

  • Pages in Print Edition: 560

  • Subscriber Rating: [0 Ratings]



关键词:Mastering Framework Master Ework Aster

沙发
Lisrelchen 发表于 2017-1-29 11:36:24
  1. Covariance in generic types
  2. Covariance can be extended to generic types and used with predefined delegates (remember, those delegates supplied by the Framework Factory that can be of types Action, Predicate, and Func).

  3. To place a simple code that shows this feature, observe the following declaration:

  4. IEnumerable<Func<Polygon>> dp =
  5.   new List<Func<Rectangle>>();
  6. Here, we're assigning a list of delegates of type Rectangle to an enumerable of delegates of type Polygon. This is possible because three characteristics play their role:

  7. Rectangle is assignable to Polygon for Substitution Principle
  8. Func<Rectangle> is assignable to Func<Polygon> due to covariance in the generic out T parameter of Func<T>
  9. Finally, IEnumerable<Func<Rectangle>> is assignable to IEnumerable<Func<Polygon>> due to a covariance extension over the generic type out T of IEnumerable
复制代码

藤椅
Lisrelchen 发表于 2017-1-29 11:37:20
  1. Covariance in interfaces
  2. Consider this code, where we change the defined polygons identifier as type IEnumerable<Polygon>:

  3. IEnumerable<Polygon> polygons2 =
  4.   new List<Triangle> {
  5.   new Triangle(), new Triangle()};
  6. This doesn't lead to a compilation error because the same ideas are applied to interfaces. To allow this, the generic parameter of interfaces such as IEnumerable<T> is used only as an out value. In such cases, it's interesting to take a look at the definition using the Peek Definition option (available on the editor's context menu for any type):
复制代码

板凳
Lisrelchen 发表于 2017-1-29 11:39:47
  1. Tuples: implementation in C#
  2. The implementation of tuples in .NET 4 is based on the definition (mscorlib.dll assembly and the System namespace) of eight generic classes Tuple<> with different number of type parameters to represent tuples of different cardinalities (it's also called arity).

  3. As a complement to this family of generic classes, eight overloads of the Create method in the Tuple class are provided, converting it into a factory of many possible variations of these types. In order to deliver resources for the creation of longer tuples, the eighth tuple in the list can also be a tuple itself, allowing it to grow as required.

  4. The following code shows the implementation of one of these methods. Thus, to create tuples, we can take advantage of a more concise notation and write this:

  5. Tuple.Create(1, 2, 3, 4, 5);
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群
GMT+8, 2026-1-30 09:38