Two Generic parameters

image_pdfimage_print

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

public interface IDocument {
string Title {
get;
}

string Content {
get;
}
}

public class Document : IDocument {
private string title;
public string Title {
get {
return title;
}
}

private string content;
public string Content {
get {
return content;
}
}

public Document(string title, string content) {
this.title = title;
this.content = content;
}
}

public class ProcessDocuments
where T : IDocument
where U : IDocumentManager {
public static void Start(U dm) {
new Thread(new ThreadStart(new ProcessDocuments(dm).Run)).Start();
}

protected ProcessDocuments(U dm) {
documentManager = dm;
}

private U documentManager;

protected void Run() {
while (true) {
if (documentManager.IsDocumentAvailable) {
T doc = documentManager.GetDocument();
Console.WriteLine(“Processing document {0}”, doc.Title);
}
Thread.Sleep(new Random().Next(20));
}
}
}

public interface IDocumentManager {
void AddDocument(T doc);
T GetDocument();
bool IsDocumentAvailable {
get;
}
}

public class DocumentManager : IDocumentManager {
private readonly Queue documentQueue = new Queue();

public void AddDocument(T doc) {
lock (this) {
documentQueue.Enqueue(doc);
}
}

public T GetDocument() {
T doc = default(T);
lock (this) {
doc = documentQueue.Dequeue();
}
return doc;
}

public bool IsDocumentAvailable {
get {
return (documentQueue.Count > 0) ? true : false;
}
}
}
class Program {

static void Main(string[] args) {
DocumentManager dm = new DocumentManager();
ProcessDocuments>.Start(dm);
for (int i = 0; i < 1000; i++) { Document doc = new Document("Doc " + i.ToString(), "content"); dm.AddDocument(doc); Console.WriteLine("added document {0}", doc.Title); Thread.Sleep(new Random().Next(20)); } } } [/csharp]

Serialize and Deserialize generic objects

image_pdfimage_print
   
 
using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;


public class Starter {
    public static void Main(string[] args) {
        BinaryFormatter binary = new BinaryFormatter();
        FileStream file = new FileStream("data.bin", FileMode.OpenOrCreate);

        MyClass<int> obj = new MyClass<int>(5);
        binary.Serialize(file, obj);
        MyClass<int> obj1 = (MyClass<int>)
        binary.Deserialize(file);
        Console.WriteLine(obj1.GetValue());
    }
}

[Serializable]
public class MyClass<T> {

    public MyClass(T init) {
        fielda = init;
    }

    public void GetObjectData(SerializationInfo info,StreamingContext ctx) {
        info.AddValue("fielda", fielda, typeof(T));
    }

    private MyClass(SerializationInfo info,StreamingContext ctx) {
        fielda = (T)info.GetValue("fielda", typeof(T));
    }

    public void SetValue(T data) {
        fielda = data;
    }

    public T GetValue() {
        return fielda;
    }

    private T fielda = default(T);
}

    


Demonstrate a generic method

image_pdfimage_print

using System;

class ArrayUtils {

public static bool copyInsert(T e, int idx, T[] src, T[] target) {

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]

Generic parameter interface

image_pdfimage_print

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(dm).Run).Start();
}

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 : IPresentationManager {
private Queue documentQueue = new 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 dm = new PresentationManager();

ProcessPresentations>.Start(dm);

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]