728x90
반응형

박싱과 언박싱

  • 박싱(boxing)

      데이터 형을 최상위 object 형으로 변환하여 힙(heap 메모리)에 저장

      int m = 123;

      object obj = m;

 

  • 언박싱(unboxing)

      힙에 저장된 형식을 다시 원래의 형식으로 변환

      int n = (int)obj;

 

 

예제) int형 값을 박싱한 후에 다시 언박싱하여 출력해보자.

박싱과 언박싱 과정에서 메모리 공유가 발생하는지, 또는 복사가 발생하는지 확인해 보자. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication10
{
    class Program
    {
        static void Main(string[] args)
        {
            //int i = 123;
            //object obj = i;
            // (int) = cast 연산자
            //Console.WriteLine("{0}", (int)obj); //123 출력



            int i = 123;
            object o = i; //힙 메모리
            i = 456;
            Console.WriteLine("{0} {1}", i, (int)o); //456 123
        }
    }
}

 

03. 표준입력


  • Console.ReadKey()

      사용자가 눌린 키 한 문자 정보를 리턴하는 메서드

 

  • 함수 원형

      public static ConsoleKeyInfo ReadKey()

      public static ConsoleKeyInfo ReadKey(bool intercept)

      true : 화면 출력 안함    false : 화면 출력

 

  • ConsoleKeyInfo

      키의 문자와 Shift, Alt, Ctrl 보조키 상태 포함

 

 

ConsoleKeyInfo.Key, ConsoleKey 열거형 값, ConsoleKey.A, ConsoleKey.Escape 등..., MSDN 참조

3-10 예제) 사용자가 눌린 키를 화면에 출력하되 Escape 키가 입력되면 프로그램을 종료하도록 작성

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleRead01
{
    class Program
    {
        static void Main(string[] args)
        {           
            ConsoleKeyInfo KeyInfo;
            do
            {
                KeyInfo = Console.ReadKey();
                if (KeyInfo.Key == ConsoleKey.A)
                    Console.WriteLine("a가 눌렸다");
            } while (KeyInfo.Key != ConsoleKey.Escape); // Escape가 아니면 계속반복 //ESC
        }
    }
}

ConsoleKeyInfo.KeyChar 눌린 키의 유니코드를 얻는 속성으로 대소문자 등을 모두 구분할 수 있다.

3-11 예제) 사용자가 눌린 키를 출력하는 프로그램을 작성해 보자.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleReadLine01
{
    class Program
    {
        static void Main(string[] args)
        {
            ConsoleKeyInfo KeyInfo;
            do
            {
                KeyInfo = Console.ReadKey(true); // 화면을 출력하지않고 입력을 받아준다.
                if (KeyInfo.KeyChar == 'a')
                    Console.WriteLine("a가 눌렸어"); // a만 반응 나머지키 반응x
               // Console.Write(KeyInfo.KeyChar);
            } while (KeyInfo.Key != ConsoleKey.Escape);
        }
    }
}

ㄴㅁㅇ

  • Console.ReadLine()

      엔터키가 눌려질 때까지 입력 받은 문자열을 리턴하는 메서드

 

  • 활용

      입력 받은 문자열을 숫자로 사용할 때는 Convert.Toint32() 등의 메서드를 사용

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleReadLine02
{
    class Program
    {
        static void Main(string[] args)
        {
            int Kor, Eng, Math, Total;
            float Average;

            Console.Write("국어 점수를 입력해 주세요 : ");
            Kor = Convert.ToInt32(Console.ReadLine());
            Console.Write("영어 점수를 입력해 주세요 : ");
            Eng = Convert.ToInt32(Console.ReadLine());
            Console.Write("수학 점수를 입력해 주세요 : ");
            Math = Convert.ToInt32(Console.ReadLine());
            Total = Kor + Eng + Math;
            Average = Total / 3.0f;
            Console.WriteLine("{0} {1} {2} {3} {4:f1}", Kor, Eng, Math, Total, Average);
        }
    }
}

728x90
반응형

+ Recent posts