您如何访问收藏夹中的任何一项 [英] how can u access any one of item from collections

查看:116
本文介绍了您如何访问收藏夹中的任何一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上,我不得不使用foreach循环来访问集合中的每个项目,但我想

可以访问集合中的任何一项,即,只能访问
中的一项
collection.不想访问其余项目.

foreach循环就像

actually i had to use foreach loop for accessing each item from collection but i want

to access the any one of item from collection i.e access only one item from

collection.don''t want to access the remaining items.

foreach loop like as

foreach(string item in collection)
{


}



我要访问集合中的任何一项,即,仅访问
中的一项
collection.不想访问其余项目.


请帮帮我.

谢谢.



i want to access the any one of item from collection i.e access only one item from

collection.don''t want to access the remaining items.


please help me.

thank u.

推荐答案

如果您的集合实现了索引器,则可以直接访问项目.
使用索引器(C#编程指南) [第64部分-C#教程-.net中索引器的使用方式和位置 [ ^ ].
If your collection implements an indexer, you can directly access an item.
Using Indexers (C# Programming Guide)[^] provides some details about this.

Here is a video that shows indexer examples - Part 64 - C# Tutorial - How and where are indexers used in .net [^].


// get the first item if there is one
string x = collection.FirstOrDefault();

// or get the first one that confirms to a condition, such as it
// starts with "A"
string y = collection.FirstOrDefault(s => s.StartsWith("A"));


这篇关于您如何访问收藏夹中的任何一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆