zoukankan      html  css  js  c++  java
  • IEnumerable接口

    实现IEnumerable接口的类可以进行简单迭代,例如foreach语句
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;

    namespace IEnumerable接口
    {
        
    class Program
        
    {
            
    static void Main(string[] args)
            
    {
                A a 
    = new A();
                
    foreach (int? var in a)//当int值可能被赋空值时用?

                
    {
                    Console.WriteLine(var);
                    
                }


            }

        }

        
    class A : IEnumerable
        
    {
            
    int x = 1;
            
    int y = 2;
            
    int z = 3;
            
    public IEnumerator GetEnumerator()
            
    {
               
    // int i = -1;
                for (int i = 0; i <=3; i++)
                
    {


                    
    if (i == x)
                    
    {
                        yield 
    return 1;
                    }

                    
    else if (i == y)
                    
    {
                        yield 
    return 2;

                    }

                    
    else if (i == z)
                    
    {
                        yield 
    return 3;

                    }

                    
    else
                    
    {
                        yield 
    return null;
                    }

                }

            }

        }

    }

    肩负责任,永不退缩
  • 相关阅读:
    多个tomcat配置,解决冲突问题
    多态-重载和覆载
    静态成员、静态类和枚举
    重复使用类--继承和组合
    建立更可靠的OOP程序-类和成员的访问控制
    用ps画一个Gif的小房子(1)
    在h5页面上添加音乐播放
    使用Object类为实例定义方法和属性
    使用 prototype 定义方法和属性
    使用 this 关键字定义方法和属性
  • 原文地址:https://www.cnblogs.com/ATP/p/860660.html
Copyright © 2011-2022 走看看