title: Checking for incorrect input inside properties in C#
body: <p>I am using properties to allow a single string <code>name</code> to be added to the class <code>Details</code>, I want the property to only accept the string if it can be split into two parts.</p>
  
  <p>The two parts would be the <code>firstName</code> and the <code>LastName</code>. However if the resulting split has 1,0 or more than 2 strings in the array then the input should be deemed invalid and I want to throw a error to whatever code called the property in the first place. </p>
  
  <p>Can error handling be done on properties like this?</p>
  
  <p>If <strong>not</strong> then what of the following is the preferred way to get data into a class whilst checking for correctness:</p>
  
  <ol>
  <li>Use a method inside the class <code>Details</code> to handle error inputs, make that method <code>boolean</code>.</li>
  <li>Continue using properties but have the error checking done by the code that calls the property. I don't like this because I want all the error checking code to be self-contained in the <code>Details</code> class.  </li>
  </ol>
  
  <p>.</p>
  
  <pre><code>class Details
  {
      private string firstName, lastName;
  
      public string Name
      {
          // name
          get { return firstName + " " + lastName; }
          set 
          { 
              string name = value;
              string[] nameArray = name.Split(' ');
              firstName = nameArray[0];
              lastName = nameArray[1];
          }
      }
  }
  </code></pre>
  
  <p>EDIT: I am mostly interested in what of the three options is concidered best pratice:</p>
  
  <ol>
  <li>Error check inside properties.</li>
  <li>Error check outside class in another class, and then just add the verified inputs to <code>Details</code></li>
  <li>Use a <code>boolean</code> method inside <code>Details</code> to verify the inputs.</li>
  </ol>