I am trying to refactor below two methods
method 1]
public void DoSomething(Bus a, string str)
{
var temp = new List<Bus>();
...
}
method 2]
public void DoSomething(Car a, string str)
{
var temp = new List<Car>();
...
}
Below doesn't work, and gives 'a is a variable, but used like a type' error. In addition to that, I can't even imagine how to call this method / what to put in the first parameter.
public void DoSomething<T>(T a, string str)
{
var temp = new List<a>();
...
}
DoSomething<Bus>(Bus, "str");
DoSomething<Car>(Car, "str");
- Other posts suggest to use
MakeGenericMethod
. Is this the only way? Use variable as Type - If I want those methods to return
List<T>
(Car / Bus) instead ofvoid
, how can I use #1 solution with Generics?
Thank you very much in advance!
Aucun commentaire:
Enregistrer un commentaire