开发者

Adding a property to an interface that's a List

This compiles:

public interface IBookCatalogueView
{
    Book[] Books 
    { 
        get; 
        set; 
    }
}

This doesn't, giving the error "Interfaces cannot contain fields"

public interface IBookCatalogueView
{
    List<Book> Books
    { 
        get;
        set;
    }
}

>

开发者_JAVA技巧

Why? How can I define a property that's a list in an interface?


This (your second example) does compile:

public interface IBookCatalogueView
{
    // Property
    List<Book> Books
    {
        get;
        set;
    }
}

This does not:

public interface IBookCatalogueView
{
    // Field
    List<Book> Books;
}

Check your syntax. Did you sneak an accidental ; in there, perhaps?


This doesn't, giving the error "Interfaces cannot contain fields"

public interface IBookCatalogueView 
{ 
    List<Book> Books 
    {  
        get; 
        set; 
    } 
}

But this is not fields but instead property and hence will compile fine.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜