English speaking and learning Interview Questions and Answers Programs C# Concept OOPS Concept JOBS

Saturday, May 13, 2017

Generics in C#

Generics enables the creation of classes, structures,interfaces, methods, and delegates that work in a type-safe manner with various kinds of data.

if you apply generics on class,it can avoid his feature of abstract or customized data type,so that type can behave single type only at run time.it will give only one type and object will hold one type of data.
object is the base class of all other classes, an object reference can refer to any type of object. Thus, in pre-generics code, generalized code used object references to operate on a variety of different kinds of objects.The problem was that it could not do so with type safety because casts were needed to convert between the object type and the actual type of the data. This was a potential source of errors because it was possible to accidentally use an incorrect cast. Generics avoid this problem by providing the type safety that was lacking.

By Generics, we can create multiple types of any class.
By Generics,we can achieve polymorphism(static).

collection of multiple data is hold in single data type  - Collections
collection of multiple data is hold in multiple data type - Generics

Generics allow you to delay the specification of the data type of programming elements in a class or a method, until it is actually used in the program. In other words, generics allow you to write a class or method that can work with any data type.

Creating Generic List<T> Collections

Generic List collection is similar to arrays. You declare the List, populate its members, and then access the members. Here's a code example of how to use a List.

Sample Generic List <int>
List<int> myValue = new List<int>();
Sample Generic List <string>
List<string> myValue = new List<string>();

The above two codes can be replaced by a single line of code using Generics concept.

List<T> myValue = new List<T>();

Based on the usage, the [T] will behave as either “int” or “string” or any data type based on the executing application usage.

Benefits of using Generics

Generics in .NET let you reuse code and the effort you put into implementing it. The types and internal data can change without causing code bloat, regardless of whether you are using value or reference types. You can develop, test, and deploy your code once, reuse it with any type, including future types, all with full compiler support and type safety. 

The generic code does not force the boxing and unboxing of value types, or the down casting of reference types, performance is greatly improved. 

Generics help to decouple to logic of data type.It is type safe, full of conversion . if you want to create a function that gives output of your type,function type should be generics type.
by Generics,we achieve categorization,classification,standard.

Step 1 - 
 Create a class 'Csharplearing' . to make this class generics type , we use <>. In this bracket , we can define our data type.This can be any data type at compile time like string,int,number. in class,create a function 'show', that type is generics data type.

class Csharplearing<anydatatype>
    public anydatatype show(anydatatype t)
        return t;


Step 2 -
Create object of class 'csharplearning' .
in data type, if we pass string , show method return string.
if we pass int, show method return int.
Type is also checked at compile time.

class Program
    static void Main(string[] args)
        Csharplearing<string> c = new Csharplearing<string>();
        string b = c.show("Generics");

        Csharplearing<int> c1 = new Csharplearing<int>();
        int b1 = c1.show(10);




Step 3

You also like.

1)Abstraction and Encapsulation in OOPS

2)Inheritance in OOPS

3)Polymorphism in OOPS

4)Interface in OOPS

5)What is Virtual Function

6)What is Abstract class and Abstract function

7)What is Static Class and Static Members
8)What is Collections


1)How to make a Registration Page using CAPTCHA in C#

2)How to make a Login Window in C# - Step by Step

3)How to make PhoneBook in C#

C# Interview questions and answers

1) C# Interview Questions and Answers

2)C# Interview Questions and Answers Part 2

3)C# Interview Questions and Answers Part 3

4)C# Interview Questions and Answers Part 4    

1 comment :