Hi,

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

Thanks & Regards
Bhuwan

Re: How generics can be used in our application by guffa

guffa
Sun Oct 07 06:53:07 PDT 2007

Bhuwan Bhaskar wrote:
> Hi,
>
> What are generics? How generics can be used in our application?
>
> Thanks & Regards
> Bhuwan
>

http://www.google.com/search?q=generics+c%23

--
Göran Andersson
_____
http://www.guffa.com

Re: How generics can be used in our application by Scott

Scott
Sun Oct 07 08:26:57 PDT 2007

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

If so, generics are for you.

"Bhuwan Bhaskar" <kxxx@gmail.com> wrote in message
news:OtGaxKMCIHA.3884@TK2MSFTNGP05.phx.gbl...
> Hi,
>
> What are generics? How generics can be used in our application?
>
> Thanks & Regards
> Bhuwan
>



Re: How generics can be used in our application by Peter

Peter
Sun Oct 07 10:04:57 PDT 2007

Scott M. wrote:
> "Bhuwan Bhaskar" <kxxx@gmail.com> wrote in message
> news:OtGaxKMCIHA.3884@TK2MSFTNGP05.phx.gbl...
>> Hi,
>>
>> What are generics? How generics can be used in our application?
>
> 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

Re: How generics can be used in our application by sloan

sloan
Sun Oct 07 19:14:49 PDT 2007


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.



"Bhuwan Bhaskar" <kxxx@gmail.com> wrote in message
news:OtGaxKMCIHA.3884@TK2MSFTNGP05.phx.gbl...
> Hi,
>
> What are generics? How generics can be used in our application?
>
> Thanks & Regards
> Bhuwan
>



Re: How generics can be used in our application by Scott

Scott
Mon Oct 08 11:50:16 PDT 2007

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.


"Peter Duniho" <NpOeStPeAdM@NnOwSlPiAnMk.com> wrote in message
news:13gi4dpphhilj55@corp.supernews.com...
> Scott M. wrote:
>> "Bhuwan Bhaskar" <kxxx@gmail.com> wrote in message
>> news:OtGaxKMCIHA.3884@TK2MSFTNGP05.phx.gbl...
>>> Hi,
>>>
>>> What are generics? How generics can be used in our application?
>>
>> 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