using System; public class Starter{ public static void Main(){ MethodA<int>(20); } public static void MethodA<T>(T param) { Console.WriteLine(param.GetType().ToString()); } }
using System; public class Starter{ public static void Main(){ MethodA<int>(20); } public static void MethodA<T>(T param) { Console.WriteLine(param.GetType().ToString()); } }
using System;
class ArrayUtils {
public static bool copyInsert
if(target.Length < src.Length+1) return false; for(int i=0, j=0; i < src.Length; i++, j++) { if(i == idx) { target[j] = e; j++; } target[j] = src[i]; } return true; } } class Test { public static void Main() { int[] nums = { 1, 2, 3 }; int[] nums2 = new int[4]; Console.Write("Contents of nums: "); foreach(int x in nums) Console.Write(x + " "); Console.WriteLine(); ArrayUtils.copyInsert(99, 2, nums, nums2); Console.Write("Contents of nums2: "); foreach(int x in nums2) Console.Write(x + " "); Console.WriteLine(); string[] strs = { "Generics", "are", "powerful."}; string[] strs2 = new string[4]; Console.Write("Contents of strs: "); foreach(string s in strs) Console.Write(s + " "); Console.WriteLine(); ArrayUtils.copyInsert("in C#", 1, strs, strs2); Console.Write("Contents of strs2: "); foreach(string s in strs2) Console.Write(s + " "); } } [/csharp]
using System; public class Starter { public static void Main() { MyClass.MethodA<int>(20); } } public class MyClass { public static void MethodA<T>(T param) { Console.WriteLine(param.GetType().ToString()); } }
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
public interface IPresentation {
string Title {
get;
}
string Content {
get;
}
}
public class Presentation : IPresentation {
private string title;
public string Title {
get {
return title;
}
}
private string content;
public string Content {
get {
return content;
}
}
public Presentation(string title, string content) {
this.title = title;
this.content = content;
}
}
public class ProcessPresentations
where TPresentation : IPresentation
where TPresentationManager : IPresentationManager
public static void Start(TPresentationManager dm) {
new Thread(new ProcessPresentations
}
protected ProcessPresentations(TPresentationManager dm) {
documentManager = dm;
}
private TPresentationManager documentManager;
protected void Run() {
while (true) {
if (documentManager.IsPresentationAvailable) {
TPresentation doc = documentManager.GetPresentation();
Console.WriteLine(“Processing document {0}”, doc.Title);
}
Thread.Sleep(20);
}
}
}
public interface IPresentationManager
void AddPresentation(T doc);
T GetPresentation();
bool IsPresentationAvailable {
get;
}
}
public class PresentationManager
private Queue
public void AddPresentation(T doc) {
lock (this) {
documentQueue.Enqueue(doc);
}
}
public T GetPresentation() {
T doc;
lock (this) {
doc = documentQueue.Dequeue();
}
return doc;
}
public bool IsPresentationAvailable {
get {
return (documentQueue.Count > 0) ? true : false;
}
}
}
class Program {
static void Main(string[] args) {
PresentationManager
ProcessPresentations
for (int i = 0; i < 1000; i++) { Presentation d1 = new Presentation("title" + i.ToString(), "content"); dm.AddPresentation(d1); Console.WriteLine("added document {0}", d1.Title); Thread.Sleep(10); } } } [/csharp]
using System; using System.Collections.Generic; using System.Text; class Program { static void Main(string[] args) { Console.WriteLine("Generic Interfaces"); BasicMath m = new BasicMath(); Console.WriteLine("1 + 1 = {0}", m.Add(1, 1)); Console.ReadLine(); } } public interface IBinaryOperations<T> { T Add(T arg1, T arg2); T Subtract(T arg1, T arg2); T Multiply(T arg1, T arg2); T Divide(T arg1, T arg2); } public class BasicMath : IBinaryOperations<int> { public int Add(int arg1, int arg2) { return arg1 + arg2; } public int Subtract(int arg1, int arg2) { return arg1 - arg2; } public int Multiply(int arg1, int arg2) { return arg1 * arg2; } public int Divide(int arg1, int arg2) { return arg1 / arg2; } }
using System;
public interface ISequence
T getNext();
void reset();
void setStart(T v);
}
class ByTwos
T start;
T val;
public delegate T IncByTwo(T v);
IncByTwo incr;
public ByTwos(IncByTwo incrMeth) {
start = default(T);
val = default(T);
incr = incrMeth;
}
public T getNext() {
val = incr(val);
return val;
}
public void reset() {
val = start;
}
public void setStart(T v) {
start = v;
val = start;
}
}
class GenIntfDemo {
static int intPlusTwo(int v) {
return v + 2;
}
static double doublePlusTwo(double v) {
return v + 2.0;
}
public static void Main() {
ByTwos
for(int i=0; i < 5; i++)
Console.Write(intBT.getNext() + " ");
Console.WriteLine();
ByTwos
dblBT.setStart(11.4);
for(int i=0; i < 5; i++) Console.Write(dblBT.getNext() + " "); } } [/csharp]
public delegate void ADelegate<T>(T tvalue); public class MyClass{ public void MethodA() { ADelegate<int> del=delegate(int var) { }; } } public class YClass<T>{ public delegate void BDelegate(T tValue); public void MethodA() { BDelegate del=delegate(T tValue) { }; } }