You can check this and throw an exception at runtime, for example:
if (grade < 2.00 || grade > 6.00)
throw new ArgumentOutOfRangeException("grade");
Always put such conditions at the beginning of a method or constructor. I even put them on my own #region(but this is my personal preference):
public Student(string firstName, string lastName, double grade)
: base(firstName, lastName)
{
#region Contract
if (grade < 2.00 || grade > 6.00)
throw new ArgumentOutOfRangeException("grade");
#endregion
this.FirstName = firstName;
this.LastName = lastName;
this.Grade = grade;
}
, , Code Contracts. MSDN, . - Visual Studio Microsoft. , . :
using System.Diagnotistics.Contracts;
public Student(string firstName, string lastName, double grade)
: base(firstName, lastName)
{
#region Contract
Contract.Requires<ArgumentOutOfRangeException>(grade >= 2.00);
Contract.Requires<ArgumentOutOfRangeException>(grade <= 6.00);
#endregion
this.FirstName = firstName;
this.LastName = lastName;
this.Grade = grade;
}