Why generics came into picture?
and detail concept of generic
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Upendra Pratap ShahiPosted Jun 2, 2015, 4:43 AM
Nilesh JadavPosted Jun 2, 2015, 3:17 AM
Generic : A generic method is a method that can accept any type of arguments. For most of the algorithms, logic is same whatever the type of data is. Hence instead of creating one method for every data type, you can create only one method that can accept any type of arguments, which is called as generic method.
Generics provides the type safe code with re-usability like as algorithm. In algorithms such as sorting, searching, comparing etc. you don’t specify what data type(s) the algorithm operates on. The algorithm can be operates with any types of data. In the same way Generics operate, you can provide different data type to Generics. For example, a sorting algorithm can operates on integer type, decimal type, string type, DateTime type etc.
Advantage of Generic:-
1. Generics provide type safety without the overhead of multiple implementations. Ex. We can create a linked list of string. LinkedListlinkList=new LinkedList(); There is no need to inherit from a base type and override members.The linked list is ready for immediate use.
2. There is no need to write code to test for the correct data type because it is enforced at compile time. The need for type casting and the possibility of run-time errors are reduced.
3. Generic collection types generally perform better for storing and manipulating value types because there is no need to box the value types
. 4. Generic delegates enable type-safe callbacks without the need to create multiple delegate classes.
5.Generic delegates can also be used in dynamically generated code without requiring the
This is the syntax of generics:
{
}
Here is the program for it :
using System;
//Example program for Generics in c#.net
namespace ProgramCall
{
class Program
{
//Generic Method
public static void Swap
{
MyType T;
T = X;
X = Y;
Y = T;
}
static void Main(string[] args)
{
int A = 10, B = 20;
float F1 = 3.5F, F2 = 5.5F;
string S1 = "Nilesh", S2 = "Microsoft";
//Calling generic method using int types variables
Swap<int>(ref A, ref B);
//Calling genrics method using float type variables
Swap<float>(ref F1, ref F2);
//Calling genric method using string variables
Swap<string>(ref S1, ref S2);
Console.WriteLine("After Swap ...");
Console.WriteLine("A = {0}\tB = {1}", A, B);
Console.WriteLine("F1 = {0}\tF2 = {1}", F1, F2);
Console.WriteLine("S1 = {0}\tS2 = {1}", S1, S2);
}
}
}
After Swap ...
A = 20 B = 10
F1 = 5.5 F2 = 3.5
S1 = Microsoft S2 = Nilesh
Hope this work for you !
Manoj BhoirPosted Jun 2, 2015, 3:15 AM
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.
You write the specifications for the class or the method, with substitute parameters for data types. When the compiler encounters a constructor for the class or a function call for the method, it generates code to handle the specific data type.