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, 2022 in C# by jyoti
• 1,240 points
87 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, 2022 by krishna
• 2,820 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, 2022 in C# by rajiv
• 1,620 points
654 views
0 votes
1 answer

Why does one use dependency injection?

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

answered Jun 6, 2022 in C# by pranav
• 2,580 points
85 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, 2022 in C# by pranav
• 2,580 points
118 views
0 votes
1 answer

What is typing discipline

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

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

Why isn't Array a generic type

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

answered Jun 21, 2022 in C# by jyoti
• 1,240 points
74 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, 2022 in Other DevOps Questions by Kichu
• 19,040 points
632 views
0 votes
1 answer

Datatypes in C#

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

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

Creating a generic method in C#

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

Jun 11, 2022 in C# by pranav
• 2,580 points
85 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, 2022 in C# by krishna
• 2,820 points
125 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, 2022 in C# by krishna
• 2,820 points
67 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP