0Pricing
C# Academy · Lesson

Generic methods/types; constraints

Write generic methods and types; add constraints with where T : … (class, struct, new(), interface/base).

Generic methods/types; constraints is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Generics overview

Goal: Reuse code safely with generics.

  • Generic methods & types
  • Constraints: class, struct, new(), interface/base
  • Keep examples small and clear

Generic method

A generic method has a type parameter list, e.g., Identity<T>, and works for many types.

using System;

// Generic method works for any T
public class Program
{
  static T Identity<T>(T value)
  {
    // just returns what it got
    return value;
  }

  public static void Main(string[] args)
  {
    int a = Identity<int>(5);
    string b = Identity<string>("hi");
    Console.WriteLine(a + " & " + b);
  }
}

Generic type

A generic type (like Box<T>) is compiled once and used with many concrete types.

using System;

// Simple generic type to hold a value
public sealed class Box<T>
{
  private T _value;
  public Box(T value) { _value = value; }
  public T Get() { return _value; }
  public void Set(T value) { _value = value; }
}

public class Program
{
  public static void Main(string[] args)
  {
    Box<int> bi = new Box<int>(10);
    Box<string> bs = new Box<string>("ok");
    Console.WriteLine(bi.Get() + " | " + bs.Get());
  }
}

new() constraint

Add where T : new() if you need to call new T() inside generic code.

using System;

// Factory that can create T with a parameterless ctor
public static class Factory
{
  public static T Create<T>() where T : new()
  {
    return new T(); // allowed thanks to new() constraint
  }
}

public sealed class Person
{
  public string Name;
  public Person() { Name = "Anon"; }
}

public class Program
{
  public static void Main(string[] args)
  {
    Person p = Factory.Create<Person>();
    Console.WriteLine(p.Name);
  }
}

Interface constraint

Constrain T to an interface or base so you can call its members (e.g., IComparable<T>).

using System;

// Min for two values that can be compared
public static class Utils
{
  public static T Min<T>(T a, T b) where T : IComparable<T>
  {
    return a.CompareTo(b) <= 0 ? a : b;
  }
}

public class Program
{
  public static void Main(string[] args)
  {
    Console.WriteLine(Utils.Min(5, 3));          // 3
    Console.WriteLine(Utils.Min("b", "a"));      // a
  }
}

Class/struct constraints & tips

Other constraints:

  • where T : class → reference types only
  • where T : struct → non-nullable value types
  • Combine constraints: where T : class, IDisposable, new()
  • Keep APIs minimal; prefer interface constraints for flexibility.

new() constraint meaning

Quick check: Which constraint lets you create a new T() inside generic code?

Recap

Recap: Create generic methods/types and add constraints to use required members: interface/base for behavior, class/struct for kind, and new() to call a parameterless constructor.

Frequently asked questions

Is the “Generic methods/types; constraints” lesson free?

Yes — the full text of “Generic methods/types; constraints” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic methods/types; constraints”?

Write generic methods and types; add constraints with where T : … (class, struct, new(), interface/base). You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.

How long does the “Generic methods/types; constraints” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generic methods/types; constraints
  2. Variance (in/out) with interfaces & delegates
  3. Nullable reference types (awareness) & annotations (C# 6 patterns)
← Back to C# Academy