How generics can be used in our application

B

Bhuwan Bhaskar

Hi,

What are generics? How generics can be used in our application?

Thanks & Regards
Bhuwan
 
S

Scott M.

Do you have a need for strongly-type collections of things?

If so, generics are for you.
 
P

Peter Duniho

Scott said:
Do you have a need for strongly-type collections of things?

If so, generics are for you.

Sure, but generics are a lot more than that. The most visible example
of the use of generics in C# are the generic collections, but that's
just one example of how one might use generics.

A very simple answer, for someone that is already familiar with C++
templates, is that generics address a similar need. So where you'd use
a template in C++, you can use generics in C# (generally speaking).

And if you're not already familiar with C++ templates, the reason you'd
use templates or generics is to implement code that can be written in a
generic way (hence the name), not specific to the type that will
actually be used in the concrete use of the code. Then you can reuse
that code without creating new versions of it for each concrete type for
which it's going to be used.

In addition to that reuse advantage, generics also have the advantage
that for reference types the same compiled code is used for all uses of
the generic type (as opposed to doing "copy, paste, search and replace"
which produces a whole new instance of the compiled code for each use).

It's hard to sum up every way in which generics are useful in a single
post, but hopefully the above does some justice to the question. I
would definitely agree that the next step for anyone considering the
question is to read the MSDN documentation on the topic.

Pete
 
S

sloan

public class Employee{}


public class EmployeeCollection : List<Employee>
{

}

instantiate an instance of EmployeeCollection, and see all the wonderful
methods, that are STRONGLY TYPED.

ex:

EmployeeCollection ec = new EmployeeCollection();

Employee e = new Employee();

ec.Add ( e ) ; //<< Notice the add takes an Employee as a param, not an
"object".


That's def not the only thinng you can do. but its a start. The benefit is
that anytime you need a collection, all you do is this:

List < MyCustomObject > = new List <MyCustomObject >();
//or use my method above of EmployeeCollection.

Because you get strong typing, your code is alot less buggy.

Check this blog entry out:
http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!151.entry

It gives you access to the Session object, but in a strongly typed way ...

But you can also see how to use generics in a slightly different way that
just collection based ones.
 
S

Scott M.

The OP was so vauge, I didn't bother to go into details. It sounds like the
OP isn't even sure of what he needs to do, so I just gave him a carrot to
nibble on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Generics newbie 3
When to use class and when structure 9
Generics? 12
foreach with generics and without generics 3
generics 3
Generics 2
What type objects can we can not serialiaze in dot net ? 2
using Generics 3

Top