Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have one enum and class
C#
public enum rank
{
gold,
silver,
bronze
}

public class studentdet
{
public string name{get;set;}
public IList<rank> studentrank{get; set;}

}

how to assign values to studentdet class and its property IList?
Posted
Updated 24-Dec-15 1:49am
v2
Comments
Raje_ 24-Dec-15 2:02am    
Your question is not clear.
Sergey Alexandrovich Kryukov 24-Dec-15 2:30am    
All wrong. Do you understand that IList is interface?
—SA

1 solution

I think you meant to declare the Property as:

public IList studentranks<rank> {get; set;}

a. while you can declare a Property as an IList<SomeType>, and use it when you assign a List<SomeType> to it just as you would use it if you had declared it as List<SomeType>

That's not good practice: it's important for you to keep in mind, as Sergey says, that an IList is an Interface, and that List is an object that implements that Interface. In the case of IList and List, there's nothing to be gained by casting the List to its IList interface ... in other cases there are, indeed, uses in casting an Object to an Interface.

b. while it doesn't make sense to me why one student would have a list of 'rank, I'll assume you have a reason for that:
C#
public enum Rank { Gold, Silver, Bronze }

public class StudentDet
{
    public string StudentName { get; set; }
    public List<Rank> StudentRanks { get; set; }

    // optional entry of rank(s) here
    public StudentDet(string name, params Rank[] ranks)
    {
        StudentName = name;
        AddRanks(ranks);
    }

    public void AddRanks(params Rank[] ranks)
    {        
        if(ranks.Length > 0)
        {
            // lazy initialization here
            if(StudentRanks == null) StudentRanks = new List<Rank>();

            StudentRanks.AddRange(ranks);
        }        
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900