-2

Original Questions: I know the question sounds pretty "thin", since generic classes (interfaces) and collections go hand in hand. Out of curiosity and a desire to 'cover all the bases' ... are there uses for these generics other than as collections?

The response is that there are too many possibilities to make for a good thread, so let me try to clarify the question because I ( and probably others) will definitely benefit.

My revised question is:

What are applications of instantiated generics (not methods!) in addition to collections? So, now I know there are many ... however, classified by use... what are they?

A concise format for answers is:

Use: Short description or example

(ie) Collections: The generic allows for collections of objects and with a where T: constraint gives access to methods on all members of the collection. (link or reference).

I'm really eager to hear responses.

Stephan Luis
  • 911
  • 1
  • 9
  • 24
  • 3
    Yes. Lots. Just look through the base library and you'll find plenty of generic classes, many of which are not collections. – Servy Mar 04 '14 at 21:56
  • Yes. Take a look at the Reactive Extension for a different kind of view. It also supports LINQ. – Gábor Bakos Mar 04 '14 at 22:00
  • This question is not a good format for StackOverflow because it does not have a specific answer and its not about specific code. To briefly answer your question: think about `Lazy`, `Task`, `Func`, `Nullable`. None of those are collections. **Generics enable you to add super powers to your types**, like lazy computation, asynchronous computation, on-demand computation, and optionality. – Eric Lippert Mar 05 '14 at 16:42
  • @EricLippert I looked at `Lazy` on MSDN before asking for the post to be re-opened. Isn't `Lazy` a collection of objects used to access the Lazy members? – Stephan Luis Mar 05 '14 at 16:50
  • @StephanLuis: I think you don't understand what `Lazy` does. It computes a value either zero or one times, it does not compute the value until it is needed, and it stores the value computed so that it does not have to be computed a second time. What does that have to do with a collection of values? – Eric Lippert Mar 05 '14 at 16:53
  • You might find Eric Lippert's [Monads series](http://ericlippert.com/category/monads/) to be interesting. Anyhow, this updated question still doesn't strike me as a good fit for SO, since the answers will be lists of things. There isn't really a specific right answer; there's arbitrarily many right answers. – Brian Mar 05 '14 at 18:51
  • @EricLippert To follow your comment, not a collection of values a collection of objects of one or more. Like I said, my question is entirely speculative... are generics used for anything other than collecting or manipulating objects? Everyone says yes! – Stephan Luis Mar 05 '14 at 21:35
  • @StephanLuis: I am confused by your new question. Are **programs** in any imperative language anything other than *controlled manipulation of abstract objects*? – Eric Lippert Mar 05 '14 at 22:33
  • Regardless, this vague and speculative question is still a bad fit for StackOverflow. Try to stick to *specific* questions about *actual code*. If you want to start a conversation about the nature of generics, try another forum or post on a blog or some such thing. – Eric Lippert Mar 05 '14 at 22:35

2 Answers2

1

You can create not only generic types but also generic methods. Though the most common use of generics is for creating collections they are also used for many other purposes such as containers or algorithms.

class Point<T>
{
  T x;
  T y;
};

class Math<T>
{
  T Add(T a, T b);
};

You should also have a look at this discussion: What is cool about generics, why use them?.

Community
  • 1
  • 1
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
  • +1 I forgot about generic methods – BradleyDotNET Mar 05 '14 at 01:25
  • I think these are bad examples of generics since they are not generic at all. How can you implement `Math.Add`? How is `Point` better than `Int32Point` and `DoublePoint`? Can you reuse code? – adrianm Mar 07 '14 at 09:55
  • Coming back to this a year (after writing this pretty silly question) I have another resource that explains the power of generics by Anders Hejlsberg, https://channel9.msdn.com/Blogs/TheChannel9Team/Anders-Hejlsberg-Whats-so-great-about-generics – Stephan Luis Feb 08 '16 at 22:29
0

I've used generics for a "EventHandler" (with a restriction on the generic that the parameter implemented my BaseEvent class) when sending events via WCF to another piece of the system.

As the comments note, the answer is unequivocally yes. You use generics whenever multiple types (and ideally all types) should have the same behavior (and occasionally state). Collections are an easy example of this, but there are many, many other situations where this holds true and generics are a good choice.

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117