Generics
Box<T> ek generic class hai — T ek placeholder hai jo jahan bhi Box use ho wahan ek real type se fill ho jata hai. Box<string> aur ek hypothetical Box<int>, dono isi class ke valid, type-safe instantiations hain, aur compiler call site par koi bhi type mismatch pakar leta hai.
Main.cs
Khud try karo →using System;
class Box<T> {
private T value;
public Box(T value) { this.value = value; }
public T GetValue() { return value; }
}
class Program {
static void Main() {
Box<string> box = new Box<string>("hello");
Console.WriteLine(box.GetValue());
}
}Output
hello
Aage: collections aur LINQ — Where aur Select se collections query karna, C# ka signature feature jis ka is site par kahin aur koi real equivalent nahi hai.
Practice exercise
Ek generic class Box<T> define karein jis ka value field ho aur ek GetValue() method ho. Ek Box<string> banayein jis mein "hello" ho, phir us ka value print karein.