C # Passing a general list to a method and then casting to type

I have a method that uses a list that I need to pass to it.

Depending on where I call this method, the type of the list is different.

Is it possible to transfer the list as a general list and then pass it to a specific type, or do I need to create parameters for each type of list that can be used (see below)?

public string MyMethod(List<Type1> list1, List<Type2> list2, ...etc ) { switch(someCondition) { case 1: //pass list1 into a method from class1 break; case 2: //pass list2 into a method from class2 break; ...//etc for several more lists } 
+8
list generics c #
source share
5 answers

Instead of creating a single method with multiple parameters, why not make multiple methods with the same list type? It seems you still don't share a lot of code from one List type to another.

 public string MyMethod(List<int> list) { //do something with list } public string MyMethod(List<bool> list) { //do something with list1 } ... 
+12
source share

you can create a generic function that takes a generic type like this

 public virtual List<T> SelectRecord <T>(int items, IEnumerable<T> list) { if (items == -1) return list.ToList<T>(); else if (items > list.Count<T>()) return list.ToList<T>(); else return list.Take<T>(items).ToList<T>(); } 

In my example, I passed IEnumerable to my function and returns List (where is the type of my object). I do not indicate what type of object is.

I hope this will be helpful.

+5
source share

You can do something like this:


 class GenericsExample1Class<T> { public string WhatsTheType(List<T> passedList) { string passedType = string.Empty; passedType = passedList.GetType().ToString(); return passedType; } } 

  private void ClienMethod() { string typeOfTheList = string.Empty; // Call Type 1: Value Type List<int> intergerObjectList = new List<int> { 1, 2 }; typeOfTheList = (new GenericsExample1Class<int>()).WhatsTheType(intergerObjectList); MessageBox.Show(typeOfTheList); // Call Type 2: Reference Type List<string> stringObjectList = new List<string> { "a", "b" }; typeOfTheList = (new GenericsExample1Class<string>()).WhatsTheType(stringObjectList); MessageBox.Show(typeOfTheList); // Call Type 2: Complex-Reference Type List<Employee> complexObjectList = new List<Employee> { (new Employee { Id = 1, Name = "Tathagat" }), (new Employee { Id = 2, Name = "Einstein" }) }; typeOfTheList = (new GenericsExample1Class<Employee>()).WhatsTheType(complexObjectList); MessageBox.Show(typeOfTheList); } 

+3
source share

Thanks for all the answers.

Another solution that I just discovered would be to use a new feature in C # 4.0:

 public string MyMethod(List<Type1> list1 = null, List<Type2> list2 = null, ...etc ) { switch(someCondition) { case 1: //pass list1 into a method from class1 break; case 2: //pass list2 into a method from class2 break; ...//etc for several more lists } } 

Then I could just call the method and not specify all the parameters:

 MyMethod(list1: mylist1) 
+1
source share

Yes, this can be done easily by passing a parameter of type Object, as shown below:

  public void Test(int SomeCondition,Object Param) { dynamic list; switch (SomeCondition) { case 0: list = (List<int>)Param; MessageBox.Show(list[0].ToString()); break; case 1: list = (List<string>)Param; MessageBox.Show(list[0].ToString()); break; default: MessageBox.Show("Default!"); break; } } 

You can call this function as follows:

  List<int> list1 = new List<int>(new int[]{1,2,3}); List<string> list2 = new List<string>(new string[] { "one", "two", "three"}); Test(0, list1); Test(1, list2); 
0
source share

All Articles