Delegate, Lambda expression, and event series 06. Use Action to implement observer mode and experience the difference between delegate and event. lambdaaction
In "two ways to implement the Observer Pattern", the Observer Pattern has been implemented through the interface, delegate, and event methods. This article uses Action to implement this mode and to experience the difference between delegation and events.
□Use Action to implement observer Mode
Let's take a football field as an example
Question 4 of the first round of the 2006 Baidu STAR Program Design Competition
N children are playing a scissors and stone cloth game with you. One of the N children is a referee, And the other children are divided into three groups (it is not ruled out that some groups do not have any members), but you do not know who is a referee, I do not know the group of children. Then, the children started to play th
number of rounds. Following is M lines, each line contains the integers in [0, N) separated by one symbol. The integers is the IDs of the children selected to play Rochambeau for this round. The symbol May is "=", ">" or "Output:There is a one line for each test case. If The judge can be found, print the ID of the judge, and the least number of rounds after which the judge can uniquely Determined. If The judge can not be found, or the outcomes of the M rounds of game is inconsistent, print the
to plug the offensive position as the illusion,2nd players concealed in the front row 3rd players side, false for the back plug, when a ball is higher or closer to the net,2nd players suddenly approached the run-off buckle two times the ball or lob, in order to achieve the purpose of a surprise attack. 4th players can also, the method and the position of the 2nd players and play the same, only in the left side of the field offensive. Plug it back in.A type of offensive in which volleyball is in
pointer (this in the Delphi term is "closure"), Java with the adaptation class to achieve, VC with WINDOWSAPI message system, and C # The delegate and event keywords are used directly to solve the problem. Let's take a look at an example that gives you the whole process of declaring, invoking, and handling events.
The first is the declaration of a reference, which defines the event signal that wakes a function
public delegate void Scorechangeeventhandler (int newscore, ref bool cancel);
Defi
In. before Net Framework 2.0, array representation can be represented by array and arraylist of collection classes. After 2.0 ,. NET introduces the generic concept list
2.0 the intention of introducing generic models is to reduce the performance consumption caused by packing and unpacking of types.
For example;
Private void compareto
{
....
}
The list
Today, we mainly discuss what kind of performance is higher when the type is determined. arraylist does not need to be considered. It must be
int count = 1000000; // Number of inserted itemsStatic void Main (string [] args){// Long commonInsertRunTime = CommonInsert ();// Console. WriteLine (string. Format ("the time used to insert {1} pieces of data in normal mode is {0} millisecond", commonInsertRunTime, count ));Long sqlBulkCopyInsertRunTime = SqlBulkCopyInsert ();Console. WriteLine (string. Format ("the time used to insert {1} pieces of data using SqlBulkCopy is {0} millisecond", sqlBulkCopyInsertRunTime, count ));Long TVPInsertR
;Using System. Diagnostics;Using System. Data;Using System. Data. SqlClient;Using com. DataAccess;Namespace ConsoleAppInsertTest{Class Program{Static string connectionString = SqlHelper. ConnectionStringLocalTransaction; // database connection stringStatic int count = 1000000; // Number of inserted itemsStatic void Main (string [] args){// Long commonInsertRunTime = CommonInsert ();// Console. WriteLine (string. Format ("the time used to insert {1} pieces of data in normal mode is {0} millisecon
= Sqlhelper.connectionstringlocaltransaction; Database connection stringstatic int count = 1000000; Number of bars insertedstatic void Main (string[] args){Long commoninsertruntime = Commoninsert ();Console.WriteLine (String. Format (the time spent in the normal way to insert {1} data is {0} milliseconds, Commoninsertruntime, count));Long sqlbulkcopyinsertruntime = Sqlbulkcopyinsert ();Console.WriteLine (String. Format ("The time used to insert {1} data using SqlBulkCopy is {0} milliseconds", S
comments.
Copy Code code as follows:
Using System;
Using System.Diagnostics;
Using System.Data;
Using System.Data.SqlClient;
Using COM. DataAccess;
Namespace Consoleappinserttest
{
Class Program
{
static string connectionString = Sqlhelper.connectionstringlocaltransaction; Database connection string
static int count = 1000000; Number of bars inserted
static void Main (string[] args)
{
Long commoninsertruntime = Commoninsert ();
Console.WriteLine (String. Format (the ti
delegate and event keywords to solve this problem. Let's take a look at the example below. We will give you the whole process of declaring, calling, and processing events.
// First, it refers to the declaration, which defines the event signal to wake up a function.Public Delegate void scorechangeeventhandler (INT newscore, ref bool cancel );
// Define an event-generating class> public class game{// Note that the event keyword is used herePublic event scorechangeeventhandler scorechange;Int
cancel);
Define a class that generates events
public class Game
{
Note that the event keyword is used here
public event Scorechangeeventhandler Scorechange;
int score;
Score Property
public int Score
{
get {
return score;
}
set {
If (score!= value)
{
bool Cancel = false;
Scorechange (value, ref cancel);
if (! Cancel)
Score = value;
}
}
}
Classes that handle events
public class referee
{
Public referee
Scorechangeeventhandler Scorechange;
int score;
Score Property
public int Score
{
get {
return score;
}
set {
If (score!= value)
{
bool Cancel = false;
Scorechange (value, ref cancel);
if (! Cancel)
Score = value;
}
}
}
Classes that handle events
public class referee
{
Public referee (Game Game)
{
The referee is responsible for adjusting the score changes in t
I talked with others about the performance of linq yesterday. I feel that the performance of linq is not good, but in fact it is still outstanding in terms of performance, except for the ability to write data to SQL, the following is a simple performance test comparison code. Here I will explain that the execution speed of the Code cannot be tested using datetime. I am still a little white, just starting with datetime, the results showed that the performance of the linq service was not good, but
I talked with others about the performance of LINQ yesterday. I feel that the performance of LINQ is not good, but in fact it is still outstanding in terms of performance, except for the ability to write data to SQL, the following is a simple performance test comparison code. Here I will explain that the execution speed of the Code cannot be tested using datetime. I am still a little white, just starting with datetime, the results showed that the performance of the LINQ service was not good, but
1. Use the stopwatch class (system. Diagnostics. stopwatch)
The stopwatch instance can measure the running time at one interval or the total running time at multiple intervals. In a typical stopwatch solution, call the start method, call the stop method, and use the elapsed attribute to check the running time.
The
In a project, it is often necessary to log records of execution performance (time spent) of some methods, which are implemented in two ways.StopWatchUsing the Stopwatch class to measure time is very simple. Like the stopwatch in real life, this class of objects can start, stop, zero (reset) operations on the counter, but it is much more accurate than the average stopwat
distance between place I and place j is defined as ceil (sqrt (xi-xj) ^ 2 + (yi-yj) ^ 2 )). (ceil means rounding the number up, e.g. ceil (4.1) = 5) Output For each case, output two numbers. The first is the minimum number of judges for question 1. The second is the minimum sum of walking time for question 2.If it's impossible to serve all the contestants, please output-1-1 instead. Sample Input
3 30 00 30 10123 20 00 30 10123 10 00 30 1012 16 3530 4037 5249 4952 6431 6252 3342 4152 4157 5862
Program{Static string connectionstring = sqlhelper. connectionstringlocaltransaction; // database connection stringStatic int COUNT = 1000000; // Number of inserted itemsStatic void main (string [] ARGs){// Long commoninsertruntime = commoninsert ();// Console. writeline (string. Format ("the time used to insert {1} pieces of data in normal mode is {0} millisecond", commoninsertruntime, count ));Long sqlbulkcopyinsertruntime = sqlbulkcopyinsert ();Console. writeline (string. Format ("the time u
method for obtaining objects. This method is used with constructor, factory, and service locator (such as JNDI )) compared with traditional methods, these methods provide better reusability, testability, and maintainability. The processing process of this method is a well-known dependency injection, which is very valuable to most applications.
In our program, many types depend on other types. For example, a stopwatch may depend on a timesource. If so
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.