zoukankan      html  css  js  c++  java
  • 泛型和数组

    在 C# 2.0 中,下限为零的一维数组自动实现 IList<T>。这使您可以创建能够使用相同代码循环访问数组和其他集合类型的泛型方法。此技术主要对读取集合中的数据很有用。IList<T> 接口不能用于在数组中添加或移除元素;如果试图在此上下文中调用 IList<T> 方法(如数组的 RemoveAt),将引发异常。

    下面的代码示例演示带有 IList<T> 输入参数的单个泛型方法如何同时循环访问列表和数组,本例中为整数数组。

    class Program2
        
    {
            
    static void Main()
            
    {
                
    int[] arr = 01234 };
                List
    <int> list = new List<int>();

                
    for (int x = 5; x < 10; x++)
                
    {
                    list.Add(x);
                    list.Remove(
    2);
                }


                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();
            }

        }
  • 相关阅读:
    gitlab备份及迁移
    python paramiko 进行文件上传处理
    秒杀场景简介
    nmon--非常棒的LINUX/AIX性能计数器监测和分析工具
    使用wait()与notify()实现线程间协作
    【转】Spring bean处理——回调函数
    ldconfig和ldd用法
    tcpdump 获取http请求url
    clearfix清除浮动
    git push命令
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/961751.html
Copyright © 2011-2022 走看看