首页 > 编程语言 >C#的迭代器/枚举器,索引器

C#的迭代器/枚举器,索引器

时间:2023-05-20 11:44:29浏览次数:30  
标签:animals 迭代 C# Add zoo 枚举 Animal new public

1.迭代器:可使用Foreach遍历,如List,Dictionary等,其继承IEnumerable接口,并实现public IEnumerator GetEnumerator()方法;

举例:有个动物园,其中有各种动物:

public class Zoo : IEnumerable
{
    List<Animal> animals;
    public Zoo()
    {
        animals = new List<Animal>();
    }
    public void Add(Animal ani) { animals.Add(ani); }
    public IEnumerator GetEnumerator()//返回迭代器
    {
        foreach (var v in animals)
            yield return v;
    }
}

2.索引器,可以直接指定[Index]对对象中的元素进行读取或赋值

public Animal this[int index]//索引器
{
    get
    {
        if (animals.Count > index)
            return animals[index];
        return null;
    }
    set
    {
        if (animals.Count > index)
            animals[index] = value;
    }
}

使用迭代器和索引器:

Zoo zoo = new Zoo();
zoo.Add(new Animal("Cat"));
zoo.Add(new Animal("Dog"));
zoo.Add(new Animal("Pig"));
zoo.Add(new Animal("Duck"));
zoo[2] = new Animal("Cattle");
Console.WriteLine(zoo[3].name);//索引器,可指定Index进行赋值或读值
foreach (Animal v in zoo)
    Console.WriteLine(v.name);//迭代器,使用Foreach进行遍历

运行结果

 

  

  

  

  

标签:animals,迭代,C#,Add,zoo,枚举,Animal,new,public
From: https://www.cnblogs.com/cfsl/p/17341464.html

相关文章

  • synopsys dw_axi_dmac 使用集成经验
    总体简介AHB/APB用于寄存器配置和访问,2个AXIMasterport用于实现数据src和dst的搬运,带perh请求握手接口,独立的debuginterface,中断接口主要特性▲共32个channel,每个通道都对应一对src和dst▲每个channel都是单向的▲2个AXIMaster支持多层级连接访问▲mem2mem,mem2perh,pe......
  • 手撕ThreadLocal源码
    1. 图解  722. 代码  72代码在com.powernode.threadlocalTestpackagecom.powernode.threadlocal;//这里是主main方法72publicclassTest{publicstaticvoidmain(String[]args){//获取Thread对象Threadthread=Thread.currentThread();......
  • Centos 设置密钥登陆并禁用密码
    1:备份文件cd/etc/ssh/scpsshd_configsshd_config.bak2:生成密钥一路回车按下去就可以了。[root@centos7-1ssh]#ssh-keygen-trsaGeneratingpublic/privatersakeypair.Enterfileinwhichtosavethekey(/root/.ssh/id_rsa):#回车Createddirectory'/root/.ssh......
  • 用python读取excel文件
    需要用到的包--pandasimportpandasaspd简单读取excel文件,要用到read_excel()df=pd.read_excel("D:\无名字的文件夹\实验材料.xlsx")dfOut[11]:姓名年龄喜好0张三15抖音1李四16快手2王五17抖音3小明18小红书4小花19小红书使用r......
  • @EnableAsync @Async
    一.基本介绍@Async是spring为了方便开发人员进行异步调用的出现的,在方法上加入这个注解,spring会从线程池中获取一个新的线程来执行方法,实现异步调用@EnableAsync表示开启对异步任务的支持,可以放在springboot的启动类上,也可以放在自定义线程池的配置类上,具体看下文二.最简单的......
  • python-docx - 1
    python-docx用于创建和更新Word文件的python库1.安装pip3installpython-docx-ihttps://mirrors.aliyun.com/pypi/simple2.创建与保存文件#导入DocumentfromdocximportDocument#创建一个新文档doc=Document()print(type(doc))#<class'docx.document.Doc......
  • subsequence1 (牛客多校) (2个串比大小, DP, 组合数)
    题面大意:给定2个字符串,问有多少个子字符串S,是大于t的 思路数据范围很小,因此考虑n^2做法分2步,位数s>位数t的时候然后位数相等的时候利用DP,处理,分别就是枚举前k个数和s相同,然后k+1个数比t大就可以. 具体思路自己想想,和那个比较像   cons......
  • c#Mutex总结
    c#Mutex的用法总结C#多线程系列之进程同步Mutex类......
  • 1. react项目【前端】+C#【后端】从0到1
    1、创建前端基础框架 1.1前端创建软件: 1.1.1npxcreate-react-apppcps:pc是文件名; 1.1.2npmstart启动项目 2、创建后端基础框架软件: 2.1创建webapi项目  Program.cs是启动文件;  ......
  • iEnhancer-ENCC_test_layer1.py源码阅读
    一、基本准备¶1、导入包:与train一致¶ In [ ]:importnumpyasnpimporttorchfromtorchimportnnfromtorch.autogradimportVariablefromtorch.utils.dataimportDataset,DataLoaderimporttorch.nn.functionalasFimportpickleimport......