Jak Mogę zwrócić pustą liczbę?
Biorąc pod uwagę następujący kod i sugestie podane w tym pytaniu, zdecydowałem się zmodyfikować tę oryginalną metodę i zapytać, czy są jakieś wartości w IEnumerable return it, jeśli nie zwróci IEnumerable bez wartości.
Oto metoda:
public IEnumerable<Friend> FindFriends()
{
//Many thanks to Rex-M for his help with this one.
//https://stackoverflow.com/users/67/rex-m
return doc.Descendants("user").Select(user => new Friend
{
ID = user.Element("id").Value,
Name = user.Element("name").Value,
URL = user.Element("url").Value,
Photo = user.Element("photo").Value
});
}
Ponieważ wszystko jest w deklaracji powrotu, Nie wiem, jak mógłbym to zrobić. Czy coś takiego zadziała?
public IEnumerable<Friend> FindFriends()
{
//Many thanks to Rex-M for his help with this one.
//https://stackoverflow.com/users/67/rex-m
if (userExists)
{
return doc.Descendants("user").Select(user => new Friend
{
ID = user.Element("id").Value,
Name = user.Element("name").Value,
URL = user.Element("url").Value,
Photo = user.Element("photo").Value
});
}
else
{
return new IEnumerable<Friend>();
}
}
Powyższa metoda nie działa, a tak naprawdę nie powinna; po prostu czuję to ilustruje moje intencje. uważam, że powinienem określić, że kod nie działa, ponieważ nie można utworzyć instancji klasy abstrakcyjnej.
Oto kod wywołujący, nie chcę, aby w dowolnym momencie otrzymywał liczbę null:
private void SetUserFriends(IEnumerable<Friend> list)
{
int x = 40;
int y = 3;
foreach (Friend friend in list)
{
FriendControl control = new FriendControl();
control.ID = friend.ID;
control.URL = friend.URL;
control.SetID(friend.ID);
control.SetName(friend.Name);
control.SetImage(friend.Photo);
control.Location = new Point(x, y);
panel2.Controls.Add(control);
y = y + control.Height + 4;
}
}
Dziękuję za poświęcony czas. 5 answers
Możesz użyć list ?? Enumerable.Empty<Friend>()
, lub mieć FindFriends
return Enumerable.Empty<Friend>()
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-12 15:24:46
You could return Enumerable.Empty<T>()
.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-16 18:43:19
Jak dla mnie najbardziej elegancki sposób to yield break
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-18 04:39:07
To oczywiście tylko kwestia osobistych preferencji, ale opisałbym tę funkcję używając yield return:
public IEnumerable<Friend> FindFriends()
{
//Many thanks to Rex-M for his help with this one.
//http://stackoverflow.com/users/67/rex-m
if (userExists)
{
foreach(var user in doc.Descendants("user"))
{
yield return new Friend
{
ID = user.Element("id").Value,
Name = user.Element("name").Value,
URL = user.Element("url").Value,
Photo = user.Element("photo").Value
}
}
}
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-12 15:40:03
Myślę, że najprostszym sposobem byłoby
return new Friend[0];
Wymagania zwracania polegają jedynie na tym, że metoda zwraca obiekt, który implementuje IEnumerable<Friend>
. Fakt, że w różnych okolicznościach zwracasz dwa różne rodzaje obiektów jest nieistotny, o ile oba zaimplementują IEnumerable.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-07-12 15:38:00