I have list of following class Student
class student
{
Guid id;
string name;
}
The list contains multiple students. To search a student with specific id, I need to use foreach loop and compare id of each student.
I am looking for a better alternative instead of foreach loop. Is there any alternative available?
[EDIT]: What I meant by better alternative is optimized solution in terms of execution time and performance
[EDIT2] One more twist, what if id is Guid.
Thanks,
Ram
If each student
can appear in the list only once, you might want to use a Dictionary<int, stutent>
instead. Then you will have a very efficient way of looking up students by id.
Dictionary<int, student> students = GetSomeStudents();
// locate student with id = 42
if (students.ContainsKey(42))
{
var student = students[42];
// do something useful
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With