C# Generic Methods

Description

A generic method declares type parameters within the signature of a method.

With generic methods, many algorithms can be implemented in a general-purpose way.

Example

Here is a generic method that swaps two values of any type:


static void Swap<T> (ref T a, ref T b)
{/* w ww. ja  v a2  s.co m*/
 T temp = a;
 a = b;
 b = temp;
}

Swap<T> can be used as follows:


int x = 5;
int y = 10;
Swap (ref x, ref y);




















Home »
  C# Tutorial »
    Custom Types »




C# Class
C# Struct
C# Interface
C# Inheritance
C# Namespace
C# Object
C# Delegate
C# Lambda
C# Event
C# Enum
C# Attribute
C# Generics
C# Preprocessor