public class GenericList<T>{
....
void AddInput(T input) { .. }
}
class Program {
private class SomeClass {}
static void Main() {
GenericList<int> list1 = new GenericList<int>();
GenericList<string> list2 = new GenericList<string>();
GenericList<SomeClass> list3 = new GenericList<SomeClass>();
}
}
where T : struct // T must be value type
where T : class // T must be reference type
where T : new() // T must have public parameterless constructor
where T : <base class name> // T must derive from specified base class
where T : <interface name> // T must implement interface
where T : U // T must be or derive from parameterized U
// E.g.
class EmployeeList<T> where T : Employee, IEmployee, new() { .. }
class Base { .. }
class Test<T,U>
where T : struct
where U : Base, new()
{ .. }
static void Main() {
foreach(var num in someNums()) {
Console.WriteLine(num);
}
}
public static IEnumerable someNums() {
yield return 3;
yield return 5;
yield return 9;
}
static void Main() {
Days days = new Days();
foreach(var day in days()) {
Console.Write(day + " ");
}
}
public class Days : IEnumerable {
private string[] days = { "Sun", "Mon", "Tue",... };
public IEnerumator GetEnumerator() {
for(int i = 0; i < days.length; i++) {
yield return days[i];
}
}
}
public delegate int PerformCalc(int x, int y);
public delegate void Del(string message); public static void DelegateMethod(string message) { // ... } public static void DelegateMethod2(string message) { // ...} static void Main() { Del handler = new Del(DelegateMethod);
Del handler2 = DelegateMethod2; handler("hi"); MethodWithCallback(1,2, handler); } public void MethodWithCallback(int param1, int param2, Del callback) { callback("Params: " + param1 + param2); }
delegate void Del(int x);
Del d = delegate(int k) { // ... };
MethodWithCallback(1,2, delegate(string str) { // ... });
delegate int del(int i);
static void Main() {
del myDelegate = x => x + x;
int j = myDelegate(5); // j?
}
(x,y) => x == y
(int x, string s) => s.Length > x
() => SomeMethod()
n => { string s = n + " World"; Console.WriteLine(s); };
int outerVariable = 3;
i => { return outerVariable + 5 };
static void Main() {
int[] primeNums = new int[] { 2, 3, 5, 7,... };
IEnumerable<int> primNumsGT10 =
from primeNum in primeNums
where primeNum > 10
select primeNum;
foreach(int p in primNumsGT10) {
Console.WriteLine(p);
}
// 11, 13, 17, ...
}
var query = from ....... ; // query variable query
// query gets evaluated here, d hols result elements
foreach(var d in query) {
// ...
}
Thank you for your attention!