I have listed five different scenarios that, I think, zero verification is necessary in the product code. Although, most of the reference books that I checked do NOT carry out such checks. It is prudent to ignore those checks, while the original link tries to deliver other important ideas. Here I summarize all my considerations as follows and please correct me if there are errors.
class Student
{
public string Name { get; set; }
public Student(string name)
{
Name = name;
}
public Student() { }
public override string ToString()
{
return string.Format("Name: {0}", Name);
}
}
class StudentNameComparer : IComparer<Student>
{
public int Compare(Student x, Student y)
{
if ( (x == null) || (y == null) )
throw new ArugmentNullException("bla bla");
return x.Name.CompareTo(y.Name);
}
}
List<Student> students = new List<Student> {
new Student("s1"),
new Student("s4"),
new Student("s3"),
new Student("s2")
};
students.Sort(delegate(Student x, Student y)
{
if ( (x == null) || (y == null) )
throw new ArugmentNullException("bla bla");
return x.Name.CompareTo(y.Name);
});
List<Student> students = new List<Student> {
new Student("s1"),
new Student("s4"),
new Student("s3"),
new Student("s2")
};
students.Sort( (x, y) =>
{
if ( (x == null) || (y == null) )
throw new ArugmentNullException("bla bla");
return x.Name.CompareTo(y.Name);
});
Or
students.Sort( (Student x, Student y) =>
{
if ( (x == null) || (y == null) )
throw new ArugmentNullException("bla bla");
return x.Name.CompareTo(y.Name);
});
List<Student> students = new List<Student> {
new Student("s1"),
new Student("s4"),
new Student("s3"),
new Student("s2")
};
foreach (Student std in students.OrderBy(p =>
{
if (p == null)
{
throw new ArgumentNullException("...");
}
return p.Name;
}))
{
Console.WriteLine(std);
}
q0987 source
share