c# 기초구문 업데이트 완료

This commit is contained in:
2025-03-05 11:57:12 +09:00
parent 15885a2286
commit 6ebd0fb5a9
22 changed files with 740 additions and 0 deletions
+10
View File
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
+43
View File
@@ -0,0 +1,43 @@
using static MyClass;
internal class Program
{
private static void Main(string[] args)
{
MyClass cls = new();
cls.OnFinished += new OnFinishedEventHandler(Cls_OnFinished);
cls.OnFinished += Cls_OnFinished;
//Annonymous Method 사용
cls.OnFinished2 += delegate (object? sender, EventArgs eventArgs)
{
Console.WriteLine("Finished");
};
//Lambda 식 사용, 무명메서드를 대신할 수 있다.
//(입력 파라미터) => { 실행문장 블럭 };
cls.OnFinished += (sender, eventArgs) => { Console.WriteLine("Finished"); };
}
private static void Cls_OnFinished(object? sender, EventArgs eventArgs)
{
Console.WriteLine("Finished");
}
}
class MyClass
{
//EventHandler를 delegate로 구현
public delegate void OnFinishedEventHandler(object? sender, EventArgs eventArgs);
public event OnFinishedEventHandler? OnFinished;
public EventHandler<EventArgs>? OnFinished2;
public void Run(int complexity)
{
Thread.Sleep(complexity);
OnFinished?.Invoke(this, EventArgs.Empty);
OnFinished2?.Invoke(this, EventArgs.Empty);
}
}