What is cool about generics why use them

0 votes
What are generics, what are their benefits, why should I use them, where should I use them, and how should I use them?

Keep the answers simple
Jun 11 in C# by jyoti
• 1,240 points
15 views

1 answer to this question.

0 votes

Okay. For simplification, I am listing it as points:

  1. Makes it possible to create type-safe code or use library methods; for example, a list of strings is always guaranteed to be a list.
  2. Because generics are utilised, the compiler is able to check the type safety of code at build time, for example, are you thinking to insert an int into that list of strings? This runtime error might be less obvious if you used an ArrayList.
  3. It is faster than using objects since it does not require casting from objects to the desired reference  type or boxing/unboxing (where.net must convert value types to reference types or vice versa).
  4. It gives you the ability to write code that is applicable to many types with the same underlying behaviour. For example, a Dictionary <string, int> uses the same code type as a Dictionary <DateTime, double>; using generics, the framework team only had to write one piece of code to achieve both results with the benefits.
answered Jun 23 by krishna
• 2,540 points

Related Questions In C#

0 votes
1 answer

What is the best C# to VB.net converter

Telerik has a solid SharpDevelop-based converter that ...READ MORE

answered May 30 in C# by rajiv
• 1,620 points
69 views
0 votes
1 answer

Why does one use dependency injection?

I believe that many people are perplexed ...READ MORE

answered Jun 6 in C# by pranav
• 2,580 points
23 views
0 votes
1 answer

What is the difference between C# and .NET?

C# is a programming language, and.NET refers ...READ MORE

answered Jun 7 in C# by pranav
• 2,580 points
23 views
0 votes
1 answer

What is typing discipline

Wikipedia's typing discipline relates to the type ...READ MORE

answered Jun 14 in C# by jyoti
• 1,240 points
26 views
0 votes
1 answer

Why isn't Array a generic type

They basically copied the concept of arrays ...READ MORE

answered Jun 21 in C# by jyoti
• 1,240 points
13 views
0 votes
0 answers

String was not recognized as a valid DateTime " format dd/MM/yyyy"

I want to convert my string formatted ...READ MORE

May 1 in Other DevOps Questions by Kichu
• 19,040 points
197 views
0 votes
1 answer

Datatypes in C#

You might be beginner I think. Go ...READ MORE

answered Jun 9 in C# by rajiv
• 1,620 points
18 views
0 votes
0 answers

Creating a generic method in C#

I'm attempting to integrate several comparable techniques ...READ MORE

Jun 11 in C# by pranav
• 2,580 points
14 views
0 votes
1 answer

What does the extension method do in c# and why do you need it

Extension Methods allow you to add additional ...READ MORE

answered Jun 21 in C# by krishna
• 2,540 points
19 views
0 votes
1 answer

Is there an official logo of C#

There isn't an official logo, as far ...READ MORE

answered Jun 13 in C# by krishna
• 2,540 points
20 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP