Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Apress committed Oct 11, 2016
0 parents commit ca4ba71
Show file tree
Hide file tree
Showing 321 changed files with 12,626 additions and 0 deletions.
56 changes: 56 additions & 0 deletions 10_delegates/anonymous_1.cs
@@ -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) );
}
}
57 changes: 57 additions & 0 deletions 10_delegates/anonymous_2.cs
@@ -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) );
}
}
75 changes: 75 additions & 0 deletions 10_delegates/anonymous_3.cs
@@ -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) );
}
}
44 changes: 44 additions & 0 deletions 10_delegates/basic_use_1.cs
@@ -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 );
}
}
39 changes: 39 additions & 0 deletions 10_delegates/binder_1.cs
@@ -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) );
}
}
40 changes: 40 additions & 0 deletions 10_delegates/binder_2.cs
@@ -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) );
}
}
41 changes: 41 additions & 0 deletions 10_delegates/binder_3.cs
@@ -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) );
}
}

0 comments on commit ca4ba71

Please sign in to comment.