• yield关键字的用法


    在上一篇文章中,说了下foreach的用法,但是还是比较复杂的,要实现接口才能进行遍历,有没有简单些的方法呢?答案是肯定的。且看下面。

    yield关键字的用法:

    1.为当前类型添加一个任意方法,但是要求该方法的返回值类型必须是IEnumerable:<代码1-1>

     1     class Person
     2     {
     3         public string Name { get; set; }
     4         public int Age { get; set; }
     5 
     6         public string[] _Name = new string[] { "zxh", "jk", "ml", "wcw", "sk", "yzk" };
     7 
     8         public IEnumerable<string> GetEnumerableObject()
     9         {
    10             for (int i = 0; i < _Name.Length; i++)
    11             {
    12                 yield return _Name[i];
    13             }
    14         }
    15     }

    遍历的方法如下:<代码1-2>

    1            Person p1 = new Person();
    2             foreach (var item in p1.GetEnumerableObject())
    3             {
    4                 Console.WriteLine(item);
    5             }
    6             Console.ReadKey();

    2.为当前类型添加一个GetEnumerator()方法,返回值类型是IEnumerator.<代码1-3>

     1    class Person
     2     {
     3         public string Name { get; set; }
     4         public int Age { get; set; }
     5 
     6         public string[] _Name = new string[] { "zxh", "jk", "ml", "wcw", "sk", "yzk" };
     7 
     8         //为当前类型添加一个GetEnumerator()方法,返回值类型是IEnumerator.
     9         public IEnumerator<string> GetEnumerator()
    10         {
    11             for (int i = 0; i < _Name.Length; i++)
    12             {
    13                 yield return _Name[i];      //yield break;则跳出循环。
    14             }
    15         }
    16     }

    遍历的方法如下:<代码1-4>

    1            Person p1 = new Person();
    2             foreach (var item in p1)
    3             {
    4                 Console.WriteLine(item);
    5             }
    6             Console.ReadKey();
    yield break的用法,我们在代码1-3修改一下:<代码1-5>
     1 class Person
     2     {
     3         public string Name { get; set; }
     4         public int Age { get; set; }
     5 
     6         public string[] _Name = new string[] {  "jk", "ml","zxh", "wcw", "sk", "yzk" };
     7 
     8         //为当前类型添加一个GetEnumerator()方法,返回值类型是IEnumerator.
     9         public IEnumerator<string> GetEnumerator()
    10         {
    11             for (int i = 0; i < _Name.Length; i++)
    12             {
    13                 if (_Name[i] == "zxh")
    14                 {
    15                     yield break;  //跳出循环
    16                 }
    17                 else
    18                 {
    19                     yield return _Name[i]; 
    20                 }
    21             }
    22         }
    23     }

    当再执行<代码1-4>时,会发现遍历到zxh,程序退出。运行结果如下:

    通过方法二的实现,就可以直接遍历你的自定义对象了。

  • 相关阅读:
    IIS配置跨域请求
    ABP框架页面权限验证
    WPF-DataGrid增删改查不绑定数据源
    WPF-DataGrid增删改查绑定数据源
    .NET开发框架集合(长期更新)
    C# Webbrowser 常用方法及多线程调用
    Devexpress-GridLookUpEdit
    Devexpress提示框的使用
    Asp.Net MVC中Action跳转小结
    ASP.NET MVC备忘
  • 原文地址:https://www.cnblogs.com/chens2865/p/3861713.html
Copyright © 2020-2023  润新知