Few Days back, I faced a test where I was asked to answer the following question: Though it seems basic, I've some doubt and my own opinion
A publication center publishes books. A writer can write many books and a book has a author
There were four options and among them, I omitted two options that weren't close. So left two options as follows:
One Option With List:
public class Publisher
{
public int PublisherId { get; set;}
public string PublisherName { get; set;}
public string Address { get; set;}
public List<Author> Authors { get; set;}
public List<Book> Books{ get; set;}
}
public class Author
{
public int AuthorId { get; set;}
public string AuthorName { get; set;}
public string AuthorAddress { get; set;}
public List<Book> Books{ get; set;}
}
public class Book
{
public int BookId { get; set;}
public string BookName { get; set;}
public Author Author { get; set;}
}
Another Option With Array:
public class Publisher
{
public int PublisherId { get; set;}
public string PublisherName { get; set;}
public string Address { get; set;}
public Author[] Authors { get; set;}
public Book[] Books{ get; set;}
}
public class Author
{
public int AuthorId { get; set;}
public string AuthorName { get; set;}
public string AuthorAddress { get; set;}
public Book[] Books{ get; set;}
}
public class Book
{
public int BookId { get; set;}
public string BookName { get; set;}
public Author Author { get; set;}
}
In the same time, I looked into this link to understand about the difference: List Vs Array
Upon that, Firstly, I chose the first option (My answer was this) and believe, List
has more functionality and would be a better choice. Another reason is that, I use EF in projects and when work with relationship with tables specifically for One-To-Many, then classes created, has a collection of List
like this:
public List<Book> Books{ get; set;}
Secondly, I was thinking, if arrays could be used for the same, but what I learned array data structure is perfect for fixed data. I hope, I am on the right track.
Finally I was unable to understand two things from the link provided:
1) As a counter - List<T>
is one-dimensional; where-as you have have rectangular (etc) arrays like int[,] or string[,,] - but there are other ways of modelling such data (if you need) in an object model
My views - One dimensional means is List
a one-dimensional array or something related.
2) Little bit confused I mean in what case should we use the following with Array
? Though it explains but need some more clarification:
it does a lot of bit-shifting, so a byte[] is pretty much essential for encoding;
I use a local rolling byte[] buffer which I fill before sending down to the underlying stream (and v.v.); quicker than BufferedStream etc;
- it internally uses an array-based model of objects (Foo[] rather than List), since the size is fixed once built, and needs to be very fast.