CSharp OOP - Generics

/*

Author: Jiangong SUN

*/

Generics is introduced by .NET Framework 2.0, it's most commonly used with collections and the methods that operate on them. 

Namespace: System.Collections.Generic


There are two forms of generics in C#: generic types (including classes, interfaces, delegates, and structures—there are no generic enums) and generic methods.

In most cases, it is recommended that you use the List<T> class provided by the .NET Framework class library, rather than create your own.


If you ever need to describe a generic type to a colleague, it’s conventional to use “of” to introduce the type parameters or arguments—so List<T> is pronounced “list of T”.


Generic types can effectively be overloaded on the number of type parameters—so you could define MyType, MyType<T>, MyType<T,U>, MyType<T,U,V>, and so forth, all
within the same namespace.


Generic List vs. ArrayList:

- ArrayList will box all value types to Object when adding to it, and unbox Object to value types when retrieving from it, which will reduce the performance.

- ArrayList cast all types of data to Object, and it doesn't check errors in compile time.

See my post for the comparison



Generic Class:


Generic Interface:

Generic Delegates:

Generic Methods:




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值