Validating input values in C# I began studying C# 5.0 a few days ago and am trying to avoid duplicated code for validation of input values.

class transcript
{
  //Use lamda expression and Func for validation logic
  Func&lt;byte, byte&gt; validate = (grade) =&gt;
  {
    if (grade &gt; 100)
      throw new ArgumentOutOfRangeException("grade can`t be more than 100");
    else
      return grade;
  };

  public string name { get; set; }

  public byte kor
  {
    get { return kor; }
    set { kor = validate(value); }
  }

  public byte eng
  {
    get { return this.eng; }
    set { eng = validate(value); }
  }
}
Per the Capitalization Conventions on MSDN, types, methods, and properties should be PascalCasemeaning they start with an uppercase letter.



  Func&lt;byte, byte&gt; validate = (grade) =&gt;
  {
    if (grade &gt; 100)
      throw new ArgumentOutOfRangeException("grade can`t be more than 100");
    else
      return grade;
  };


There is no reason to use a Func here. Just use a normal method, like this

private byte Validate(byte grade)
{
    // the method body can go here
}




  public byte kor
  {
    get { return kor; }
    set { kor = validate(value); }
  }


Have you tested this code? The getter and setter both look like a StackOverflowException (via infinite recursion) to me. You need a backing field, like this

private byte kor;
public byte Kor
{
    get { return kor; }
    set { kor = Validate(value); }
}




It's uncommon to use byte instead of int, even if you know your data will fit in a byte. Only optimize for size if you know that you need to do so.