TryGetResult

I think this entry has been in the pipeline for a couple of years now and today i have decided to finally post it ;) I got frustrated with the annoying out parameter in TryGet methods so i decided to use a different signature using TryGetResult:

public class TryGetResult<T>
{
 public TryGetResult()
 {
  Success = false;
 }

 public TryGetResult(T result)
 {
  Success = true;
  Result = result;
 }

 public bool Success { get; private set; }
 public T Result { get; private set; }
}

And now your TryGet methods can have the following signature:

public TryGetResult<Person> TryGetPersonByName(string name)
{
 // person is not available
 if(name.IsInvalidPersonName()) return new TryGetResult();
 // return the person
 return new TryGetResult(new Person(name));
}

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>