Guard your methods' Ins and Outs.
This library contains helpers to add semantics to the optionality of your parameters and results from method calls.
This library is built on NetStandard1.0
for maximum compatability.
You can install the nuget package using Install-Package Beefeater
or by heading to the Nuget Package Page.
public Option<string> Modify(NotNull<string> first, Option<string> second)
{
return second.Match(
v => first + v,
() => Option<string>.None);
}
var x = Modify("Hello", "World");
var x = Modify("Hello", null);
public enum ErrorResult
{
UnknownError,
FileNotFound,
Unauthorized
}
public Result<bool, ErrorResult> Create(NotNull<string> filePath, Option<string> second)
{
FileStream stream;
try
{
stream = File.OpenWrite(filePath);
}
catch (UnauthorizedAccessException ex)
{
return ErrorResult.Unauthorized;
}
catch (FileNotFoundException ex)
{
return ErrorResult.FileNotFound;
}
catch (Exception ex)
{
return ErrorResult.UnknownError;
}
using (stream)
{
return second.Match(
v =>
{
using (var writer = new StreamWriter(stream))
{
writer.WriteLine(v);
}
return true;
},
() => false);
}
}
public Either<long, double> DivideByTwo(int aNumber)
{
if (aNumber % 2 == 0)
return aNumber / 2;
return aNumber / (double)2;
}
var x = DivideByTwo(10);
var x = DivideByTwo(5);