SORU
12 Temmuz 2010, PAZARTESİ


Ne kadar boş bir IEnumerable iade edebilir miyim?

Verilen takip Kodu ve verilen öneriler in this question, karar verdim, değiştirmek bu özgün yöntem ve sor eğer varsa değerleri İEnumarable iade, değilse iade IEnumerable ile hiçbir değer.

İşte yöntemi:

public IEnumerable<Friend> FindFriends()
        {
            //Many thanks to Rex-M for his help with this one.
            //http://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
            });
        }

Her şeyi iade deyimi içinde olduğundan, bunu nasıl yapabileceğimi bilmiyorum. Bu iş böyle bir şey yapsın?

public IEnumerable<Friend> FindFriends()
        {
            //Many thanks to Rex-M for his help with this one.
            //http://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>();
            }
        }

Yukarıdaki yöntem işe yaramazsa, ve aslında gerek yok; ben sadece niyetimi gösterir hissediyorum.Soyut bir sınıf örneği oluşturulamıyor çünkü bu kod işe yaramadığını belirtmek isterim.

Burada çağıran kodun, herhangi bir zamanda boş bir IEnumerable almak istemiyorum:

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;
            } 

        }

Zaman ayırdığınız için teşekkür ederim.

CEVAP
12 Temmuz 2010, PAZARTESİ


list ?? Enumerable.Empty<Friend>() veya FindFriends dönüş Enumerable.Empty<Friend>() olabilir

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Google Analytics

    Google Analy

    25 ŞUBAT 2008
  • Hak5

    Hak5

    7 EYLÜL 2005
  • J Medema

    J Medema

    11 EKİM 2006