C
C#21h ago
Lounder

How terrible is this Result implementation?

public class SuccessResult : Result
{
public object? Data { get; protected set; }

internal SuccessResult(int statusCode, object? data)
: base(statusCode, true)
{
this.Data = data;
}
}

public class SuccessResult<T> : SuccessResult
{
public new T? Data
{
get => (T?)base.Data;
private set => base.Data = value;
}

internal SuccessResult(int statusCode, T? data)
: base(statusCode, data)
{
}
}
public class SuccessResult : Result
{
public object? Data { get; protected set; }

internal SuccessResult(int statusCode, object? data)
: base(statusCode, true)
{
this.Data = data;
}
}

public class SuccessResult<T> : SuccessResult
{
public new T? Data
{
get => (T?)base.Data;
private set => base.Data = value;
}

internal SuccessResult(int statusCode, T? data)
: base(statusCode, data)
{
}
}
2 Replies
Lounder
LounderOP21h ago
Is it absolutely wrong to do it like so? I'll be using this instead
public class SuccessResult<T> : Result, ISuccessResult
{
public object? DataObject => this.Data;

public T? Data { get; private set; }

internal SuccessResult(int statusCode, T? data)
: base(statusCode, true)
{
this.Data = data;
}
}
public class SuccessResult<T> : Result, ISuccessResult
{
public object? DataObject => this.Data;

public T? Data { get; private set; }

internal SuccessResult(int statusCode, T? data)
: base(statusCode, true)
{
this.Data = data;
}
}
Sossenbinder
Sossenbinder17h ago
The first version is definitely not ideal, you have a shadowed property, and this would also be prone to boxing in case T is a value type Second option definitely looks better Although I'm not sure when you would need an object as the Data opposed to the T itself There's a bunch of result implementations out there you can just use as well
Want results from more Discord servers?
Add your server