Csharp Simple Delegate Test
Notice the simple delegate public delegate void ByteCallBack(byte[] bytes);
using System; using System.Text; namespace DelegateTester { public delegate void ByteCallBack(byte[] bytes); public static class Printer { public static void HexPrint(byte[] bytes) { foreach (byte b in bytes) Console.Write("{0,3:X} ", b); Console.WriteLine(); } public static void Print(byte[] bytes) { foreach (byte b in bytes) Console.Write("{0,3} ", b); Console.WriteLine(); } } class Program { static void Spew(ByteCallBack method, byte[] bytes) { method.Invoke(bytes); } static void Main(string[] args) { byte[] bytes = {1, 15, 16, 42, 255}; Spew(Printer.HexPrint, bytes); Spew(Printer.Print, bytes); } } }
The output should be something like
1 F 10 2A FF 1 15 16 42 255
Tillhör Kategori Programmering