Why do C # and VB have Generics? What benefits do they provide? Generics, FTW

From Wikipedia :

General programming is a style of computer programming in which algorithms are written in terms of to-be-defined-later, which are then created if necessary for specific types provided as parameters and was initiated by Ada, which appeared in 1983. This approach allows the writing of common functions or types that differ only in the set of types on which they work when they are used, thereby reducing duplication.

Generics provide the ability to define types specified later. You do not need to throw elements into a type in order to use them, because they are already typed.

Why do C # and VB have Generics? What benefits do they provide? What benefits do you use for them?

What other languages โ€‹โ€‹do generics have?

+6
generics c #
source share
15 answers

C # and VB have generics to use generic support in the base CLR (or vice versa?). They allow you to write code in a statically typed language, which can be applied to several types without rewriting the code for each type you use (runtime will do it for you) or in another way using System.Object and casting everywhere (as, for example, we dealt with ArrayList).

Have you read the article?

These languages โ€‹โ€‹also have generics:

  • C ++ (via templates)
  • Ada (via templates)
  • Eiffel
  • D (via templates)
  • Haskell
  • Java
+6
source share

Personally, I think that they save a lot of time. I still use the .NET Framework 1.1, and each time you need a specific collection, you need to create a strongly typed collection, implementing CollectionBase. With Generics, you just need to declare your collection as a List<MyObject> and do it.

+5
source share

Consider these method signatures:

 //Old and busted public abstract class Enum { public static object Parse(Type enumType, string value); } //To call it: MyEnum x = (MyEnum) Enum.Parse(typeof(MyEnum), someString); 

 //New and groovy public abstract class Enum { public static T Parse<T>(string value); } //To call it: MyEnum x = Enum.Parse<MyEnum>(someString); 

Take a look at ma: No runtime manipulation.

+3
source share

From MSDN :

General tools provide a solution to the restriction in earlier versions of the total runtime and C # language in which generalizations are performed by type casting from an object of a universal base type. By creating a generic class, you can create a collection that is type safe at compile time.

Read the rest of this article to see some examples of how Generics can improve the readability and performance of your code.

+2
source share

Probably the most common use for them is the strongly typed ArrayLists. In .NET 1.1, you need to either drop all objects from your desired type, or use something like CodeSmith to create a strongly typed ArrayList.

In addition, they help reduce boxing . Again, in .NET 1.x, if you tried to use an ArrayList with a value type, you end the box and unpack the objects everywhere. Generics avoid this by letting you define a type, whether it be Reference or Value.

There are other applications that are convenient for them: event handlers, LINQ queries, etc.

+2
source share

Generics in .NET is great for object collections. You can define your object type as you like, and have, say, a list without writing any code for this, and have access to all the effective functions of the universal .NET List collection, while being safe for the T-type. great stuff.

+1
source share

The generalization is based on the concept of templates in C ++, if you are familiar with them.

Its a way to implement an algorithm or data structure, but delays the actual type in which it is used.

Then the list can be assigned by any type of your choice: int, string, and even custom types, which is assigned to the type when building the list. But you can use add remove list operations, etc.

You can really save a lot of coding effort by getting used to generics. And you do not need to insert and unpack between types.

Java also has generics. They are called wildcards.

+1
source share

Generalizations in .net, such as inheritance and extension methods, reduce code duplication. Let me explain with refactoring.

If all classes with a common ancestor have a common method, put the common method in the common class ancestor (inheritance).

If some classes have a common method that uses a public contract to achieve some result, make a general method in the extension method of this public contract.

If several methods or classes have the same code that differs only in the types that they act on (especially when the details of this type are not related to the operation of the method), collect these methods or classes into a common one.

+1
source share

They increase performance for collections using value types, since boxing / unpacking is not required. They are much cleaner to use since you donโ€™t need to throw the object (for example, using ArrayList) on the type you need โ€” and they also help ensure type safety.

+1
source share

The biggest advantage of generics regarding non-generic types in C # (rather than Java, Java is a completely different story) is that they are much faster. JIT generates the best machine code that may arise for a given type. List<int> is actually a list of int objects, not integer objects wrapping int. This creates the typical awesomely fast types, as well as the safe type, which can help you detect a huge mass of errors at compile time :)

+1
source share

A common example is collections. for example, a set of type T, such as the Add (T) method and the T get () method. The same code, secure collections of different types.

C ++, D, Ada and others have templates, a super-set of generics that make this a slightly different mistake get the same final result (and then some).

IIRC Java has generics, but I don't do Java.

0
source share

The easiest way to explain this is to give an example. Suppose you want two hash tables that map objects of type string to type int and one that maps objects of type string to double input. You can define a Hashtable, and then use the types K and V. Without generics, you will need to use the type "object", which, in addition to being distinguished, makes sense, refuses types. Just create a Hashtable and a Hashtable, and you have your hash tables with proper type checking and that's it.

0
source share

Java also has generics. C ++ has templates.

Dynamic languages โ€‹โ€‹like Perl and Javascript do not have the same type restrictions, so they get basically the same benefits with less work.

0
source share

In objective-C, you can use protocols to achieve generic goals. Since the language is weakly typed, it usually doesnโ€™t bother as much as when you struggle with the type system in order to use the same code path for many types.

0
source share

Personally, I'm a big fan of generics because of all the code I don't need to write.

What is control inversion?

0
source share

All Articles