Skip to content

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Result

This is an idea of handling methods that can return error without using pattern TryMethod(out string error).

In this concept, we have 4 types of methods that does:

  • not return anything and cannot fail (void methods)
  • return some value and cannot fail (non-void methods)
  • not return anything and can fail (returning Result)
  • return some value and can fail (returning Result<T>)

Usage

pubilicstatic void Main(string[]args){ResultfirstMethodResult=VoidMethodThatCanReturnError();if(firstMethodResult.IsOk){Console.WriteLine("first method finished without an error");}else{Console.WriteLine(firstMethodResult.ErrorMsg);}Result<Person>secondMethodResult=ReturningMethodThatCanReturnError();if(secondMethodResult.IsError){Console.WriteLine(secondMethodResult.ErrorMsg);}else{Console.WriteLine(secondMethodResult.Value.Name);}}privateResultVoidMethodThatCanReturnError(){if(condition){returnResult.Error("error message")}else{returnResult.Ok();}}privateResult<Person>ReturningMethodThatCanReturnError(){try{varperson=newPerson("John","Doe");returnResult.Ok(person);}catch(Exceptionex){returnResult.Error(ex.Message);}}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages