当时是用foreach实现遍历,但是函数传入参数是Object类型的,由于Objectl类型没有实现相关接口,所以foreach并不能执行。

那么下面我们来看看,想要使用foreach需要具备什么条件。

需要实现IEnumerable接口或声明GetEnumerator方法的类型。

 下面我们来看看foreach原理,

参考原文  http://blog.csdn.net/guobin_lu/article/details/8812092

为什么有些类可以可以用foreach遍历,有些类却不可以了.经反编译过后得出:

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections; namespace Myforeach
{
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p[] = "宝马";
p[] = "奥迪";
p[] = "阿斯顿马丁";
//for (int i = 0; i < p.Count; i++)
//{
// Console.WriteLine(p[i]);
//} //任何类型,只要想使用foreach来循环遍历,就必须在当前类型中
//存在: public IEnumerator GetEnumerator()方法,(一般情况我们会通过实现IEnumerable接口,来创建该方法。)
foreach (string item in p)
{
Console.WriteLine(item);
} //IEnumerator etor = p.GetEnumerator();
//while (etor.MoveNext())
//{
// string str = etor.Current.ToString();
// Console.WriteLine(str);
//}
Console.ReadKey(); }
} public class Person : IEnumerable
{
private List<string> listCar = new List<string>(); public int Count
{
get
{
return this.listCar.Count;
} } public string this[int index]
{
get
{
return listCar[index];
} set
{
if (index >= listCar.Count)
{
listCar.Add(value);
}
else
{
listCar[index] = value;
}
}
}
public string Name
{
get;
set;
}
public int Age
{
get;
set;
}
public string Email
{
get;
set;
} #region IEnumerable 成员 //这个方法的作用不是用来遍历的,而是用来获取一个对象
//这个对象才是用来遍历的。
public IEnumerator GetEnumerator()
{
return new PersonEnumerator(listCar);
} #endregion
} //这个类型,的作用就是用来遍历Person中的List集合的。
public class PersonEnumerator : IEnumerator
{
public PersonEnumerator(List<string> _cars)
{
cars = _cars;
} //这个字段中存储的就是Person对象中的listCar集合
private List<string> cars; //假设一开始遍历的对象的索引是-1
private int index = -; #region IEnumerator 成员 //表示获取当前正在遍历的那个对象
public object Current
{
get
{
if (index < )
{
return null;
}
return cars[index];
}
}
//让自定义下标index累加
public bool MoveNext()
{
index = index + ;
if (index >= cars.Count)
{
return false;
}
else
{
return true;
}
} public void Reset()
{
index = -;
} #endregion
}
}

如果大家想要详细了解 foreach语句。建议大家学习一下迭代器。

参考网址

http://www.cnblogs.com/yangecnu/archive/2012/03/17/2402432.html

涉及 C#的 foreach问题的更多相关文章

  1. php中的foreach问题(1)

    前言 php4中引入了foreach结构,这是一种遍历数组的简单方式.相比传统的for循环,foreach能够更加便捷的获取键值对.在php5之前,foreach仅能用于数组:php5之后,利用for ...

  2. 深入解析php中的foreach问题

    本篇文章是对php中的foreach问题进行了详细的分析介绍,需要的朋友参考下   前言:php4中引入了foreach结构,这是一种遍历数组的简单方式.相比传统的for循环,foreach能够更加便 ...

  3. 【移动端兼容问题研究】javascript事件机制详解(涉及移动兼容)

    前言 这篇博客有点长,如果你是高手请您读一读,能对其中的一些误点提出来,以免我误人子弟,并且帮助我提高 如果你是javascript菜鸟,建议您好好读一读,真的理解下来会有不一样的收获 在下才疏学浅, ...

  4. Foreach遍历

    前天在项目中遇到一个问题,foreach遍历过程中修改responses中的对象,其中responses的类型:IEnumerable<Order>,代码如下: foreach (Orde ...

  5. Java语法糖1:可变长度参数以及foreach循环原理

    语法糖 接下来几篇文章要开启一个Java语法糖系列,所以首先讲讲什么是语法糖.语法糖是一种几乎每种语言或多或少都提供过的一些方便程序员开发代码的语法,它只是编译器实现的一些小把戏罢了,编译期间以特定的 ...

  6. MVC路由探寻,涉及路由的惯例、自定义片段变量、约束、生成链接和URL等

    引子 在了解MVC路由之前,必须了解的概念是"片段".片段是指除主机名和查询字符串以外的.以"/"分隔的各个部分.比如,在http://site.com/Hom ...

  7. foreach 相关

    20 Nov 08 深入理解PHP原理之foreach 作者: Laruence(   ) 本文地址: http://www.laruence.com/2008/11/20/630.html 转载请注 ...

  8. C# Winform 涉及的拖放操作总结

    在开发程序的时候,为了提高用户的使用体验,或满足相关用户的功能,总是离不开拖放功能.而本文是总结winform下的常用拖放操作.主要有 1.textbox接受拖放的文件2.listbox允许用户自定义 ...

  9. c#--foreach遍历的用法与split的用法

    一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素.      该循环 ...

随机推荐

  1. c++标准库中几个常见的数据结构的区别和应用规则

    转载自http://www.lifecrunch.biz/archives/202 vector和built-in数组类似,它拥有一段连续的内存空间,并且起始地址不变,因此它能非常好的支持随即存取,即 ...

  2. 在eclipse中配置一个简单的spring入门项目

    spring是一个很优秀的基于Java的轻量级开源框架,为了解决企业级应用的复杂性而创建的,spring不仅可用于服务器端开发,从简单性.可测试性和松耦合性的角度,任何java应用程序都可以利用这个思 ...

  3. Java for LeetCode 172 Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

  4. 【JAVA、C++】LeetCode 001 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  5. codeforces A. Domino 解题报告

    题目链接:http://codeforces.com/problemset/problem/353/A 题目意思:通俗地说,就是当上下两半的数的总和不完全是偶数时,通过上下调换某些骨牌来使这两半的数和 ...

  6. border-box

      box-sizing属性可以为三个值之一:content-box(default),border-box,padding-box. content-box,border和padding不计算入wi ...

  7. CSS对字体单位的总结

    国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的 ...

  8. Fedora 21 install chrome

    Steps to install Google Chrome on Fedora 21 A. Create google-chrome.repo file Use this command to cr ...

  9. 个人博客转移到CSDN

    因个人博客编辑的需要,现将我的博客转移到CSDN,如果没有特别需要,以后会在CSDN上继续书写博客 关于Fresco的分析以及后续的相关分析,也会转移到CSDN 个人CSDN主页: http://bl ...

  10. 使yum保留下载的rpm包

    [root@14LN yum]# egrep 'cachedir|keepcache' /etc/yum.conf #cachedir=/var/cache/yum/$basearch/$releas ...