sample code

main
syneffort 2 years ago
parent ff8222b942
commit 2a2319833a
  1. 24
      MySolution/ConsoleApp/.vscode/launch.json
  2. 41
      MySolution/ConsoleApp/.vscode/tasks.json
  3. 10
      MySolution/ConsoleApp/ConsoleApp.csproj
  4. 30
      MySolution/ConsoleApp/Program.cs
  5. 28
      MySolution/ConsoleApp/Samples/CalculateSample.cs
  6. 33
      MySolution/ConsoleApp/Samples/EventSample.cs
  7. 29
      MySolution/ConsoleApp/Samples/MessagePrinterSample.cs
  8. 40
      MySolution/ConsoleApp/Samples/StringLength.cs
  9. 22
      MySolution/MySolution.sln

@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (console)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/bin/Debug/net7.0/ConsoleApp.dll",
"args": [],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"stopAtEntry": false
},
{
"name": ".NET Core Attach",
"type": "coreclr",
"request": "attach"
}
]
}

@ -0,0 +1,41 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/ConsoleApp.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/ConsoleApp.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/ConsoleApp.csproj"
],
"problemMatcher": "$msCompile"
}
]
}

@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

@ -0,0 +1,30 @@
using Samples;
namespace ConsoleApp;
class Program
{
static void Main(string[] args)
{
#region Delegate
// CalculateSample.Sample();
// MessagePrinterSample.Sample();
#endregion
#region Callback
// StringLength.Sample();
#endregion
#region Event
EventSample.Sample();
#endregion
}
}

@ -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));
}
}
}

@ -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);
}
}
}

@ -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...");
}
}
}

@ -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);
}
}
}
}

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.0.31903.59
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsoleApp", "ConsoleApp\ConsoleApp.csproj", "{AF3FAEF8-E438-4E8C-9A99-C0A94649EFEE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{AF3FAEF8-E438-4E8C-9A99-C0A94649EFEE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AF3FAEF8-E438-4E8C-9A99-C0A94649EFEE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AF3FAEF8-E438-4E8C-9A99-C0A94649EFEE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AF3FAEF8-E438-4E8C-9A99-C0A94649EFEE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Loading…
Cancel
Save