views:

45

answers:

2

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

>

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

+3  A: 

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?

Dan Tao
Must have, and been tricked by the error message. Thanks
Treighton
+1  A: 
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.

priyanka.sarkar_2