Tuesday, December 11, 2007

Generic Decorator Pattern


using System;
using 
System.Collections.Generic;

//Decorator pattern is very powerful pattern which can be used for supplementing the inheritance relationship with 
//composition based relationship. The basic idea is Attaching additional responsibilities to an object dynamically. 
//Decorators provide a flexible alternative to subclassing for extending functionality and it can be implemented 
//on several different ways. 
//This sample program presented here show that it can be done through Generic and Interface. 

//The classes and/or objects participating in this pattern are:
//1. Generic Abstract Decorator
//2. Generic Concrete Decorator
//3. Abstract Component
//4. Concrete Component


namespace Generic.DecoratorPattern
{
    
    // Generic Abstract Decorator 
    
abstract class Decorator<T> : ComputerItem  where T : ComputerItem, new() 
    {
        
protected readonly T computerItem = new T();

        
// Constructor 
        
public Decorator(T computerItem)
        {
            
this.computerItem computerItem;
        
}

        
public override void DisplayReport()
        {
            computerItem.DisplayReport()
;
        
}

    }

    
// Generic Concrete Decorator 
    
class Saleable<T> :  Decorator<T> where T : ComputerItem, new()
    {
        
protected List<string> Buyers = new List<string>();

        
// Constructor 
        
public Saleable(T computerItem) : base(computerItem)
        {
        }

        
public void SaleItem(string name)
        {
            Buyers.Add(name)
;
            
computerItem.NumStocks--;
        
}

        
public void ReturnItem(string name)
        {
            Buyers.Remove(name)
;
            
computerItem.NumStocks++;
        
}

        
public override void DisplayReport()
        {
            
base.DisplayReport();

            foreach 
(string buyer in Buyers)
            {
                Console.WriteLine(
" Buyer: " + buyer);
            
}
        }
    }

    
interface IComputerItem
    {
        
void DisplayReport();
    
}

    
// Abstract Component 
    
abstract class ComputerItem : IComputerItem
    {
        
private string brand;

        public string 
Brand
        {
            
get return brand}
            
set { brand = value; }
        }

        
private string type;

        public string 
Type
        {
            
get return type}
            
set { type = value; }
        }

        
private int numStocks;

        public int 
NumStocks
        {
            
get return numStocks}
            
set { numStocks = value; }
        }

        
public abstract void DisplayReport();
    
}

    
// Concrete Component 
    
class Desktop : ComputerItem
    {
        
public Desktop()
        { }

        
public Desktop(string brand, string type, int numStocks)
        {
            
this.Brand brand;
            this
.Type type;
            this
.NumStocks numStocks;
        
}

        
public override void DisplayReport()
        {
            Console.WriteLine(
"\n----- Desktop ----- ");
            
Console.WriteLine(" Brand: {0}", Brand);
            
Console.WriteLine(" Type: {0}", Type);
            
Console.WriteLine(" # Stocks: {0}", NumStocks);
        
}
    }

    
// Concrete Component 
    
class Laptop : ComputerItem
    {
        
public Laptop()
        { }

        
public Laptop(string brand, string type, int numStocks)
        {
            
this.Brand brand;
            this
.Type type;
            this
.NumStocks numStocks;
        
}

        
public override void DisplayReport()
        {
            Console.WriteLine(
"\n----- Laptop ----- ");
            
Console.WriteLine(" Brand: {0}", Brand);
            
Console.WriteLine(" Type: {0}", Type);
            
Console.WriteLine(" # Stocks: {0}", NumStocks);
        
}
    }

    
class Program
    {
        
static void Main()
        {
            
// Create desktop 
            
Desktop desktop = new Desktop("HPP""XM600"32);
            
desktop.DisplayReport();

            
// Create laptop 
            
Laptop laptop = new Laptop("ASUSS""F3MAP0"9);
            
laptop.DisplayReport();

            
Console.WriteLine("\nMaking laptop saleable:");

            
Saleable<Laptop> saleLaptop = new Saleable<Laptop>(laptop);
            
saleLaptop.SaleItem("Customer #1");
            
saleLaptop.SaleItem("Customer #2");
            
saleLaptop.SaleItem("Customer #3");

            
saleLaptop.DisplayReport();

            
Console.Read();
        
}
    }
}

Tuesday, December 4, 2007

Design Event Using Generic Delegate And Generic Interface

//When you design an event you have to do 3 steps

//1. Declare a private member for the event
//private event EventHandler<MyEventType> _EventName;

//2. Declare public accessors for adding and removing handlers
//public event EventHandler<MyEventType> EventName
//{
//      add    { _EventName += value; }
//      remove { _EventName -= value; }
//}

//3. Provide a virtual OnEventName method
//virtual void OnEventName(MyEventType e)
//{
//      if(_EventName != null)
//      {
//            try
//            {
//                  _EventName(this,e);
//            }
//            catch(Exception e)
//            {
//                  //your code here
//            }
//      }
//}

using System;

namespace 
Lab
{
    
    
interface IEventNum
    {
        
int EventNum
        {
            
get; set;
        
}
    }

    
//Define an interface that can perform operations on a generic type parameter
    
interface ITEventNum<T>
    {
        T EventNum
        {
            
get;
            set;
        
}
    }

    
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
    
public class MyEventArgs1 : EventArgs, IEventNum
    {
        
public int _EventNum;

        public int 
EventNum
        {
            
get return  _EventNum}
            
set { _EventNum = value; }
        }

    }

    
//Create a delegate signature for the event. 
    
public delegate void MyEventHandler(object source, MyEventArgs1 arg);

    class 
MyEvent1
    {
        
private static int count 0;

        
// Declare an event of delegate type MyEventHandler of MyEventArgs1.
        // Implicit Registration
        
public event MyEventHandler SomeEvent;

        
//Create a method on the type that will raise the event.
        
public virtual void OnSomeEvent()
        {
            MyEventArgs1 arg 
= new MyEventArgs1();

            
// Copy to a temporary variable to be thread-safe.
            
MyEventHandler temp SomeEvent;
            if 
(SomeEvent != null)
            {
                
try
                
{
                    arg.EventNum 
count++;
                    
temp(this, arg);
                
}
                
catch (Exception e)
                {
                    Console.WriteLine(e.Message)
;
                
}
            }
        }
    }

    
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
    
public class MyEventArgs2 : EventArgs, IEventNum
    {
        
public int _EventNum;

        public int 
EventNum
        {
            
get return _EventNum}
            
set { _EventNum = value; }
        }
    }

    
class MyEvent2
    {
        
private static int count 0;

        
//Generic EventHandler<T> delegate to declare an event bound to the specific EventArgs derivative 
        //Implicit Registration
        
public event EventHandler<MyEventArgs2> SomeEvent;

        
//Create a method on the type that will raise the event.
        
public virtual void OnSomeEvent()
        {
            MyEventArgs2 arg 
= new MyEventArgs2();

            
// Copy to a temporary variable to be thread-safe.
            
EventHandler<MyEventArgs2> temp SomeEvent;
            if 
(temp != null)
            {
                
try
                
{
                    arg.EventNum 
count++;
                    
temp(this, arg);
                
}
                
catch (Exception e)
                {
                    Console.WriteLine(e.Message)
;
                
}
            }
        }
    }


    
//Create a type that inherits from EventArgs if the event is going to propagate event specific data.
    //When you implement a generic interface, the supporting type specifies the placeholder type
    
public class MyEventArgs3<T> : EventArgs, ITEventNum<T>
    {
        
public T _EventNum;
        
        public T 
EventNum
        {
            
get return  _EventNum}
            
set { _EventNum = value; }
        }

    }

    
    
class MyEvent3
    {
        
private static int count 0;

        
// Declare an event of delegate type EventHandler of MyEventArgs3.
        // Implicit Registration
        // public event EventHandler<MyEventArgs3<int>> SomeEvent;

        // Explicit registration
        
private EventHandler<MyEventArgs3<int>> _SomeEvent;
        public event 
EventHandler<MyEventArgs3<int>> SomeEvent
        {
            
add    { _SomeEvent += value; }
            
remove { _SomeEvent -= value; }
        }
        
        
//Create a method on the type that will raise the event.
        
public virtual void OnSomeEvent()
        {
            MyEventArgs3<
int> arg = new MyEventArgs3<int>();

            
// Copy to a temporary variable to be thread-safe.
            
EventHandler<MyEventArgs3<int>> temp _SomeEvent;
            if 
(temp != null)
            {
                
try
                
{
                    arg.EventNum 
count++;
                    
temp(this, arg);   
                
}
                
catch(Exception e)
                {
                    Console.WriteLine(e.Message)
;
                
}
                
            }
        }
    }

    
class EventDemo1 : MyEvent1
    {
        
public void TestMain()
        {
            Test1 test1 
= new Test1();

            
MyEvent1 evt;

            
evt = new EventDemo1();
            
MyEventArgs1 arg = new MyEventArgs1();

            
// Add handler() to the event list.
            
evt.SomeEvent += new MyEventHandler(test1.handler);

            
// Fire the event.
            
evt.OnSomeEvent();
            
evt.OnSomeEvent();

        
}

        
public override void OnSomeEvent()
        {
            
base.OnSomeEvent();
            
Console.WriteLine("Override");
        
}
    }

    
class EventDemo2 : MyEvent2
    {
        
public void TestMain()
        {
            Test2 test2 
= new Test2();
            
MyEvent2 evt;

            
evt = new EventDemo2();
            
MyEventArgs2 arg = new MyEventArgs2();

            
// Add Handler() to the event list.
            
evt.SomeEvent += new EventHandler<MyEventArgs2>(test2.Handler);

            
// Fire the event.
            
evt.OnSomeEvent();
            
evt.OnSomeEvent();

        
}

        
public override void OnSomeEvent()
        {
            
base.OnSomeEvent();
            
Console.WriteLine("Override");
        
}

    }


    
class EventDemo3 : MyEvent3
    {
        
public void TestMain()
        {
            Test3 test3 
= new Test3();
            
            
MyEvent3 evt;
            
            
evt = new EventDemo3();
            
MyEventArgs3<int> arg = new MyEventArgs3<int>();

            
// Add Handler() to the event list.
            
evt.SomeEvent += new EventHandler<MyEventArgs3<int>>(test3.Handler);
           
            
// Fire the event.
            
evt.OnSomeEvent();
            
evt.OnSomeEvent();

        
}

        
public override void OnSomeEvent()
        {
            
base.OnSomeEvent();
            
Console.WriteLine("Override");
        
}

    }


    
class Test1
    {
        
public void handler(object source, MyEventArgs1 arg)
        {
            
if (source == nullthrow new ArgumentNullException("source");
            
Result.Write(source, arg);
        
}
    }

    
class Test2
    {
        
public void Handler(object source, MyEventArgs2 arg)
        {
            
if (source == nullthrow new ArgumentNullException("source");
            
Result.Write(source, arg);
        
}
    }

    
class Test3
    {
        
public void Handler(object source, MyEventArgs3<int> arg)
        {
            
if (source == nullthrow new ArgumentNullException("source");
            
Result.Write(source, arg);
        
}

    }

    
class Result
    {
        
public static void Write(object source, IEventNum arg)
        {
            Console.WriteLine(
"Event {0} received by an Test object.", arg.EventNum);
            
Console.WriteLine("Source is " + source);
        
}

        
public static void Write(object source, ITEventNum<int> arg)
        {
            Console.WriteLine(
"Event {0} received by an Test object.", arg.EventNum);
            
Console.WriteLine("Source is " + source);
        
}
    }
   
    
class program
    {
        
public static void Main()
        {
            EventDemo1 evt1 
= new EventDemo1();
            
evt1.TestMain();

            
EventDemo2 evt2 = new EventDemo2();
            
evt2.TestMain();

            
EventDemo3 evt3 = new EventDemo3();
            
evt3.TestMain();

            
Console.Read();
        
}
    }
}

Thursday, November 29, 2007

Enumeration

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


class 
Program
{
    
//Flag enums are designed to support bitwise operations on the enum values
    
[Flags]
    
public enum LogLevels : uint
    
{
        
//Increment by powers of 2.
        //If you don't assign explicit values VS increments by 1. 
        
None        0,
        NotifyTrace 
1,
        NotifyDebug 
2,
        NotifyInfo  
4,
        NotifyWarn  
8,
        NotifyError 
16,
        NotifyAll   
=   NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError,
        
//The rationale for avoiding zero in a flag enumeration for an actual flag 
        //is that you can't OR it in with other flags as expected.
        
NotifyNone  ~(NotifyTrace | NotifyDebug | NotifyInfo | NotifyWarn | NotifyError),
    }

    LogLevels options 
LogLevels.NotifyDebug | LogLevels.NotifyWarn | LogLevels.NotifyError;

    static void 
Main(string[] args)
    {
        Program p 
= new Program();
        
p.Test1();
        
p.Test2();
        
LogLevels v (LogLevels) 2;
        
p.Test3(v);

        
// Interesting, Compiler Catch Invalid Argument
        
p.Test4(2);
        
// Surprise, Correct
        
p.Test4(0);

        
Console.ReadLine();
    
}

    
public void Test1()
    {
        Type 
value = typeof(LogLevels);
        foreach 
(string in Enum.GetNames(value))
        {
            Console.WriteLine(
"{0,-11} = {1}", s, Enum.Format(value, Enum.Parse(value, s), "d"));
        
}
    }

    
public void Test2()
    {
        Console.WriteLine(IsLevelsSet(LogLevels.NotifyNone))
;
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyTrace));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyDebug));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyInfo));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyWarn));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyError));
        
Console.WriteLine(IsLevelsSet(LogLevels.NotifyAll));
    
}

   
    
public void Test3(LogLevels value)
    {
        
switch (value)
        {
            
case LogLevels.NotifyTrace:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyDebug:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyInfo:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyWarn:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyError:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyAll:
                Console.WriteLine(
value.ToString());
                break;
            case 
LogLevels.NotifyNone:
                Console.WriteLine(
value.ToString());
                break;
            default
:
                
break;
        
}
    }

    
public void Test4(LogLevels value)
    {
        //in C# the literal constant 0 implicitly converts to any enum type             
        
if (value == LogLevels.None)
            Console.WriteLine(
value.ToString());
    
}

    
private bool IsLevelsSet(LogLevels values)
    {
        
return (options & values) == values;
    
}

}

Friday, November 23, 2007

Create Objects From Their Names

//you can use the Activator.Create instance to do so.

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

namespace 
Labs1
{
    
class Program
    {
        
static void Main(string[] args)
        {

            
string TypeName "DerivedTest";
            string 
NmSpace  "Labs2";

            
//gets the System.Type of the control name (must be fully qualified)
            
Type t Type.GetType(NmSpace + "." + TypeName);

            
//Uses the 'Activator' to create a new instance of the specified type
            
Labs2.Test obj1 (Labs2.Test)Activator.CreateInstance(t);

            
//Now, "DerivedTest" is an instance of that type
            
obj1.Method();


            
NmSpace  "Labs3";

            
Type.GetType(NmSpace + "." + TypeName);

            
Labs3.ITest obj2 (Labs3.ITest) Activator.CreateInstance(t);
            
obj2.Method();

            
Console.ReadLine();

        
}
    }

}

namespace Labs2
{
    
public abstract class Test
    {
        
public virtual void Method()
        {
            Console.WriteLine(
"BasedTest2");
        
}
    }

    
public class DerivedTest : Test
    {
        
public override void Method()
        {
            Console.WriteLine(
"DerivedTest2");
        
}
    }
}

namespace Labs3
{
    
interface ITest
    {
        
void Method();
    
}
    
    
public class Test : ITest
    {
        
public virtual void Method()
        {
            Console.WriteLine(
"BasedTest3");
        
}
    }
   
    
public class DerivedTest : Test
    {
        
public override void Method()
        {
            Console.WriteLine(
"DerivedTest3");
        
}
    }
}

Wednesday, November 21, 2007

What is the use of 'virtual new' keyword ?

using System;
using 
System.Text;

namespace 
Polymorphism
{
    
class Program
    {
        
static void Main(string[] args)
        {
            Derived1 dv1 
= new Derived1();
            
//Do you know which Class Method is called here ?
            
dv1.Method(dv1);

            
Derived2 dv2 = new Derived2();
            
//Do you know which Class Method is called here ?
            
dv2.Method(dv2);

            
DerivedDerived3 dv3 = new DerivedDerived3();
            
//Do you know which Class Method is called here ?
            
dv3.Method(dv3);

            
DerivedDerived4 dv4 = new DerivedDerived4();
            
//Do you know which Class Method is called here ?
            
dv4.Method(dv4);

            
Console.ReadLine();
        
}
    }

    
class Based1
    {
           
public string Display()
           {
               
return "based";
           
}
     }
     
    
class Derived1:Based1
    {
        
//What is the use of new keyword ?
           
public new string Display() 
           {
               
return "derived";
           
}
    
        
public void Method(Based1 t)
        {
              Console.WriteLine(t.Display())
;
        
}
    }

    
class Based2
    {
        
//What is the use of virtual keyword ?
        
public virtual string Display()
           {
               
return "based";
           
}
     }

    
class Derived2 : Based2
    {
        
//What is the use of override keyword ?
        
public override string Display()
        {
            
return "derived";
        
}

        
public void Method(Based2 t)
        {
            Console.WriteLine(t.Display())
;
        
}

    }

    
class Based3
    {
        
public string Display()
        {
            
return "based";
        
}
    }
    
    
class Derived3 : Based3
    {
        
//What is the use of 'virtual new' keyword ?
        //Methods of a derived class can both be virtual and at the same time hide the derived method. 
        //In order to declare such a method, both keywords virtual and new have to be used in the method declaration
        
public virtual new string Display()
        {
            
return "derived";
        
}
    }

    
class DerivedDerived3 : Derived3
    {
        
public override string Display()
        {
            
return "derivedderived";
        
}

        
public void Method(Derived3 t)
        {
            Console.WriteLine(t.Display())
;
        
}
    }

    
class DerivedDerived4 : Derived3
    {
        
public new string Display()
        {
            
return "derivedderived";
        
}

        
public void Method(Derived3 t)
        {
            Console.WriteLine(t.Display())
;
        
}
    }

}

 

Friday, November 16, 2007

C# Syntax Cheat Sheet


x       Indicates x is supplied by the programmer or to be used verbatim
x?      Indicates x may occur zero-or-one times
x*      Indicates x may occur zero-or-more times, separated by commas
x+      Indicates x may occur one-or-more times,  separated by commas
[...]   Indicates a logical grouping of code elements, when not implicitly grouped using the verbatim 
terms {}, (), and []
[x|y]   Indicates only one of a choice of code elements may occur


 


Expression Statements


[variable = ]? expression;


 


Variable declaration        


type [variable [ = expression ]?]+ ;


 


Checked/unchecked operator


checked (expression)


unchecked (expression)


 


Checked/unchecked statement


Checked  statement-or-statement-block


Unchecked statement-or-statement-block


 


new operator 


class-var = new class-name();


 


Constant declaration       


const type [variable = constant-expression]+ ;


 


if-else statement


if (Boolean-expression)


statement-or-statement-block [


else


statement-or-statement-block ]?


 


switch statement


switch (expression) {


[ case constant-expression : statement* ]*


[ default : statement* ]? }


 


while loops


while (Boolean-expression) statement-or-statement-block


 


do-while loops


do


statement-or-statement-block while (Boolean-expression);


 


 


for loops


for (statement?; Boolean-expression?; statement?)


statement-or-statement-block


 


foreach loops


foreach ( type-value in IEnumerable) statement-or-statement-block


or


foreach(type var-name in collection) statement


 


goto statement :


goto statement-label;


goto case-constant


 


return statement


return expression?;


 


throw statement     


throw exception-expression?;


or


throw exceptOb;


The exceptOb must be an object of an exception class derived from Exception.


 


lock statement


lock (expression) statement-or-statement-block


 


using statement


using (declaration-expression) statement-or-statement-block


 


 


Namespace declaration syntax:


namespace name+ {


   using-statement*


   [namespace-declaration | type-declaration]*


}


 


Class


attributes? unsafe? access-modifier?


new?


[ abstract | sealed ]?   


class class-name [: base-class | : interface+ |: base-class, interface+ ] ?


{ class-members }


 


 


interface declaration:


interface name {


  ret-type method-name1(param-list);


  ret-type method-name2(param-list);


  // ...


  ret-type method-nameN(param-list);


}


 


interface property


type name {


  get;


  set;


}


 


interface indexer


element-type this[int index] {


  get;


  set;


}


 


Fields


attributes? unsafe? access-modifier? new?


static?


[readonly | volatile]?


type [ field-name [ = expression]? ]+ ;


 


 


Struct


attributes? unsafe? access-modifier?


new?


struct struct-name [: interface+]?


{ struct-members }


 


  


Constant 


attributes? access-modifier?


new?


const type [ constant-name = constant-expression ]+;


 


 


Properties


attributes? unsafe? access-modifier?


[


[[sealed | abstract]? override] |    


new? [virtual | abstract | static]?


]?


type property-name { [    


attributes? get statement-block |    //     read-only


attributes? set statement-block |    //      write-only


attributes? get statement-block      //     read-write


attributes? set statement-block ] 


}


 


Indexers


attributes? unsafe? access-modifier?             


[                 


  [[sealed | abstract]? override] |           


  new? [virtual | abstract]?            


]?               


type this [ attributes? [type arg]+ ]      {       


attributes? get statement-block |  //       read-only


attributes? set statement-block |  //       write-only


attributes? get statement-block    //       read-write


attributes? set statement-block             


}


 


Method


attributes? unsafe? access-modifier? [


[[sealed | abstract]? override] |


new? [ virtual | abstract ]?


[ void | type ] method-name (parameter-list) statement-block


 


 


Parameter list


[attributes? [ref | out]? type arg ]* [ params attributes? type[ ] arg ]?


 


 


Instance Constructors


attributes? unsafe? access-modifier?


class-name (parameter-list) [ :[ base | this ] (argument-list) ]?


statement-block


 


calling base class contructor :


derived-constructor(parameter-list) : base(arg-list) {


   // body of constructor


}


 


Static Constructors


attributes? unsafe? extern? static class-name ( ) statement-block


 


Destructors and Finalizers


attributes? unsafe? ~class-name () statement-block


 


Interfaces


attributes? unsafe? access-modifier? new?


interface interface-name [ : base-interface+ ]? { interface-members }


 


 


Arrays


type[*]+ array-name = new type [ dimension+ ][*]*;


 


[*] is the set: [] [,] [,,]


 


Initializing a one-dimensional array


type[ ] array-name = { val1, val2, val3, ..., valN };


 


 


Enums


attributes? access-modifier?


new?


enum enum-name [ : integer type ]?


{ [attributes? enum-member-name [ = value ]? ]* }


 


 


Delegates


attributes? unsafe? access-modifier? new?


Delegate [ void | type ]  delegate-name (parameter-list);


 


Delegate-invocations


primary-expression-of-a-delegate-type (expression);


 


Delegate-creation


new   delegate-type  (expression);


The argument of a delegate creation expression must be a method group or a value of a delegate-type.


 


Events


access-modifier? event event-delegate object-name;


 


Event Accessors


 attributes? unsafe? access-modifier?


[


  [[sealed | abstract]? override] | new? [virtual | static]?


]?


event delegate type event-property-accessor-name


{


attributes? add statement-block


attributes? remove statement-block


}


 


 


try statement


try statement-block


[catch (exception type value?)? statement-block]+  | finally statement-block |


[catch (exception type value?)? statement-block]+     finally statement-block


 


 


Attributes


[[target:]? attribute-name


(


positional-param+ |


[named-param = expression]+ |


positional-param+, [named-param = expression]+)?]


 


overloading a unary operator


public static ret-type operator op(param-type operand)


{


   // operations


}


 


overloading a binary operator


public static ret-type operator op(param-type1 operand1, param-type1 operand2)


{


   // operations


}


 


 


NET-compatible event handlers 


void handler(object source, EventArgs arg) {


  // ...


}


 


Delegates


public delegate  void  Del<T>(T item);


public void Notify(int i) { }


 


Del<int> d1 = new Del<int>(Notify);


 


In C# 2.0


 


Del<int> d2 = Notify;


 


 


generic class


class class-name<type-param-list> { // ...


 


declaring a reference to a generic class


class-name<type-arg-list> var-name =


new class-name<type-arg-list>(cons-arg-list);


 


generic method


ret-type meth-name<type-parameter-list>(param-list) { // ...


 


generic delegate


delegate ret-type delegate-name<type-parameter-list>(arg-list);


 


 


References :
C# Essentials, 2nd Edition, Ben Albahari Peter Drayton Brad Merrill
Microsoft Visual C# .NET,
http://msdn.microsoft.com/vcsharp
ECMA C# Standard, http://www.ecma.ch/ecma1/stand/ecma-334.htm


 

Indonesia To Blog -Top Site
SEO - search engine submission and optimisation