zoukankan      html  css  js  c++  java
  • C# 获取 IEnumerable 集合的个数

               IEnumerable<Attribute> keys = p.GetCustomAttributes().ToList();

    var data1 = data.Where(n => n.Name.Contains(search)).ToList();
      if (data1.Count == 0)

     //MSDN例子

    using System;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace Exceltest
    {
        public partial class Form3 : Form
        {
            public Form3()
            {
                InitializeComponent();
            }
    
            private void Form3_Load(object sender, EventArgs e)
            {
                  MessageBox.Show("000");
                Person[] peopleArray = new Person[3]
            {
                new Person("John", "Smith"),
                new Person("Jim", "Johnson"),
                new Person("Sue", "Rabon"),
            };
                var ss = new People(peopleArray); //创建一个对象,这个对象_people属性是一个数组
    
                People peopleList = new People(peopleArray);
                foreach (Person p in peopleList)  //如果不集成IEnumerable,对象将无法使用foreach
                    Console.WriteLine(p.firstName + " " + p.lastName);
            }
        }
    
        public class Person //人,个人
        {
            public Person(string fName, string lName)
            {
                this.firstName = fName;
                this.lastName = lName;
            } //构造方法
            public string firstName;
            public string lastName;
        }
    
        public class People : IEnumerable //人,大家
        {
            private Person[] _people; //定义一个个人数组
    
            public People(Person[] pArray) //构造函数需要传递过来一个数组 /////数组传递到people中
            {
                _people = new Person[pArray.Length]; //初始化数组的范围,
    
                for (int i = 0; i < pArray.Length; i++) 
                {
                    _people[i] = pArray[i]; 
                }
            }
    
            IEnumerator IEnumerable.GetEnumerator() // 集成必须要实现该接口
            {
                PeopleEnum ss = new PeopleEnum(_people);
    
                return new PeopleEnum(_people); //返回
            }
        }
    
        public class PeopleEnum : IEnumerator
        {
            public Person[] _people;
    
            // Enumerators are positioned before the first element
            // until the first MoveNext() call.
            int position = -1;
    
            public PeopleEnum(Person[] list)
            {
                _people = list;
            }
    
            public bool MoveNext()
            {
                position++;
                return (position < _people.Length);
            }
    
            public void Reset()
            {
                position = -1;
            }
    
            public object Current
            {
                get
                {
                    try
                    {
                        return _people[position];
                    }
                    catch (IndexOutOfRangeException)
                    {
                        throw new InvalidOperationException();
                    }
                }
            }
        }
    }
  • 相关阅读:
    准备使用 Office 365 中国版--邮箱迁移
    准备使用 Office 365 中国版--域名
    中国版的 Office 365
    了解 Office 365
    MySQL Database on Azure 参数设置
    MySQL Database on Azure 的用户名
    Android NOTE
    从源码看Android中sqlite是怎么通过cursorwindow读DB的
    一个由proguard与fastJson引起的血案
    【转载】淘宝架构框架发展史
  • 原文地址:https://www.cnblogs.com/enych/p/9322117.html
Copyright © 2011-2022 走看看