Creating Custom Generic Methods
While most developers will typically make use of the existing generic types within the base class
libraries, it is certainly possible to build your own generic members and custom generic types. To
learn how to incorporate generics into your own projects, the first task is to build a generic swap
method. Begin by creating a new Console Application named GenericMethod.
The goal of this example is to build a swap method that can operate on any possible data type
(value-based or reference-based) using a single type parameter. Due to the nature of swapping algo-
rithms, the incoming parameters will be sent by reference (via the C# ref keyword). Here is the full
implementation of our generic swap method, contained within the initial Program class:
Do'stlaringiz bilan baham: |