Generics were added in C# version 2.0 and are one of the most important concepts in the language. They enable you to write reusable, high-performance code that is type-safe at compile time. Generics allow you to use a type in your code without knowing the type beforehand.
Generics are used in many places in .NET, including collections, delegates, and asynchronous code. With generics, you don't need to know the size of the collection in advance, and you can use generics with any element type, even custom data types specific to your code. C# provides support for generic types (classes, interfaces, etc.) and generic methods.
In generics, you have type parameters and type parameters. This is similar to a method that has parameters that you can pass to the method.
The syntax for declaring a generic type consists of type parameters enclosed in angle brackets. The name of the type. For example, Locator
public class Locator<T> { }
To create an instance of Locator
var stringLocator = new Locator<string>();
You can use type parameters (T) on class methods as shown in the following example.
public class Locator{ public IList Items { get; set; } public T Locate(int index){ return Items[index]; } } var stringLocator = new Locator<string>(); string item = stringLocator.Locate(2);
Another benefit of generics is the IntelliSense provided by the editor. When you type stringLocator.Locate(4) in Visual Studio or VS Code and hover over the method name; it shows that it returns a string instead of a T. The compiler will throw an error if you try to assign the result to any type other than string. For example,
// Error: Cannot implicitly convert type 'string' to 'int' [c-sharp]csharp(CS0029) int item = stringLocator.Locate(2);
When inheriting from a generic base type or generic interface, the generic type can use type parameters as type arguments. The generic LinkedList
public class LinkedList<T> : IEnumerable<T>
A generic method is simply a method that declares a type parameter that you can use inside a method and use it as parameter and return type. In the following example, Swap
public class Swapper{ public T Swap<T>(T first, T second){ T temp = first; first = second; second = temp; return temp; } }
Like generic types, when you call a generic method, it returns a strongly typed variable.
var swapper = new Swapper(); int result = swapper.Swap<int>(5, 3);
Can have multiple common parameters. The Dictionary class in the System.Collections.Generic namespace has two type parameters, key and value.
public class Dictionary<TKey, TValue>
Finally, it’s important to understand what is universal. For types, everything can be generic except enumerations. These include -
For type members, only methods and Nested types can be generic. The following members cannot be general -
Live demonstration
using System; using System.Collections.Generic; class Program{ static void Main(){ var stringLocator = new Locator<string>(){ Items = new string[] { "JavaScript", "CSharp", "Golang" } }; string item = stringLocator.Locate(1); Console.WriteLine(item); // CSharp var swapper = new Swapper(); int a = 5, b = 3; int result = swapper.Swap<int>(ref a, ref b); Console.WriteLine($"a = {a}, b = {b}"); } } public class Locator<T>{ public IList<T> Items { get; set; } public T Locate(int index){ return Items[index]; } } public class Swapper{ public T Swap<T>(ref T first, ref T second){ T temp = first; first = second; second = temp; return temp; } }
CSharp a = 3, b = 5
The above is the detailed content of Explain how generics work in C#. For more information, please follow other related articles on the PHP Chinese website!