Here, Today, I would like to share the basics of generics. The Generic in C# is a concept that allows us to define classes and methods with the placeholder. The C# compiler replaces these placeholders with specified type at compile time. That is the concept of generics is used to create general-purpose classes and methods. When defining a generic class, we must use angle <> brackets. The angle brackets are used to declare a class or method as a generic type. In the following example, we are creating a generic class that can be used to deal with any type of data. Generics are so important because of that, we acquire the ability to create such classes, structures, interfaces, methods, and delegates, which work in type-safe for different types of data. The Generics(class or methods) in C# and .NET procedure many of the benefits of strongly-typed collections as well as provide a higher quality of and a performance boost for the code. The Generics are very similar to C++ templates but having a slight difference in such a way that the source code of C++ templates is required when the templates are instantiated with the specific type and .NET Generics are not limited to classes only.
Example - 1
using System; using System.Collections.Generic; using System.Linq;
using System.Text; using System.Threading.Tasks;
namespace ConsoleApplication2 { class Program { static void Swap<T>(ref T lhs, ref T rhs) { T temp; temp = lhs; lhs = rhs;
rhs = temp; } static void Main(string[] args) { int a, b; char c, d; a = 10; b = 20; c = 'I'; d = 'V';
// values before swap:
Console.WriteLine("Int values before calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values before calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d);
//call the swap Swap<int>(ref a, ref b); Swap<char>(ref c, ref d);
// values after swap:
Console.WriteLine("Int values after calling swap:"); Console.WriteLine("a = {0}, b = {1}", a, b); Console.WriteLine("Char values after calling swap:"); Console.WriteLine("c = {0}, d = {1}", c, d); Console.ReadKey(); } } }
Example - 2.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks;
delegate T NumberChanger<T>(T n);
namespace ConsoleApplication3 { class TestDelegate { static int num = 10;
public static int AddNum(int p) { num += p; return num; }
public static int MultNum(int q)
{ num *= q; return num; }
public static int getNum() { return num; } static void Main(string[] args) {
//create delegate instances NumberChanger<int> nc1 = new NumberChanger<int>(AddNum); NumberChanger<int> nc2 = new NumberChanger<int>(MultNum); //calling the methods using the delegate objects nc1(25); Console.WriteLine("Value of Num: {0}", getNum()); nc2(5); Console.WriteLine("Value of Num: {0}", getNum()); Console.ReadKey(); } } }
Liked By
Write Answer
Generics in C#
Join MindStick Community
You have need login or register for voting of answers or question.
Anonymous User
14-Dec-2018Generics in C#
Here, Today, I would like to share the basics of generics. The Generic in C# is a concept that allows us to define classes and methods with the placeholder. The C# compiler replaces these placeholders with specified type at compile time. That is the concept of generics is used to create general-purpose classes and methods. When defining a generic class, we must use angle <> brackets. The angle brackets are used to declare a class or method as a generic type. In the following example, we are creating a generic class that can be used to deal with any type of data. Generics are so important because of that, we acquire the ability to create such classes, structures, interfaces, methods, and delegates, which work in type-safe for different types of data. The Generics(class or methods) in C# and .NET procedure many of the benefits of strongly-typed collections as well as provide a higher quality of and a performance boost for the code. The Generics are very similar to C++ templates but having a slight difference in such a way that the source code of C++ templates is required when the templates are instantiated with the specific type and .NET Generics are not limited to classes only.
Example - 1
Example - 2.