Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c#和列表:如何使用foreach循环从列表中检索对象?_C#_Android - Fatal编程技术网

c#和列表:如何使用foreach循环从列表中检索对象?

c#和列表:如何使用foreach循环从列表中检索对象?,c#,android,C#,Android,这是一个很难回答的问题,只是我很久没有这样做了,需要一些帮助 这就是问题所在。我的列表“lstfriendlist”中有此调试信息: 我只是在我的活动中设置了一个断点,然后点击我的列表,看到我所有的“朋友”都出现在这个列表的“friendUsername”下 我可以通过以下方式检索某个用户名: string temp = lstfriendList[11].friendUsername.ToString(); 这将在字符串“temp”上返回“torben” 现在我忘记了如何

这是一个很难回答的问题,只是我很久没有这样做了,需要一些帮助

这就是问题所在。我的列表“lstfriendlist”中有此调试信息:

我只是在我的活动中设置了一个断点,然后点击我的列表,看到我所有的“朋友”都出现在这个列表的“friendUsername”下

我可以通过以下方式检索某个用户名:

        string temp = lstfriendList[11].friendUsername.ToString(); 
这将在字符串“temp”上返回“torben”

现在我忘记了如何使用foreach循环从列表中按顺序检索所有对象,然后将它们写下来。很抱歉打扰你,但我忘了:(

我希望你能帮助我。
谢谢:)

您已经在foreach循环的头部声明了一个类型为“Friend”的变量。现在,您可以通过键入来访问当前对象的属性

foreach (Friend f in lstfriendList)
{
    string temp = f.friendUsername;
}

我认为没有办法停留在那里,因为你现在在循环中有
Friend
对象(
f
),只需在
f
之后放置一个
,看看intellisense的建议,不管怎样,如果你像下面这样修改类是非常好的,使用:

然后像这样使用:

foreach (Friend f in lstfriendList)
{
    string friendDetails = f.ToString();
    Console.WriteLine(friendDetails);
}

要完成Sebastian Hofmann的回答,可以使用.OrderBy或.OrderByDescending按名称或用户名排序

foreach (Friend f in lstfriendList.OrderBy(list => list.friendUsername))
{
    string temp = f.friendUsername;
}
将用户名从a返回到z

foreach (Friend f in lstfriendList.OrderByDescending(list => list.friendUsername))
{
    string temp = f.friendUsername;
}

将用户名z返回到a

你说的“写下来”是什么意思?你想用这些名字做什么?你已经把它们都列在一个列表中了,所以把它们放在另一个列表中似乎很奇怪。我仍然想知道为什么有人不写长文本,而是运行屏幕截图,在网站上加载图像以获取有关语法的简单问题不要直接找到他/她需要的信息源您知道您可以使用lamda/extension方法访问内联列表.foreach()。。您是否也了解如何在浏览器中键入
Google.com
foreach (Friend f in lstfriendList.OrderByDescending(list => list.friendUsername))
{
    string temp = f.friendUsername;
}