Please note that I created an extremely simple class for DbC in C#, it should work in .NET 6, and any .NET I believe, it is very simple and limited, but it can serve the purpose of using preconditions, postconditions and assertions.
Here it is
namespace System.Diagnostics.Meyer.Contracts
{
public static class Contract
{
private static void Initialize()
{
System.Diagnostics.Trace.Listeners.Clear();
DefaultTraceListener defaultListener;
defaultListener = new DefaultTraceListener();
Trace.Listeners.Add(defaultListener);
defaultListener.LogFileName = @".\Logs\contract.log";
}
static Contract()
{
Initialize();
}
public static void Assert(bool condition, string message = "")
{
System.Diagnostics.Trace.Assert(condition, "Assertion violation:", message);
}
public static void Require(bool condition, string message = "")
{
System.Diagnostics.Trace.Assert(condition, "Precondition violation:", message);
}
public static void Ensure(bool condition, string message = "")
{
System.Diagnostics.Trace.Assert(condition, "Postcondition violation:", message);
}
}
}
and the usage can go something like
public void Open(LoggerLevel level, string version)
{
Contract.Require(version != null, "version != null");
Contract.Require(_open == false, "_open == false");
// ...
_open = true;
Contract.Ensure(_open == true, "_open == true");
}
or
public LoggerLevel Level
{
get
{
return _level;
}
set
{
Contract.Require(_open == true, "_open == true");
if (value != _level)
{
_level = value;
if (Level != LoggerLevel.Off)
{
WriteContent(GetLevelChangeContent());
}
}
}
}
or
public class Program
{
private static Utility _utility = new Utility();
public static void Main(string[] args)
{
Utility utility = _utility;
Contract.Assert(utility != null, "utility != null");
etc.