using System; public delegate void DelegateClass(out int arg); public class Starter { public static void Main() { DelegateClass del = MethodA(); int var; del(out var); Console.WriteLine(var); } public static DelegateClass MethodA() { int local = 0; return delegate(out int arg) { arg = ++local; }; } }
Author: coder
two delegates and anonymous methods
using System; public delegate void ADelegate(int param); public delegate int BDelegate(int param1, int param2); public class Starter { public static void Main() { ADelegate del = delegate(int param) { param = 5; }; } public int MethodA() { BDelegate del = delegate(int param1, int param2) { return 0; }; return 0; } }
Anonymous methods can be assigned a signature, which is appended to the delegate keyword.
using System; using System.Threading; public delegate int DelegateClass(out int param); public class Starter { public static void Main() { int var; DelegateClass del = delegate(out int inner) { inner = 12; Console.WriteLine("Running anonymous method"); return inner; }; del(out var); Console.WriteLine("Var is {0}", var); } }
Anonymous methods can refer to local variables of the containing function and class members within the scope of the method definition
using System; public delegate void DelegateClass(out int arg); public class Starter { public static void Main() { DelegateClass del = MethodA(); int var; del(out var); Console.WriteLine(var); } public static DelegateClass MethodA() { int local = 0; return delegate(out int arg) { arg = ++local; }; } }
Define an anonymous method with the delegate keyword.
using System; using System.Threading; public delegate void DelegateClass(); public class Starter { public static void Main() { DelegateClass del = delegate { Console.WriteLine("Running anonymous method"); }; del(); } }
The continue statement
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_14.cs illustrates the use of
the continue statement
*/
public class Example4_14
{
public static void Main()
{
int total = 0;
for (int counter = 1; counter <= 10; counter++) { if (counter == 6) { System.Console.WriteLine("continue from loop start"); continue; } System.Console.WriteLine("counter = " + counter); total += counter; } System.Console.WriteLine("total = " + total); } } [/csharp]
Illustrates the use of constants
/* Mastering Visual C# .NET by Jason Price, Mike Gunderloy Publisher: Sybex; ISBN: 0782129110 */ /* Example2_7.cs illustrates the use of constants */ public class Example2_7 { public static void Main() { const int Length = 3; // mathematical constant pi const double Pi = 3.14159; // speed of light in meters per second const double SpeedOfLight = 2.99792e8; System.Console.WriteLine(Length); System.Console.WriteLine(Pi); System.Console.WriteLine(SpeedOfLight); } }