0

Question: How do I communicate which types my generic method can be used with?

I want to use generics to create methods that are compatible with three or four different types (rather than all types). Here's a contrived example:

 public List<T> GetAll<T>()
 {
      Type typeParameterType = typeof(T);
      if(typeParameterType == typeof(string))
      {
           var myList = new List<string>() { "one", "two", "three" }; 
           return myList.Cast<T>().ToList(); 
      }
      else if (typeParameterType == typeof(int))
      {
           var myList = new List<int>() { 1, 2, 3 };
           return myList.Cast<T>().ToList();
      } 
      return new List<T>(); 
 }

 var intResult = GetAll<int>();
 var stringResult = GetAll<string>(); 

Here I am using int and string, but in my real code they would be class objects. This functions fine, but to other programmers they won't know what they can put into <T> because the logic of what it supports is internal.

Is there any way I can say GetAll<int>, GetAll<string>, GetAll<bool>, etc in such a way that intellisense will "see" it and be able to communicate it to users of my code.

I am trying to move away from this style:

  public List<int> GetAllInt();   
  public List<int> GetAllString(); 
  public List<int> GetAllBool();     

Since it makes it bloats out the interface. I want them to be able to see a small list of functions they can do, and then to pick the type after they've picked the activity, so like GetAll<int>() seems cleaner to me. But having no way to communicate what is compatible is a road block.

I have seen the where T : IMyInterface which does restrict what they can send, but it doesn't communicate what it is they can send.

Safari Jones
  • 470
  • 1
  • 4
  • 13
  • 3
    why don't you just write 3 or 4 methods? this is a violation of the concept of generics. – Daniel A. White Apr 08 '15 at 15:20
  • possible duplicate of [How to define generic type limit to primitive types?](http://stackoverflow.com/questions/805264/how-to-define-generic-type-limit-to-primitive-types) – Sean Vieira Apr 08 '15 at 15:21
  • @SeanVieira string isn't covered. – Daniel A. White Apr 08 '15 at 15:22
  • this isn't "generics" as such; it is type-specificity shoe-horned into a generic API – Marc Gravell Apr 08 '15 at 15:23
  • Agreed @Daniel - this just seemed like a reasonably close approximation of the *type* of thing that the OP is looking for (but from the edit it doesn't seem so) – Sean Vieira Apr 08 '15 at 15:28
  • You may want to look at http://stackoverflow.com/q/10833918/135978 – Sean Vieira Apr 08 '15 at 15:28
  • The edit was just to fix less than/greater than characters outside of the code blocks. I didn't add or remove any text from the original post. I am open to other ideas about how best to do this. I am not dead set on generics, I am just trying to make the external interface as clear as possible, and generics seemed to improve readability (e.g. going from 20+ methods down to six). – Safari Jones Apr 08 '15 at 15:31

1 Answers1

1

There isn't a good way of expressing that, because that isn't how generics are meant to be used. The closest you'll get is via intellisense:

/// <summary>Although this looks generic, it only really works
/// for String, Int32, Boolean; sorry 'bout that</summary>
public List<T> GetAll<T>() {...}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900