sample code
This commit is contained in:
28
MySolution/ConsoleApp/Samples/CalculateSample.cs
Normal file
28
MySolution/ConsoleApp/Samples/CalculateSample.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
class CalculateSample
|
||||
{
|
||||
private delegate int Calculate(int x, int y);
|
||||
|
||||
public static int Add(int x, int y)
|
||||
{
|
||||
return x + y;
|
||||
}
|
||||
|
||||
public static int Subtract(int x, int y)
|
||||
{
|
||||
return x - y;
|
||||
}
|
||||
|
||||
public static void Sample()
|
||||
{
|
||||
Calculate calc1 = Add;
|
||||
Calculate calc2 = Subtract;
|
||||
|
||||
Console.WriteLine(calc1(10, 5));
|
||||
Console.WriteLine(calc2(10, 5));
|
||||
}
|
||||
}
|
||||
}
|
33
MySolution/ConsoleApp/Samples/EventSample.cs
Normal file
33
MySolution/ConsoleApp/Samples/EventSample.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
public delegate void CustomEventHandler(object sender, EventArgs e);
|
||||
|
||||
class EventSample
|
||||
{
|
||||
public static void Sample()
|
||||
{
|
||||
Button button = new Button();
|
||||
button.Click += new CustomEventHandler(Button_Click);
|
||||
|
||||
button.OnClick();
|
||||
}
|
||||
|
||||
private static void Button_Click(object sender, EventArgs e)
|
||||
{
|
||||
System.Console.WriteLine("Button clicked!");
|
||||
}
|
||||
}
|
||||
|
||||
class Button
|
||||
{
|
||||
public event CustomEventHandler Click;
|
||||
|
||||
public void OnClick()
|
||||
{
|
||||
if (Click != null)
|
||||
Click(this, EventArgs.Empty);
|
||||
}
|
||||
}
|
||||
}
|
29
MySolution/ConsoleApp/Samples/MessagePrinterSample.cs
Normal file
29
MySolution/ConsoleApp/Samples/MessagePrinterSample.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
class MessagePrinterSample
|
||||
{
|
||||
private delegate Task MessagePrinter(string message);
|
||||
|
||||
private static async Task PrintMessageAsync(string message)
|
||||
{
|
||||
await Task.Delay(1000);
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
public static void Sample()
|
||||
{
|
||||
MessagePrinter printer = PrintMessageAsync;
|
||||
Console.WriteLine("Start...");
|
||||
|
||||
printer("This is first message");
|
||||
printer("This is second message");
|
||||
printer("This is third message");
|
||||
|
||||
Console.WriteLine("Finish...");
|
||||
}
|
||||
}
|
||||
}
|
40
MySolution/ConsoleApp/Samples/StringLength.cs
Normal file
40
MySolution/ConsoleApp/Samples/StringLength.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using System;
|
||||
|
||||
namespace Samples
|
||||
{
|
||||
class StringLength
|
||||
{
|
||||
public static void Sample()
|
||||
{
|
||||
string[] words = new string[]
|
||||
{
|
||||
"Apple",
|
||||
"Banana",
|
||||
"Cherry",
|
||||
"Pear",
|
||||
};
|
||||
|
||||
CalculateStringLength(words, PrintStingLength);
|
||||
}
|
||||
|
||||
private static void CalculateStringLength(string[] words, Action<int[]> callback)
|
||||
{
|
||||
int[] lengths = new int[words.Length];
|
||||
for (int i = 0; i < words.Length; i++)
|
||||
{
|
||||
lengths[i] = words[i].Length;
|
||||
}
|
||||
|
||||
if (callback != null)
|
||||
callback(lengths);
|
||||
}
|
||||
|
||||
private static void PrintStingLength(int[] lengths)
|
||||
{
|
||||
foreach (int length in lengths)
|
||||
{
|
||||
Console.WriteLine(length);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user