国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

Generics in C#

2024-07-21 02:18:57
字體:
來源:轉載
供稿:網友
generics in c#


generics are the most useful c# 2.0 language extensions, beside anonymous methods, iterators, partial types and nullable types.

what are generics?
generics permit classes, structs, interfaces, delegates, and methods to be parameterized by the types of data they store and manipulate.

why generics?
to well know the useful of generics lets examine the following code:--
public class stack
{
object[] items;
int count;
public void push(object item) {...}
public object pop() {...}
}

we use the object type to store any type of data. the above simple stack class stores its data in an object array, and its two methods, push and pop, use object to accept and return data. while the use of type object makes the stack class very flexible, it is not without drawbacks. for example, it is possible to push a value of any type, such a customer instance, onto a stack.

however, when a value is retrieved, the result of the pop method must explicitly be cast back to the appropriate type, which is tedious to write and carries a performance penalty for run-time type checking:
stack stack = new stack();
stack.push(new customer());
customer c = (customer)stack.pop();

if a value of a value type, such as an int, is passed to the push method, it is automatically boxed. when the int is later retrieved, it must be unboxed with an explicit type cast:

stack stack = new stack();
stack.push(3);
int i = (int)stack.pop();

such boxing and unboxing operations add performance overhead since they involve dynamic memory allocations and run-time type checks.

a further issue with the stack class is that it is not possible to enforce the kind of data placed on a stack. indeed, a customer instance can be pushed on a stack and then accidentally cast it to the wrong type after it is retrieved:

stack stack = new stack();
stack.push(new customer());
string s = (string)stack.pop();

while the code above is an improper use of the stack class, the code is technically speaking correct and a compile-time error is not reported. the problem does not become apparent until the code is executed, at which point an invalidcastexception is thrown.

with generics those problems are all solved. how ??

public class stack<t>
{
t[] items;
int count;
public void push(t item) {...}
public t pop() {...}
}

when the generic class stack<t> is used, the actual type to substitute for t is specified. in the following example, int is given as the type argument for t:

stack<int> stack = new stack<int>();
stack.push(3);
int x = stack.pop();



the stack<int> type is called a constructed type. in the stack<int> type, every occurrence of t is replaced with the type argument int. when an instance of stack<int> is created, the native storage of the items array is an int[] rather than object[], providing substantial storage efficiency compared to the non-generic stack. likewise, the push and pop methods of a stack<int> operate on int values, making it a compile-time error to push values of other types onto the stack, and eliminating the need to explicitly cast values back to their original type when theye retrieved.

generics provide strong typing, meaning for example that it is an error to push an int onto a stack of customer objects. just as a stack<int> is restricted to operate only on int values, so is stack<customer> restricted to customer objects, and the compiler will report errors on the last two lines of the following example:

stack<customer> stack = new stack<customer>();
stack.push(new customer());
customer c = stack.pop();
stack.push(3); // type mismatch error
int x = stack.pop(); // type mismatch error

it was a breif introduction to generics that will be included in the next version of c# (v 2.0) . which is available now on its beta version with visual c# 2005 express edition beta 1.



商業源碼熱門下載www.html.org.cn

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 宜宾县| 兴城市| 沁源县| 调兵山市| 咸丰县| 道真| 岐山县| 荥经县| 布尔津县| 周至县| 霍林郭勒市| 神木县| 双辽市| 伊通| 上蔡县| 且末县| 仁怀市| 宜川县| 镇沅| 涿州市| 綦江县| 衡水市| 台南县| 左贡县| 郴州市| 河北区| 增城市| 景泰县| 龙山县| 曲水县| 林州市| 三明市| 贵州省| 如皋市| 鲜城| 都昌县| 濮阳市| 霍林郭勒市| 大城县| 分宜县| 红桥区|