当前位置: 移动技术网 > IT编程>开发语言>c# > C# 泛型数组学习小结

C# 泛型数组学习小结

2019年07月18日  | 移动技术网IT编程  | 我要评论
c# 泛型和数组在 c# 2.0 中,下限为零的一维数组自动实现 ilist<t>。这使您可以创建能够使用相同代码循环访问数组和其他集合类型的泛型方法。此技术主要对读取集合中的数据很有用。ilist<t> 接口不能用于在数组中添加或移除元素;如果试图在此上下文中调用 ilist<t> 方法(如数组的 removeat),将引发异常。下面的代码示例演示带有 ilist<t> 输入参数的单个泛型方法如何同时循环访问列表和数组,本例中为整数数组。

c# 泛型和数组代码

复制代码 代码如下:

class program
{
static void main()
{
int[] arr = { 0, 1, 2, 3, 4 };
list<int> list = new list<int>();
for (int x = 5; x < 10; x++)
{
list.add(x);
}
processitems<int>(arr);
processitems<int>(list);
}
static void processitems<t>(ilist<t> coll)
{
foreach (t item in coll)
{
system.console.write(item.tostring() + " ");
}
system.console.writeline();
}
}


c# 泛型和数组应用时注意

尽管 processitems 方法无法添加或移除项,但对于 processitems 内部的 t[],isreadonly 属性返回 false,因为该数组本身未声明 readonly 特性。

c# 泛型和数组的相关内容就向你介绍到这里,希望对你了解和学习c# 泛型和数组有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网