标签:
namespace System
{
// Summary:
// Represents the method that will handle an event.
//
// Parameters:
// sender:
// The source of the event.
//
// e:
// An System.EventArgs that contains the event data.
//
// Type parameters:
// TEventArgs:
// The type of the event data generated by the event.
[Serializable]
public delegate void EventHandler<TEventArgs>(object sender, TEventArgs e); //注意这个是模板
}
? ? ?
namespace System
{
// Summary:
// System.EventArgs is the base class for classes containing event data.
[Serializable]
[ComVisible(true)]
public class EventArgs
{
// Summary:
// Represents an event with no event data.
public static readonly EventArgs Empty;
? ? ?
// Summary:
// Initializes a new instance of the System.EventArgs class.
public EventArgs();
}
}
? ? ?
/////////////////////////////////////////////////////////////////
public class IncrementerEventArgs : EventArgs // Custom class derived from EventArgs
{
public int IterationCount { get; set; } // Stores an integer
}
? ? ?
class Incrementer
{
public event EventHandler<IncrementerEventArgs> CountedADozen;
? ? ?
public void DoCount()
{
IncrementerEventArgs args = new IncrementerEventArgs();
for ( int i=1; i < 100; i++ )
if ( i % 12 == 0 && CountedADozen != null )
{
args.IterationCount = i;
CountedADozen( this, args );
}
}
}
? ? ?
class Dozens
{
public int DozensCount { get; private set; }
? ? ?
public Dozens( Incrementer incrementer )
{
DozensCount = 0;
incrementer.CountedADozen += IncrementDozensCount;
}
? ? ?
void IncrementDozensCount( object source, IncrementerEventArgs e )
{
Console.WriteLine( "Incremented at iteration: {0} in {1}",
e.IterationCount, source.ToString() );
DozensCount++;
}
}
? ? ?
class Program
{
static void Main()
{
Incrementer incrementer = new Incrementer();
Dozens dozensCounter = new Dozens( incrementer );
? ? ?
incrementer.DoCount();
Console.WriteLine( "Number of dozens = {0}",
dozensCounter.DozensCount );
}
}
标签:
原文地址:http://www.cnblogs.com/InspiringMind/p/5858241.html