Generics in C sharp Arabic #55

 


Generics in C sharp Arabic #55

Suggested Videos

❑ Generics.

❑ Advantages of using generics.





Generics.

Generics are introduced in C# 2.0 Generics allow us to design classes and methods decoupled from the data types.

Generic class are extensively used by collection classes available in System.Collections.Generic namespace .(Covered in the next seesion).

One way of making AreEqual() method reusable, is to use object type parameters. Since , every type in NET directly or indirectly inherit from System.Object type, AreEqual() method works with any data type, but the problem is performance Degradation due to boxing and unboxing happing.

Also ,AreEuqal() method is no longer type safe . It is now possible to pass integer for the first parameter, and a string for the second parameter. It doesn’t really make sense to compare strings with integers.

So, the problem with using System.Object type is that 1-Are Equal() method is not type safe. 2-Performance degradation due to boxing and unboxing.




Generics

To make AreEqual() method generic , we specify a type parameter using angular brackets as shown below.
Public static bool AreEqual<T>(T value1, T value2)

\At the point , when the client code wants to invoke this method, the y need to specify the type, they want the method to operate on. If the user wants the AreEqual() Method to work with integers, they con invoke the method specifying int as the datatype using angular brackets as shown below.

Bool Equal=Calculator.AreEqual<int>(2,1);

To operate with string data type.
Bool Equal =Calculator.AreEqual<string>(’’A’’,’’’B’);

In this example, we made the method generic.Along the same lines. It is also possible to make classes, interfaces and delegates generic.

Follow Us

YouTube

 If you want to download pptx : click Telegram

Facebook

Twitter

Instagram

Tik Tok














إرسال تعليق

أحدث أقدم