C#교과서 마스터하기 39. 튜플(Tuple)

min seung moon·2021년 7월 15일
0

C#

목록 보기
41/54

https://www.youtube.com/watch?v=S99SmK8hQt4&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=74

1. 튜플(Tuple)

  • 괄호 기호의 간단한 구문을 사용하여 하나 이상의 속성을 가지는 개체를 만드는 형식
  • 배경
    • 메서드의 매개 변수를 전달할 때 사용자 정의 클래스를 사용하면 한 번에 여러 개의 값을 전달할 수 있다
    • 튜플은 새로운 클래스를 만들지 않고도 언어 차원에서 여러 개의 값을 전달할 수 있는 기능을 제공하여 편리함을 준다

2. 프로젝트

01. 튜플 선언 및 초기화

using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Reflection;

namespace testProject
{
   
    
    class Program
    {         
        static void Main(string[] args)
        {
            var r = (12, 34, 56); // 3개의 int 형식데이터가 r 변수에 담김
            WriteLine(r.Item1);
            WriteLine(r.Item2);
            WriteLine(r.Item3);

            var fhd = (1920, 1080); // [1] 기본 : Item1, Item2 형태
            WriteLine(fhd.Item1);
            WriteLine(fhd.Item2);

            var uhd = (Width: 3840, Height: 2160); // [2] 이름 지정
            WriteLine(uhd.Width);
            WriteLine(uhd.Height);

            (ushort Width, ushort Height) hd = (1366, 768); // [3] 이름과 형식 지정
            WriteLine(hd.Width);
            WriteLine(hd.Height);
            WriteLine(hd.Width.GetType());


        }
    }
}

02. 튜플 리턴

using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Reflection;

namespace testProject
{
   
    class Program
    {
        // [1] 튜플 리턴 : 형식(int, int)
        static (int, int) Tally1()
        {
            var r = (12, 3); //[A] 튜플 리터럴에 2개의 값 담기
            return r; // [B] 튜플 리터럴 반환
        }

        // [2] 튜플 리턴에 이름 값 지정
        static (int Sum, int Count) Tally2() => (45, 6);

        static void Main(string[] args)
        {
            
            WriteLine(Tally1());
            var tally2 = Tally2();
            WriteLine(tally2.Sum);
            WriteLine(tally2.Count);
        }
    }
}

03. 정리(+ 튜플 구조 분해)

using System;
using static System.Console;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.Reflection;

namespace testProject
{
   
    class Program
    {

        static void Main(string[] args)
        {
            // [1] 튜플 선언 및 초기화
            var boy = (Name: "철수", IsStudnet: true, OrderPrice: 1_000);
            WriteLine($"{boy.Name}(대학생 : {boy.IsStudnet}) - 주문 : {boy.OrderPrice:C0}");

            // [2] 튜플에 default 초기화
            static (int, int) ZeroZero() => default;
            WriteLine(ZeroZero());

            // [3] 튜플 메서드에서 이름 지정
            static (int First, int Second) NameTuple()
            {
                var r = (100, 200);
                return r;
            }
            var rr = NameTuple();
            WriteLine(rr.First);
            WriteLine(rr.Second);

            // 튜플 분해(Tuple Deconstuctuon) 또는 튜플 해제 작업
            var (first, second) = NameTuple();
            WriteLine(first);
            WriteLine(second);


        }
    }
}

profile
아직까지는 코린이!

0개의 댓글