c#中的循环结构

while循环:先判断在循环

int i = 0;
while (i <= 10)
{
    if (i == 5)
    {
        i++;
        continue;//结束本次循环,进入下一次循环,
    }
    else if (i == 9)
    {
        break;//跳出整个循环
    }
    Console.Write(i + "\t");
    i++;//不自增加一,就会变成0的一直死循环。
}

do...while循环:先执行一遍在执行判断

int i;//整型有个默认值为0,所以不用赋值

do
{
    Console.WriteLine("请输入密码:");
    i = Convert.ToInt32(Console.ReadLine());//接收用户输入的密码,接收的是字符串需要强转为整型
} while (i != 0); //如果输入的不是0就继续循环
Console.WriteLine("登陆成功");

for循环:99乘法表

for (int i = 1; i <= 9; i++) //for ( ; ; ) 也可以类似这样写,条件写入方法体
{
    for (int j = 1; j <= i; j++)
    {
        Console.Write("{0}*{1}={2}\t", j, i, i * j);
    }
    Console.WriteLine();
}

foreach循环:用于遍历数组/集合对象。

List<string> ilist = new List<string>() { "陈一", "小二", "张三", "李四", "王五", "赵六" };
foreach (string itme in ilist) 
{
    Console.WriteLine(itme); //把数组每个值赋值给变量,直到循环数组最后一个数据才结束
}

 foreach内部实现原理:需要继承IEnumerable枚举类型去实现其迭代器IEnumerator,通过yield关键字返回。

using System.Collections;//IEnumerable先引入命名空间在实现接口

int i = 0;//不支持遍历
string str = "张三,你好!";//支持遍历,因为string类型继承了IEnumerable接口。
Student stu = new Student() { id = 22 };//正常来说对象是不能直接遍历的,没有迭代效果,需要实现IEnumerable接口后就可以遍历了
foreach (var item in stu)
{ 
    Console.WriteLine(item);
}

class Student : IEnumerable //可迭代类型,也是枚举类型,foreach遍历都必须实现这个接口。
{
    public int id { get; set; }

    public IEnumerator GetEnumerator() //接口只有一个方法,返回的是一个接口IEnumerator迭代器,实现这个方法后就支持了迭代效果。
    {
        yield return "正常return只能返回一个值。";
        yield return "关键字:yield,可以返回多个值。";
        yield return "封装了IEnumerator接口的实现代码";
        yield return this.id;
    }
}

关键字:yield,是返回IEnumerator,其实就是c#给我们封装好了对IEnumerator迭代器的一个内部实现,转到定义f12查看需要实现3个方法。实现原理如下:

using System.Collections;//IEnumerable先引入命名空间在实现接口

Student stu = new Student();//正常来说对象是不能直接遍历的,没有迭代效果,需要实现IEnumerable接口后就可以遍历了
foreach (var item in stu)
{ 
    Console.WriteLine(item);
}

class Student : IEnumerable //可迭代类型,也是枚举类型,foreach遍历都必须实现这个接口。
{
    string[] str = { "陈一", "小二", "张三", "李四", "王五", "赵六" };//数组本来就支持遍历,这里拿做演示而已,懒得封装字段。
    public IEnumerator GetEnumerator() //接口只有一个方法,返回的是一个接口IEnumerator迭代器,实现这个方法后就支持了迭代效果。
    {
        return new StuIEnumerator(str);//C#把这个遍历方法封装成了关键字:yield
    }
}

class StuIEnumerator : IEnumerator //迭代器:演示yield内部封装的一个实现原理
{
    string[] str;
    int index = -1;//记录数组的一个下标

    public StuIEnumerator(string[] _str)
    { 
        this.str = _str;//构造方法给数组赋初始值
    }
    public object Current //IEnumerator返回的字段
    { 
        get 
        {
            if (index == -1) throw new InvalidOperationException("验证操作异常");
            if (index >= str.Length) throw new InvalidOperationException("索引超出范围长度");
            return str[index];
        }
    }

    public bool MoveNext()//循环判断,就是让操作推近下一步。
    {
        if (index < str.Length - 1)
        {
            index++;
            return true;
        }
        else
        {
            return false;
        }
    }

    public void Reset()//重置,回到原始状态
    {
        index = -1;
    }
}

 

posted @ 2021-02-10 23:31  Akai_啊凯  阅读(206)  评论(0编辑  收藏  举报