c# · level 3 · unit 1 · lesson 4
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A constraint can name your own interface, or demand new().
Constraints can name your own interfaces. where T : IHasId lets a generic class read item.Id from any type that promises one. That is enough for a : one class that stores and finds products, customers or orders by id, written once.
When nothing matches, return default: the empty value of whatever T is. That is null for classes and records, 0 for int and false for bool. You cannot write null there, because T might be a number.
Stack constraints with commas. Adding the , as in where T : IHasId, new(), allows new T() inside.
using System;
using System.Collections.Generic;
var shelf = new Repository<Product>();
shelf.Add(new Product(7, "kettle"));
shelf.Add(new Product(9, "toaster"));
Console.WriteLine(shelf.Find(9).Name);
Console.WriteLine(shelf.Find(3) == null);
interface IHasId { int Id { get; } }
record Product(int Id, string Name) : IHasId;
class Repository<T> where T : IHasId
{
private Dictionary<int, T> items = new Dictionary<int, T>();
public void Add(T item) => items[item.Id] = item;
public T Find(int id)
{
if (items.ContainsKey(id)) return items[id];
return default;
}
} The record's Id already has a get, so Product keeps the promise IHasId makes.
guess first
Three empty values. What will this show?
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
Console.WriteLine(EmptyOf<int>());
Console.WriteLine(EmptyOf<bool>());
Console.WriteLine(EmptyOf<string>() == null);
T EmptyOf<T>() => default;
your turn
Your turn. Make a blank settings form.
On line 7, add the constraint that lets Blank make a fresh T. Then choose your own theme name on line 4.
output
press run to see what happens
fix it
The team list will not compile. Can you fix it?
output
press run to see what happens
- C# refuses
nullon line 20. What ifTwereint? Tcould be a number, and numbers are nevernull. One keyword means the empty value of any type.- Change
return null;toreturn default;, then run.
nailed it
you built a repository for any type. :D
- Constraints can name your own interfaces.
defaultis the empty value of anyT.new()lets generic code make a freshT.
new words in your collection
- repository
- new() constraint
This lesson isn’t saved. A free account remembers every lesson you finish.