C# Properties - Set question
<p>I'm working in C# and I'm starting to play with properties. One thing I don't know is what is the best way/where to put the logic for the set accessors of class properties and how to handle the errors.</p>

<p>For instance, say I have this (basic) class:</p>

<pre><code>class Person
{
    private int _Age = 18;

    public Person()
    {

    }

    public int Age
    {
        get
        {
            return _Age;
        }
        set
        {
            _Age = value;
        }
    }
}
</code></pre>

<p>Now say I have a requirement on the Age property, 0 &lt; Age &lt; 100. Where do I put the logic for this? </p>

<p>Should I put it in the property?</p>

<pre><code>public int Age
    {
        get
        {
            return _Age;
        }
        set
        {
           if (value &lt; 0 || value &gt; 99)
               // handle error
           else
               _Age = Convert.ToInt32(value);
        }
    }
</code></pre>

<p>or through class that is creating a Person object? </p>

<pre><code>static void Main(string[] args)
{
   Person him = new Person();
   int NewAge = -10;

   if (NewAge &lt; 0 || NewAge &gt; 100)
      // handle error 
   else
      him.Age = NewAge;
}
</code></pre>

<p>Now what if there is a problem with the NewAge (it doesn't meet my constraint)? Should I create a custom exception and throw that? Should I just print a message saying supply a valid age?</p>

<p>I've done some Googling and I cannot find anything that fully answers my questions. I need a book :-/</p>
