【问题标题】:IEnumerable C# Class How-toIEnumerable C# 类操作方法
【发布时间】:2012-03-20 05:31:17
【问题描述】:

下面是我的课。我需要使其可枚举。我在网上查看过,虽然我找到了很多文档,但我仍然迷路了。我认为这绝对是我第一次问这个问题,但是有人可以为我制作这个该死的东西 Enumerable ,我会从那里弄清楚。 我正在使用 C# ASP.Net 4.0

    public class ProfilePics
{
    public string status { get; set; }
    public string filename { get; set; }
    public bool mainpic { get; set; }
    public string fullurl { get; set; }

}

【问题讨论】:

  • 你想列举什么?我没有看到任何明显的数据可以作为一个好的候选者。

标签: c# c#-4.0 ienumerable ienumerator


【解决方案1】:

好吧...如果您只想让某人“让这个该死的东西可枚举”,那就去吧...

public class ProfilePics : System.Collections.IEnumerable
{
    public string status { get; set; }
    public string filename { get; set; }
    public bool mainpic { get; set; }
    public string fullurl { get; set; }

    System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
    {
        yield break;
    }
}

它不枚举任何东西,但它是可枚举的。

现在我将尝试进行一些读心术,并想知道您是否想要这样的东西:

public class ProfilePicture
{
    public string Filename { get; set; }
}

public class ProfilePics : IEnumerable<ProfilePicture>
{
    public List<ProfilePicture> Pictures = new List<ProfilePictures>();

    public IEnumerator<ProfilePicture> GetEnumerator()
    {
        foreach (var pic in Pictures)
            yield return pic;
        // or simply "return Pictures.GetEnumerator();" but the above should
        // hopefully be clearer
    }
}

【讨论】:

  • 哇!... 没有意识到这个问题已经这么老了。编辑将其带到了主页...
  • 天哪,我完全忘记了这个问题,可悲的是前几天我刚刚浏览了我的问题清单,试图确保我没有忘记任何问题。好的,谢谢
【解决方案2】:

问题是:你想列举什么?

我猜你想要某种容器,包含你的 ProfilePics 类型的项目,所以去吧

List<ProfilePics>

在这样的类中:

public class ProfilePic
{
    public string status { get; set; }
    public string filename { get; set; }
    public bool mainpic { get; set; }
    public string fullurl { get; set; }

}

public class ProfilePics : IEnumerable<ProfilePic>
{
    private pics = new List<ProfilePic>();

    // ... implement the IEnumerable members
}

或者只是在需要容器的地方直接使用List&lt;ProfilePics&gt;

如果您确实错过了:这里是此 IEnumerable 的 MSDN 文档(还有更多示例)

【讨论】:

    【解决方案3】:

    要成为可枚举类应该实现一些集合。在您的示例中,我没有看到任何集合属性。如果您想收集个人资料图片,请将您的班级重命名为“ProfiePic”并使用 List。

    如果您想将某些属性公开为集合,请将其设为 IEnumerable 或 List 或其他集合类型。

    【讨论】:

      【解决方案4】:

      我会使用没有继承的类:

      using System;
      using System.Collections.Generic;
      
      namespace EnumerableClass
      {
          public class ProfilePics
          {
              public string status { get; set; }
              public string filename { get; set; }
              public bool mainpic { get; set; }
              public string fullurl { get; set; }
      
              public IEnumerator<object> GetEnumerator()
              {
                  yield return status;
                  yield return filename;
                  yield return mainpic;
                  yield return fullurl;
              }
          }
      
          class Program
          {
              static void Main(string[] args)
              {
                  ProfilePics pic = new ProfilePics() { filename = "04D2", fullurl = "http://demo.com/04D2", mainpic = false, status = "ok" };
                  foreach (object item in pic)
                  {
                      Console.WriteLine(item);
                      //if (item is string) ...
                      //else if (item is bool) ...
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-03
        • 1970-01-01
        • 2021-11-23
        • 1970-01-01
        • 2020-01-18
        • 2019-03-17
        相关资源
        最近更新 更多