what is a better way of implementing this pattern?:
void ValidateId(int Id, MyObject[] objects)
{
foreach (var myObject in objects)
{
if (Id == myObject.Id){
return;
}
}
throw new Exception("Invalid Id");
}
first, I would not throw an exception unless the situation is exceptional. Rather, prefer to return a bool and handle a return value of false with a nice friendly error message.
as for validating, you can use Linq….