Work with transfers (list creation)

Perhaps you are faced with situations in which you declare a certain enumeration, and you want to evaluate a certain object in order to find out which categories of enumerations it satisfies, i.e. an object can satisfy the conditions for its classification by more than one enumerator.

An example is, say, at a university, I have different courses, and this one has a list of students attending various specializations. Thus, we have

public class Course
{
    int Id;
    IList<Electrical> ElectricalStudents;
    IList<Computer> ComputerStudents;
    IList<Bioengineering> BioengineeringStudents;
    IList<Mechanical> MechanicalStudents;
}

and my listing is as follows

public enum ParentDepartment
{
    Electrical,
    Mechanical,
    Biology
}

and for each course object that I have, I need to determine which parent sections the students belong to, so that I can use them accordingly (so that I can send these departments)

? .

, , , :

public static IList<ParentDepartment> GetParentDepartments(Course course)
{
    public parentDepartmentList=new List<ParentDepartment>();
    if(ElectricalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(ComputerStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Electrical);
    if(MechanicalStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Mechanical);
    if(BioengineeringStudents.Count>0)
            Add(parentDepartmentList,ParentDepartment.Biology);
}

private void Add(IList<ParentDepartment> parentDepartmentList,ParentDepartment         parentdept)
{
    if(!parentDepartmentList.Contains(parentdept))
            parentDepartmentList.Add(parentdept);
}

, . , , , , .

. , . ?

.

P.S: , #, , .

+3
2

.NET enum :

[Flags]
public enum ParentDepartments
{
    None = 0x0,
    Electrical = 0x1,
    Mechanical = 0x2,
    Biology = 0x4,
}

public static ParentDepartments GetParentDepartments(Course course)
{
    var departments = ParentDepartments.None;

    if (course.ElectricalStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.ComputerStudents.Any())
        departments |= ParentDepartments.Electrical;

    if (course.MechanicalStudents.Any())
        departments |= ParentDepartments.Mechanical;

    if (course.BioengineeringStudents.Any())
        departments |= ParentDepartments.Biology;

    return departments;
}
+4

Java EnumSet. #.

+1

Source: https://habr.com/ru/post/1764490/


All Articles