I would like to implement a very simple IDisposable
.
The whole idea is to measure the execution time of my methods which they all return a MethodResult
. e.g.
public class MethodResult : IDisposable
{
private Stopwatch _StopWatch;
public MethodResult()
{
_StopWatch = new Stopwatch();
_StopWatch.Start();
}
public object Result { get; set; }
public TimeSpan ExecutionTime { get; set; }
public void Dispose()
{
_StopWatch.Stop();
ExecutionTime = _StopWatch.Elapsed;
}
}
Usage:
static MethodResult TestMehodResult()
{
using (var result = new MethodResult())
{
result.Result = 666;
Thread.Sleep(1000);
return result;
}
}
My question is really simple: is implementing only Dispose()
method is sufficient for this case, or should I implement the entire Dispose
pattern in my class?
There are no resources to free in my class.
Bonus question: Is there a better pattern to measure execution time of a method instead of using an IDisposable
like I did?
Sorry if this question is dumb. I'm really new to .net
Thanks in advance.