What's the difference between List and HashSet in a class constructor with EF6?

c# entity-framework entity-framework-6

Question

I have defined a table called ObjectiveDetail using Entity Framework 6 in my application.

I've seen the ObjectiveDetail class created in two different ways. One generates a List, whereas the other makes a hash set.

What's the distinction?

public partial class ObjectiveDetail {
    public ObjectiveDetail() {
        this.SubTopics = new List<SubTopic>();
    }
    public int ObjectiveDetailId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
    public virtual ICollection<SubTopic> SubTopics { get; set; }
}

public class ObjectiveDetail 
{
    public ObjectiveDetail() {
        this.SubTopics = new HashSet<SubTopic>();
    }
    public int ObjectiveDetailId { get; set; }
    public int Number { get; set; }
    public string Text { get; set; }
    public virtual ICollection<SubTopic> SubTopics { get; set; }
}
1
1
1/28/2014 12:12:51 PM

Accepted Answer

ZZZ_tmp
0
1/28/2014 12:46:49 PM

Popular Answer

I believe it depends on whether or not the collection's elements should be unique; in a database, this is most likely accomplished by applying a unique constraint to a column.

List can contain duplicates while Hashset exclusively contains unique objects.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow