C
C#15mo ago
YoloJoeYolo

Custom class is not able to be used with generic type T

Hello everyone I am just programming and run into a problem. I wrote a custom class:
namespace DMIS_API.Models
{
public class ServiceResponse<T>
{
public T? Data { get; set; }
public bool Success { get; set; } = true;
public string Message { get; set; } = string.Empty;
public int ErrorCode { get; set; } = 0;
}
}
namespace DMIS_API.Models
{
public class ServiceResponse<T>
{
public T? Data { get; set; }
public bool Success { get; set; } = true;
public string Message { get; set; } = string.Empty;
public int ErrorCode { get; set; } = 0;
}
}
However when I want to call it as a method parameter with generic type T it does not work: private void DoSomeThing(ref ServiceResponse<T> serviceResponse){...} it does not work. Can someone please help me?
12 Replies
Jimmacle
Jimmacle15mo ago
$details, "does not work" isn't specific
MODiX
MODiX15mo ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
Jimmacle
Jimmacle15mo ago
provide an example of the code you're trying to write and share the errors you're getting
Mayor McCheese
Mayor McCheese15mo ago
Are you literally typing T in the calling method as the type parameter?
YoloJoeYolo
YoloJoeYoloOP15mo ago
I want to use the method to be able to run with any type for the rapper ServiceRespone. A Example for ServiceRespone would be ServiceRespone<List<GetProductDto>> or ServiceRespone<GetProduct>.
Jimmacle
Jimmacle15mo ago
then the method has to be generic as well like DoSomeThing<T>(ref ServiceResponse<T> serviceResponse){...}
YoloJoeYolo
YoloJoeYoloOP15mo ago
It is So after the name of the method I have to wright <T>?
Jimmacle
Jimmacle15mo ago
yes, that's how you define a generic method
YoloJoeYolo
YoloJoeYoloOP15mo ago
I tested it your right
Jimmacle
Jimmacle15mo ago
if you don't do that it's not generic and T has no meaning in that context
YoloJoeYolo
YoloJoeYoloOP15mo ago
Thanks a lot
Jimmacle
Jimmacle15mo ago
pepeok
Want results from more Discord servers?
Add your server