I have two class definition that are actually the same. What i'm trying to do is make the T attribute Nullable
public class ApiResponse<T>
{
public Boolean IsSuccessful { get; set; }
public T Data { get; set; } = default;
public String Message { get; set; }
public String Error { get; set; }
}
public class ApiResponse
{
public Boolean IsSuccessful { get; set; }
public String Message { get; set; }
public String Error { get; set; }
}
is there any way to simplify? is for my job, I just came out of curiosity.
CodePudding user response:
In order for T
to be Nullable
implies that T
must be a struct
. This means ApiResponse<T>
must have the constraint that T
is a struct
. You can also reduce code by inheriting from ApiResponse
.
public class ApiResponse<T> : ApiResponse where T : struct
{
public Nullable<T> Data { get; set; } = null;
}
CodePudding user response:
Right now you have two completely unrelated classes. Might be helpful if they had an inheritance relationship.
public class ApiResponse
{
public Boolean IsSuccessful { get; set; }
public String Message { get; set; }
public String Error { get; set; }
}
public class ApiResponse<T> : ApiResponse
{
public T Data { get; set; } = default;
}