0

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");
  1. Other posts suggest to use MakeGenericMethod. Is this the only way? Use variable as Type
  2. If I want those methods to return List<T> (Car / Bus) instead of void, how can I use #1 solution with Generics?

Update As I left in the comments -> @ChetanRanpariya, How to call this method, then? DoSomething(?, ?);

Adrian
  • 836
  • 7
  • 20
  • 44

2 Answers2

4

You used a (the instance) instead of the actual type T

public void DoSomething<T>(T a, string str)
{
   var temp = new List<T>(); // you need to use T here, it's the actual type
}

...

DoSomething<Bus>(myBus, "str");
DoSomething<Car>(myCar, "str");

Additional Resources

Generics (C# Programming Guide)

Update

41686d6564 made an interesting point

It's hard to tell if you are actually trying to pass the type into the methods parameter:

DoSomething<Bus>(whatYouPassInHereWouldNeedToBeAnInstanceNotAtype,"str");`

If you don't need to use the instance, there is no need to pass it in. Just use the generic parameter

public void DoSomething<T>(string str)

...

DoSomething<Bus>("str");
halfer
  • 19,824
  • 17
  • 99
  • 186
TheGeneral
  • 79,002
  • 9
  • 103
  • 141
  • Also, shouldn't the passed _argument_ be an instance instead of the type? E.g., `DoSomething(someBus, "str");`? If OP doesn't really need an instance, then the first parameter can be dropped altogether and the method signature would be `public void DoSomething(string str)`. – 41686d6564 stands w. Palestine Aug 22 '20 at 06:57
  • @41686d6564 yeah it does look they are passing a type in the parameter. I just put it down to "Who know what casing style kids use these days" type of thing – TheGeneral Aug 22 '20 at 06:59
  • It's not just the casing. The actual type name is `Bus` (based on the usage of `List` and `DoSomething`). It would've been a casing issue if it was something like `DoSomething(MyBus,..)` as opposed to `myBus`. – 41686d6564 stands w. Palestine Aug 22 '20 at 07:02
  • @41686d6564 yeah i know what you mean, it still could be an instance `Bus` as well, Its just really hard to know whats going on here without the op commenting. Ie is it a property of type `Bus` named `Bus`. – TheGeneral Aug 22 '20 at 07:03
  • @41686d6564 i tried to make the point obvious, but i have run out of time, if you think you can be a bit more elegant in the wording, feel free to edit. – TheGeneral Aug 22 '20 at 07:05
  • The updated part is the what I want. Thank you Michael and @41686d6564 – Adrian Aug 24 '20 at 00:58
0

It seems that you either have a typo or the list is not defined correctly

public void DoSomething<T>(T a, string str)
{
   // Incorrect
   var temp = new List<a>();

   // Correct
   var temp = new List<T>();

   // ...
}

DoSomething<Bus>(Bus, "str");
DoSomething<Car>(Car, "str");

Maybe this helps...

sunriax
  • 592
  • 4
  • 16