낑깡의 게임 프로그래밍 도전기

C# 람다식(Lamba) 본문

C#

C# 람다식(Lamba)

낑깡겜플밍 2025. 7. 25. 12:37
반응형
Func<int, bool> func = (int actorCount) => 
{
	Console.WriteLine();
    return actorCount > 1000;
};

=>

화살표 뒤에는 리턴값이 된다.

private static void PrintActorCount(int actorCount)
{
	Console.WriteLine(actorCount);
}

private static bool IsActorCountCheck(int actorCount)
{
	if(actorCount > 1000)
	  return true;
	return false;
}

이것을 람다식으로 만들면 아래 처럼 된다.

private static void PrintActorCount(int actorCount) => Console.WriteLine(actorCount);

private static bool Is ActorCount(int actorCount) => actorCount > 1000;

액션 델리게이트와 관련지어 살펴보면

//Action<int> action = PrintActorCount;
Action<int> action = (int actorCount) => Console.WriteLine(actorCount);

이렇게 줄여줄 수 있다.

Action<int> action = (actorCount) => Console.WriteLine(actorCount);

자료형 int를 뺴도 된다 들어오는 향식이 int라서 뺄수 있다.

 

여러줄 쓰는 법

Action<int> action = (int actorCount) => {
	Console.WriteLine(actorCount);
};

 

Func 델리게이트 람다로 바꾸기

//Func<int, bool> func = ISActorCountCheck;
Func<int, bool> func = (int actorCount) => actorCount > 1000;

여러줄로도 쓸 수 있으나 그럼 return 값을 넣어 줘야한다.

 

private static void PrintActorCount(int actorCount)
{
	Console.WriteLine(actorCount);
}
private static void SendServer(Action<int> action)
{
	Console.WriteLine("서버 요청");
    Console.WriteLine("서버 요청 도착 actorCount : 3000");
    action(3000);
}

SendServer(PrintActorCount);

이것을 아래 처럼 해 줄 수 있다.

SendServer((actorCount) => Console.WriteLine(actorCount));

private static void SendServer(Action<int> action)
{
	Console.WriteLine("서버 요청");
    Console.WriteLine("서버 요청 도착 actorCount : 3000");
    action(3000);
}

 

반응형

'C#' 카테고리의 다른 글

c# 제네릭(Generic)  (2) 2025.07.27
C# 링큐(Linq)  (3) 2025.07.26
C# 서브클래스, 인터페이스, 추상클래스  (1) 2025.07.24
C# static, deepcopy  (1) 2025.07.23
C# 의존성 주입(Dependency Injection, DI)  (1) 2025.07.22