IEnumerable在VB.net和c#中不同吗?

本文关键字:VB net IEnumerable | 更新日期: 2023-09-27 18:13:59

我正在将http://customfeedaggregator.codeplex.com/移植到c#,以使自己在c#和WPF中思考。

我被困在IEnumerable的一个问题上。

有一个类- blogpost.vb

'Represents a single blog post
Class BlogPost
    Private _title As String
    Private _datePublished As DateTime
    Private _url As Uri
    Private _category As String
    Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property
    Property DatePublished() As DateTime
        Get
            Return _datePublished
        End Get
        Set(ByVal value As DateTime)
            _datePublished = value
        End Set
    End Property
    Property Url() As Uri
        Get
            Return _url
        End Get
        Set(ByVal value As Uri)
            _url = value
        End Set
    End Property
    Property Category() As String
        Get
            Return _category
        End Get
        Set(ByVal value As String)
            _category = value
        End Set
    End Property

End Class

和一个共享函数,用于检索提要并将其转换为blogposts。

Shared Function RetrieveFeeds(ByVal Address As String) As IEnumerable(Of BlogPost)
        Dim doc As XDocument = XDocument.Load(Address)
        Dim query = From item In doc...<item> _
        Let DataPubblicazione = CDate(item.<pubDate>.Value).ToLocalTime _
        Let TitoloPost = item.<title>.Value _
        Let Url = item.<link>.Value _
        Let Categoria = item.<category>.Value _
            Order By DataPubblicazione Descending _
            Select New BlogPost With _
            {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
             .Url = New Uri(Url), .Category = Categoria}
        Return query
    End Function

类是一个标准,所以这不是一个问题。但RetreiveFeeds是困难的。

这是我的c#版本:

 public static IEnumerable<BlogPost> RetrieveFeeds(string Address)
    {
        XDocument doc = XDocument.Load(Address);
        var query = from item in doc.Descendants("item")
                    let DataPubblicazione = Convert.ToDateTime(item.Attribute("pubDate").Value)
                    let TitoloPost = item.Attribute("title").Value 
                    let Url = item.Attribute("link").Value 
                    let Categoria = item.Attribute("category").Value 
                    orderby DataPubblicazione descending 
                    select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  Url, Categoria};
        return query;
    }

显示在Select New Blogpost部分的错误是:

不能初始化'FeedMe '类型。因为它没有实现'System.Collections.IEnumerable'.

那么,我需要显式实现IEnumerable在我的数据类吗?还是我的c#端口代码错了?这是VB.net和c#之间的区别吗?

IEnumerable在VB.net和c#中不同吗?

实际上正确的语法在c#和VB之间非常相似。净:

原始c#

:

select new BlogPost {DataPubblicazione ,  EscapeXML(TitoloPost),  
                     Url, Categoria};

纠正c#:

select new BlogPost {DatePublished = DataPubblicazione , 
                     Title = EscapeXML(TitoloPost),  
                     Url = new Uri(Url), 
                     Category = Categoria};

原始VB。净:

Select New BlogPost With _
    {.DatePublished = DataPubblicazione, .Title = EscapeXml(TitoloPost), _
     .Url = New Uri(Url), .Category = Categoria}

当使用对象初始化器声明new BlogPost时,您需要命名参数