C
C#16mo 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
Jimmacle16mo ago
$details, "does not work" isn't specific
MODiX
MODiX16mo 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
Jimmacle16mo ago
provide an example of the code you're trying to write and share the errors you're getting
Mayor McCheese
Mayor McCheese16mo ago
Are you literally typing T in the calling method as the type parameter?
YoloJoeYolo
YoloJoeYoloOP16mo 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
Jimmacle16mo ago
then the method has to be generic as well like DoSomeThing<T>(ref ServiceResponse<T> serviceResponse){...}
YoloJoeYolo
YoloJoeYoloOP16mo ago
It is So after the name of the method I have to wright <T>?
Jimmacle
Jimmacle16mo ago
yes, that's how you define a generic method
YoloJoeYolo
YoloJoeYoloOP16mo ago
I tested it your right
Jimmacle
Jimmacle16mo ago
if you don't do that it's not generic and T has no meaning in that context
YoloJoeYolo
YoloJoeYoloOP16mo ago
Thanks a lot
Jimmacle
Jimmacle16mo ago
pepeok

Did you find this page helpful?