- using System;
- using System.Collections;
- using System.Linq;
- using System.Text;
- namespace 接口实现01
- {
- public class User
- {
- private string UserName;
- public string UserName_
- {
- get { return UserName; }
- set { UserName = value; }
- }
- private string Password;
- public string Password_
- {
- get { return Password; }
- set { Password = value; }
- }
- /// <summary>
- /// 构造函数:
- /// </summary>
- /// <param name="userName"></param>
- /// <param name="passWd"></param>
- public User(string userName, string passWd)
- {
- UserName = userName;
- Password = passWd;
- }
- }
- class UserArray:IEnumerator
- {
- User[] user;
- int postion = -1;
- public UserArray(User[] use)
- {
- user=new User[use.Length];
- for(int i=0;i<use.Length;i++)
- user[i]=use[i];
- }
- #region IEnumerator 成员
- object IEnumerator.Current
- {
- get { return user[postion]; }
- }
- bool IEnumerator.MoveNext()
- {
- if (postion < user.Length-1)
- { postion++; return true; }
- else
- return false;
- }
- void IEnumerator.Reset()
- {
- postion=-1;
- }
- #endregion
- }//end of UserArray
- class UserArr : IEnumerable
- {
- User[] user;
- public UserArr(User[] u)
- {
- user=new User[u.Length];
- for(int i=0;i<u.Length;i++)
- user[i++]=u[i];
- }
- #region IEnumerable 成员
- public IEnumerator GetEnumerator()
- {
- return(new UserArray(user));
- }
- #endregion
- #region IEnumerable 成员
- IEnumerator IEnumerable.GetEnumerator()
- {
- return (new UserArray(user));
- }
- #endregion
- }
- class Test
- {
- public static Main()
- {
- User[] user = new User[] {
- new User("sjb","200888"),
- new User("guo","qazwsx")
- };
- UserArr userArray = new UserArr(user);
- foreach (var a in userArray)
- {
- if(a!=null)
- Console.WriteLine("{0}",a.GetType().Name);
- }
- System.Threading.Thread.Sleep(5000);
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/0810201513740.html
来源: http://www.codesnippet.cn/detail/0810201513740.html