//************************************************************
//
// foreach应用示例代码
//
// Author:三五月儿
//
// Date:2014/09/10
//
//
//************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
namespace IEnumerableExp
{
class Program
{
static void Main(string[] args)
{
List<Student> studentList = new List<Student>()
{
new Student(){Id = 1, Name = "三五月儿", Age = 23},
new Student(){Id = 2, Name = "张三丰", Age = 108},
new Student(){Id = 3, Name = "艾尔克森", Age = 25},
new Student(){Id = 3, Name = "穆里奇", Age = 27}
};
foreach (var student in studentList)
{
Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", student.Id,student.Name,student.Age);
}
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
IL_00c6: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<class IEnumerableExp.Student>::GetEnumerator() IL_00d1: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class IEnumerableExp.Student>::get_Current() IL_0102: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class IEnumerableExp.Student>::MoveNext()
IEnumerator<Student> studentEnumerator = studentList.GetEnumerator();
while (studentEnumerator.MoveNext())
{
var currentStudent = studentEnumerator.Current as Student;
Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", currentStudent.Id, currentStudent.Name, currentStudent.Age);
}
//************************************************************
//
// foreach应用示例代码
//
// Author:三五月儿
//
// Date:2014/09/10
//
//
//************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
namespace IEnumerableExp
{
class Program
{
static void Main(string[] args)
{
Student[] students = new Student[4]
{
new Student(){Id = 1, Name = "三五月儿", Age = 23},
new Student(){Id = 2, Name = "张三丰", Age = 108},
new Student(){Id = 3, Name = "艾尔克森", Age = 25},
new Student(){Id = 3, Name = "穆里奇", Age = 27}
};
StudentSet studentSet = new StudentSet(students);
foreach (var student in studentSet)
{
Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", student.Id, student.Name, student.Age);
}
}
}
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
public class StudentSet : IEnumerable
{
private Student[] students;
public StudentSet(Student[] inputStudents)
{
students = new Student[inputStudents.Length];
for (int i = 0; i < inputStudents.Length; i++)
{
students[i] = inputStudents[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return (IEnumerator)GetEnumerator();
}
public StudentEnumerator GetEnumerator()
{
return new StudentEnumerator(students);
}
}
public class StudentEnumerator : IEnumerator
{
public Student[] students;
int position = -1;
public StudentEnumerator(Student[] students)
{
this.students = students;
}
public bool MoveNext()
{
position++;
return (position < students.Length);
}
public void Reset()
{
position = -1;
}
object IEnumerator.Current
{
get
{
return Current;
}
}
public Student Current
{
get
{
try
{
return students[position];
}
catch (IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有