Development/C#

[프랙티컬 C# 연습문제] 1부 - 3장

이쥬딩 2023. 7. 15. 00:36
728x90

대리자(delegate)

-대리자는 메서드를 대신 호출하는 기법

-대리자는 메서드의 주소를 참조

-대리자를 사용하면 메서드끼리 연결 및 조합이 가능

 

* Predicate<T>

-delegate의 제네릭 버전

-델리케이트를 직접 정의하지 않아도 된다.

익명 메서드

메서드를 생성하지 않고도 delegate를 생성할 수 있게 해준다.

// delegate(int n) {return n % 2 == 0;} = 익명 메서드
var count = Count(numbers, delegate(int n) {return n % 2 == 0;});

람다식

-일종의 메서드

-C# 3.0부터 delegate 키워드가 없어지고 그 대신 =>(람다 연산자)가 사용된다.

-추상도 증가

var count = Count(numbers, n => n % 2 == 0);

LINQ

-Language Integrated Query

-객체, 데이터, XML과 같은 다양한 데 이터를 표준화된 방법으로 처리할 수 있다.

 

* 지연실행

LINQ의 특징으로 쿼리는 해당 시점에서 실행되지 않고 실제 값이 필요할 때 실행된다.

// query 변수에 검색된 결과가 대입되는 것이 아니다.
var query = names.Where(s => s.Length <=5 );

names[0] = "Busan";
foreach (var item in query) // 변수에 담기는게 아니라 쿼리문이 이때 실행된다.
	Console.WriteLine(item);

*즉시실행

-검색 된 결과가 변수에 저장된다.

-ToArray, ToList, Count 메서드 등 하나의 값을 반환하는 메서드는 즉시 실행되는 메서드다.

// query 변수에 검색된 결과가 대입된다.
var query = names.Where(s => s.Length <= 5).ToArray();

 

문제 3.1

1.

  static void Main(string[] args)
    {
      var numbers = new List<int> { 12, 87, 94, 14, 53, 20, 40, 35, 76, 91, 31, 17, 48 };

      Console.WriteLine(numbers.Exists(s => s % 8 == 0 || s % 9 == 0));
    }

2.

static void Main(string[] args)
    {
      var numbers = new List<int> { 12, 87, 94, 14, 53, 20, 40, 35, 76, 91, 31, 17, 48 };
      numbers.ForEach(s => Console.WriteLine(s / 2));
    }

3.

static void Main(string[] args)
    {
      var numbers = new List<int> { 12, 87, 94, 14, 53, 20, 40, 35, 76, 91, 31, 17, 48 };
      var result = numbers.Where(w => w >= 50);
      Console.WriteLine(String.Join(",", result));
    }

4.

static void Main(string[] args)
    {
      var numbers = new List<int> { 12, 87, 94, 14, 53, 20, 40, 35, 76, 91, 31, 17, 48 };
      List<int> result = numbers.Select(s => s * 2).ToList();
      Console.WriteLine(String.Join(",", result));
    }

문제 3.2

1.

static void Main(string[] args)
    {
      var names = new List<string> {
        "Seoul","New Delhi","Bangkok", "London", "Paris", "Berlin", "Canberra", "Hong Kong"
      };

      var line = Console.ReadLine();

      int index = names.FindIndex(s => s == line);

      Console.WriteLine(index);
    }

2.

static void Main(string[] args)
    {
      var names = new List<string> {
        "Seoul","New Delhi","Bangkok", "London", "Paris", "Berlin", "Canberra", "Hong Kong"
      };

      int count = names.Count(s => s.Contains('o'));

      Console.WriteLine(count);
    }

3.

static void Main(string[] args)
    {
      var names = new List<string> {
        "Seoul","New Delhi","Bangkok", "London", "Paris", "Berlin", "Canberra", "Hong Kong"
      };

      var citiesIncludedO = names.Where(s => s.Contains('o')).ToArray();

      Console.WriteLine(String.Join(",", citiesIncludedO));
    }

4.

static void Main(string[] args)
    {
      var names = new List<string> {
        "Seoul","New Delhi","Bangkok", "London", "Paris", "Berlin", "Canberra", "Hong Kong"
      };

      var count = names.Where(w => w[0] == 'B').Select(s => s.Count());

      Console.WriteLine(String.Join(",", count));
    }
728x90