Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Apress
committed
Oct 11, 2016
0 parents
commit ca4ba71
Showing
321 changed files
with
12,626 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
using System; | ||
|
||
public delegate int ProcStrategy( int x ); | ||
|
||
public class Processor | ||
{ | ||
private ProcStrategy strategy; | ||
public ProcStrategy Strategy { | ||
set { | ||
strategy = value; | ||
} | ||
} | ||
|
||
public int[] Process( int[] array ) { | ||
int[] result = new int[ array.Length ]; | ||
for( int i = 0; i < array.Length; ++i ) { | ||
result[i] = strategy( array[i] ); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
private static int MultiplyBy2( int x ) { | ||
return x*2; | ||
} | ||
|
||
private static int MultiplyBy4( int x ) { | ||
return x*4; | ||
} | ||
|
||
private static void PrintArray( int[] array ) { | ||
for( int i = 0; i < array.Length; ++i ) { | ||
Console.Write( "{0}", array[i] ); | ||
if( i != array.Length-1 ) { | ||
Console.Write( ", " ); | ||
} | ||
} | ||
Console.Write( "\n" ); | ||
} | ||
|
||
static void Main() { | ||
// Create an array of integers. | ||
int[] integers = new int[] { | ||
1, 2, 3, 4 | ||
}; | ||
|
||
Processor proc = new Processor(); | ||
proc.Strategy = new ProcStrategy( EntryPoint.MultiplyBy2 ); | ||
PrintArray( proc.Process(integers) ); | ||
|
||
proc.Strategy = new ProcStrategy( EntryPoint.MultiplyBy4 ); | ||
PrintArray( proc.Process(integers) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System; | ||
|
||
public delegate int ProcStrategy( int x ); | ||
|
||
public class Processor | ||
{ | ||
private ProcStrategy strategy; | ||
public ProcStrategy Strategy { | ||
set { | ||
strategy = value; | ||
} | ||
} | ||
|
||
public int[] Process( int[] array ) { | ||
int[] result = new int[ array.Length ]; | ||
for( int i = 0; i < array.Length; ++i ) { | ||
result[i] = strategy( array[i] ); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
private static void PrintArray( int[] array ) { | ||
for( int i = 0; i < array.Length; ++i ) { | ||
Console.Write( "{0}", array[i] ); | ||
if( i != array.Length-1 ) { | ||
Console.Write( ", " ); | ||
} | ||
} | ||
Console.Write( "\n" ); | ||
} | ||
|
||
static void Main() { | ||
// Create an array of integers. | ||
int[] integers = new int[] { | ||
1, 2, 3, 4 | ||
}; | ||
|
||
Processor proc = new Processor(); | ||
proc.Strategy = delegate(int x) { | ||
return x*2; | ||
}; | ||
PrintArray( proc.Process(integers) ); | ||
|
||
proc.Strategy = delegate(int x) { | ||
return x*4; | ||
}; | ||
PrintArray( proc.Process(integers) ); | ||
|
||
proc.Strategy = delegate { | ||
return 0; | ||
}; | ||
PrintArray( proc.Process(integers) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
using System; | ||
|
||
public delegate int ProcStrategy( int x ); | ||
|
||
public class Processor | ||
{ | ||
private ProcStrategy strategy; | ||
public ProcStrategy Strategy { | ||
set { strategy = value; } | ||
} | ||
|
||
public int[] Process( int[] array ) { | ||
int[] result = new int[ array.Length ]; | ||
for( int i = 0; i < array.Length; ++i ) { | ||
result[i] = strategy( array[i] ); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
public class Factor | ||
{ | ||
public Factor( int fact ) { | ||
this.fact = fact; | ||
} | ||
|
||
private int fact; | ||
|
||
public ProcStrategy Multiplier { | ||
get { | ||
// This is an anonymous method. | ||
return delegate(int x) { | ||
return x*fact; | ||
}; | ||
} | ||
} | ||
|
||
public ProcStrategy Adder { | ||
get { | ||
// This is an anonymous method. | ||
return delegate(int x) { | ||
return x+fact; | ||
}; | ||
} | ||
} | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
private static void PrintArray( int[] array ) { | ||
for( int i = 0; i < array.Length; ++i ) { | ||
Console.Write( "{0}", array[i] ); | ||
if( i != array.Length-1 ) { | ||
Console.Write( ", " ); | ||
} | ||
} | ||
Console.Write( "\n" ); | ||
} | ||
|
||
static void Main() { | ||
// Create an array of integers. | ||
int[] integers = new int[] { | ||
1, 2, 3, 4 | ||
}; | ||
|
||
Factor factor = new Factor( 2 ); | ||
Processor proc = new Processor(); | ||
proc.Strategy = factor.Multiplier; | ||
PrintArray( proc.Process(integers) ); | ||
|
||
proc.Strategy = factor.Adder; | ||
factor = null; | ||
PrintArray( proc.Process(integers) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using System; | ||
|
||
public delegate double ProcessResults( double x, | ||
double y ); | ||
|
||
public class Processor | ||
{ | ||
public Processor( double factor ) { | ||
this.factor = factor; | ||
} | ||
|
||
public double Compute( double x, double y ) { | ||
double result = (x+y)*factor; | ||
Console.WriteLine( "InstanceResults: {0}", result ); | ||
return result; | ||
} | ||
|
||
public static double StaticCompute( double x, | ||
double y ) { | ||
double result = (x+y)*0.5; | ||
Console.WriteLine( "StaticResult: {0}", result ); | ||
return result; | ||
} | ||
|
||
private double factor; | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
static void Main() { | ||
Processor proc1 = new Processor( 0.75 ); | ||
Processor proc2 = new Processor( 0.83 ); | ||
|
||
ProcessResults delegate1 = new ProcessResults( proc1.Compute ); | ||
ProcessResults delegate2 = new ProcessResults( proc2.Compute ); | ||
ProcessResults delegate3 = new ProcessResults( Processor.StaticCompute ); | ||
|
||
double combined = delegate1( 4, 5 ) + | ||
delegate2( 6, 2 ) + | ||
delegate3( 5, 2 ); | ||
|
||
Console.WriteLine( "Output: {0}", combined ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using System; | ||
|
||
public delegate int Operation( int x, int y ); | ||
|
||
public class Bind2nd | ||
{ | ||
public delegate int BoundDelegate( int x ); | ||
|
||
public Bind2nd( Operation del, int arg2 ) { | ||
this.del = del; | ||
this.arg2 = arg2; | ||
} | ||
|
||
public BoundDelegate Binder { | ||
get { | ||
return delegate( int arg1 ) { | ||
return del( arg1, arg2 ); | ||
}; | ||
} | ||
} | ||
|
||
private Operation del; | ||
private int arg2; | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
static int Add( int x, int y ) { | ||
return x + y; | ||
} | ||
|
||
static void Main() { | ||
Bind2nd binder = new Bind2nd( | ||
new Operation(EntryPoint.Add), | ||
4 ); | ||
|
||
Console.WriteLine( binder.Binder(2) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using System; | ||
|
||
public delegate int Operation( int x, int y ); | ||
|
||
// WILL NOT COMPILE !!! | ||
public class Bind2nd< DelegateType > | ||
{ | ||
public delegate int BoundDelegate( int x ); | ||
|
||
public Bind2nd( DelegateType del, int arg2 ) { | ||
this.del = del; | ||
this.arg2 = arg2; | ||
} | ||
|
||
public BoundDelegate Binder { | ||
get { | ||
return delegate( int arg1 ) { | ||
return del( arg1, arg2 ); // OUCH! | ||
}; | ||
} | ||
} | ||
|
||
private DelegateType del; | ||
private int arg2; | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
static int Add( int x, int y ) { | ||
return x + y; | ||
} | ||
|
||
static void Main() { | ||
Bind2nd binder = new Bind2nd( | ||
new Operation(EntryPoint.Add), | ||
4 ); | ||
|
||
Console.WriteLine( binder.Binder(2) ); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
using System; | ||
|
||
public delegate int Operation( int x, int y ); | ||
|
||
// STILL WILL NOT COMPILE !!! | ||
public class Bind2nd< DelegateType > | ||
where DelegateType : Delegate | ||
{ | ||
public delegate int BoundDelegate( int x ); | ||
|
||
public Bind2nd( DelegateType del, int arg2 ) { | ||
this.del = del; | ||
this.arg2 = arg2; | ||
} | ||
|
||
public BoundDelegate Binder { | ||
get { | ||
return delegate( int arg1 ) { | ||
return del( arg1, arg2 ); // OUCH! | ||
}; | ||
} | ||
} | ||
|
||
private DelegateType del; | ||
private int arg2; | ||
} | ||
|
||
public class EntryPoint | ||
{ | ||
static int Add( int x, int y ) { | ||
return x + y; | ||
} | ||
|
||
static void Main() { | ||
Bind2nd binder = new Bind2nd( | ||
new Operation(EntryPoint.Add), | ||
4 ); | ||
|
||
Console.WriteLine( binder.Binder(2) ); | ||
} | ||
} |
Oops, something went wrong.