A generic method can work with different value types using one method body.

generic method A generic method has a type parameter such as `T`.

Generic Methods

useSecond
GenericMethods.cs
Replay: real traced execution (multi-file project)
using System;

class Program
{
    static T Pick<T>(T first, T second, bool useSecond)
    {
        if (useSecond)
        {
            return second;
        }

        return first;
    }

    static void Main()
    {
        bool useSecond = false;
        string word = Pick("alpha", "beta", useSecond);
        int number = Pick(10, 20, useSecond);

        Console.WriteLine($"useSecond={useSecond}");
        Console.WriteLine($"word={word}");
        Console.WriteLine($"number={number}");
    }
}
using System;

class Program
{
    static T Pick<T>(T first, T second, bool useSecond)
    {
        if (useSecond)
        {
            return second;
        }

        return first;
    }

    static void Main()
    {
        bool useSecond = true;
        string word = Pick("alpha", "beta", useSecond);
        int number = Pick(10, 20, useSecond);

        Console.WriteLine($"useSecond={useSecond}");
        Console.WriteLine($"word={word}");
        Console.WriteLine($"number={number}");
    }
}
  1. useSecond ← False

    15static void Main()16{17    bool useSecond→ False = false; //@useSecond=true, false18    string word = Pick("alpha", "beta", useSecondFalse);19    int number = Pick(10, 20, useSecond);
  2. static T Pick<T>(T first, T second, bool useSecond)

    pass 1 of 2
    4{5    static T Pick<T>(T firstalpha, T secondbeta, bool useSecondFalse)6    {7        if (useSecond)8        {9            return second;10        }1112        return firstalpha;13    }
  3. word ← alpha

    17bool useSecond = false; //@useSecond=true, false18string word→ alpha = Pick("alpha", "beta", useSecondFalse);19int number = Pick(10, 20, useSecondFalse);
  4. static T Pick<T>(T first, T second, bool useSecond)

    pass 2 of 2
    4{5    static T Pick<T>(T first10, T second20, bool useSecondFalse)6    {7        if (useSecond)8        {9            return second;10        }1112        return first10;13    }
  5. number ← 10

    18    string word = Pick("alpha", "beta", useSecond);19    int number→ 10 = Pick(10, 20, useSecondFalse);2021    Console.WriteLine($"useSecond={useSecondFalse}");22    Console.WriteLine($"word={wordalpha}");23    Console.WriteLine($"number={number10}");24}
    outputuseSecond=False
    word=alpha
    number=10
  1. useSecond ← True

    15static void Main()16{17    bool useSecond→ True = true;18    string word = Pick("alpha", "beta", useSecondTrue);19    int number = Pick(10, 20, useSecond);
  2. static T Pick<T>(T first, T second, bool useSecond)

    pass 1 of 2
    4{5    static T Pick<T>(T firstalpha, T secondbeta, bool useSecondTrue)6    {7        if (useSecond)
  3. if (useSecond)

    pass 1 of 2
    6{7    if (useSecondTrue)8    {9        return secondbeta;10    }
  4. word ← beta

    17bool useSecond = true;18string word→ beta = Pick("alpha", "beta", useSecondTrue);19int number = Pick(10, 20, useSecondTrue);
  5. static T Pick<T>(T first, T second, bool useSecond)

    pass 2 of 2
    4{5    static T Pick<T>(T first10, T second20, bool useSecondTrue)6    {7        if (useSecond)
  6. if (useSecond)

    pass 2 of 2
    6{7    if (useSecondTrue)8    {9        return second20;10    }
  7. number ← 20

    18    string word = Pick("alpha", "beta", useSecond);19    int number→ 20 = Pick(10, 20, useSecondTrue);2021    Console.WriteLine($"useSecond={useSecondTrue}");22    Console.WriteLine($"word={wordbeta}");23    Console.WriteLine($"number={number20}");24}
    outputuseSecond=True
    word=beta
    number=20