Generic Methods

These are declared with a type parameter

Method_Name<T> (System.Generics.List<T> mylist) 

This is an example of a Generic Method Definition.
The "T" is a type parameter that is used as a placeholder for any type.
This data type can be specified when the method is called.



protected T Method_Name<T>(System.Func<bool, T> Method_ToRun, string Arg1) 
{
   return Method_ToRun(false)
}

Swap_Values

This method will swap the values of two generic types.

public class GenericMethods 
{
   public static void Swap_Values<T>(
      ref T value1,
      ref T value2)
   {
      T temporary = value1;
      value1 = value2;
      value2 = temporary;
   }
}

int mysize = 5; 
System.Collections.Generic.Stack<double> StackOne;
System.Collections.Generic.Stack<double> StackTwo;

StackOne = new System.Collections.Generic.Stack<double>(mysize);
StackTwo = new System.Collections.Generic.Stack<double>(mysize);

// this swaps the entire collections
GenericMethods.Swap_Values<System.Collections.Generic.Stack<double>>(ref StackOne, ref StackTwo);

double ValueOne = 1.2;
double ValueTwo = 3.4;

// this swaps the individual values
GenericMethods.Swap_Values<double>>(ref ValueOne, ValueTwo);


© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext