如何从列表中获取第一个项目? [英] How can I get the first item from a list?

查看:82
本文介绍了如何从列表中获取第一个项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表,其中所有项目都是字符串



像List1 = {ab,bc,ab,ts,ab,bc,ts,op}



如何从此列表中获取第一个?



以下列表中的示例输出:ab

解决方案

您可以使用索引访问您的列表:

 List< string> list =  new 列表< string> {  ab  bc  ab  ts  ab  bc  ts  op}; 
// 要获取第一项,请在索引0处寻找项目
string firstListItem = list [ 0 ]; // firstListItem ==ab





希望这有帮助。


  string  [] List1 = {  ab < span class =code-string> bc,  ab  ts  ab  bc  ts  OP}; 
MessageBox.Show(List1 [ 0 ]);











列表<   string  >  list = new List <   string  >  {ab,bc,ab,ts, ab,bc,ts,op}; 
MessageBox.Show(list.FirstOrDefault());


我无法理解你是什么意思list.but你可以使用数组代替列表



  string  [] arry = {  ab  bc  ab   ts  ab  bc  ts  op} ; 


string firstvalue = arry [ 0 ];





供参考:

阵列


I have a list where all items are string

Like List1= {ab,bc,ab,ts,ab,bc,ts,op}

how can I get the first one from this List?

sample output from following list: ab

解决方案

You can access your list using index:

List<string> list = new List<string> { "ab", "bc", "ab", "ts", "ab", "bc", "ts", "op" };
// To get the first item, seek for item at index 0
string firstListItem = list[0]; // firstListItem == "ab"



Hope this helps.


string[] List1 = {"ab", "bc", "ab", "ts", "ab", "bc", "ts", "op"};
MessageBox.Show(List1[0]);



or


List<string> list = new List<string> { "ab", "bc", "ab", "ts", "ab", "bc", "ts", "op" };
MessageBox.Show(list.FirstOrDefault());


i cant understand what do you mean list.but you can use array instead of list

string[] arry={"ab","bc","ab","ts","ab","bc","ts","op"};


string firstvalue=arry[0];



for reference:
Array


这篇关于如何从列表中获取第一个项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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