开发者

Adding a property to an interface that's a List

开发者 https://www.devze.com 2023-01-17 02:54 出处:网络
This compiles: public interface IBookCatalogueView { Book[] Books { get; set; } } This doesn\'t, giving the error \"Interfaces cannot contain fields\"

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

精彩评论

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

关注公众号