Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

You can access your list using index:
C#
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.
 
Share this answer
 
C#
string[] List1 = {"ab", "bc", "ab", "ts", "ab", "bc", "ts", "op"};
MessageBox.Show(List1[0]);


or


XML
List<string> list = new List<string> { "ab", "bc", "ab", "ts", "ab", "bc", "ts", "op" };
MessageBox.Show(list.FirstOrDefault());
 
Share this answer
 
v2
i cant understand what do you mean list.but you can use array instead of list

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


string firstvalue=arry[0];


for reference:
Array
 
Share this answer
 
v3
Try this...

Response.Write(List1.FirstOrDefault().ToString());
 
Share this answer
 
Comments
M.Kamran Asim 27-Feb-14 4:53am    
its better to use firstOrDefault instead of hardcoding index in List.
But still make sure, .ToString() will through error, if firstOrDefault reutun Null.


Also there is no need to use .ToString, as List Items are already string.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900