I cannot understand this piece of code:
public class BookCategory
{
public string CategoryName { get; set; }
public List<Book> Books { get; set; }
}
public class Book
{
public string Title
{
get;
set;
}
}
What does it mean List<Book>, and does it inherit from class Book or not ?
List<Book>[MSDN] is a generic collection ofBookobjects. It doesn’t inherit fromBook. Rather, it contains zero or moreBookobjects in a collection.Because the
List<T>class is generic, it can be reused to provide list operations on a collection of any type of object. You can haveList<int>,List<string>,List<object>, etc., but they all work from the sameList<T>class, which is a great example of the DRY principle (Don’t Repeat Yourself). In this example,Tis called the type parameter and can be any .NET or user-defined type.You can define you’re own generic classes. Here’s a trivial example of the syntax:
Some other very useful .NET generics are:
Stack<T>[MSDN]Queue<T>[MSDN]Nullable<T>[MSDN]Dictionary<TKey,TValue>[MSDN]Generics are very useful and pervasive in .NET, so definitely familiarize yourself with this concept.