C#교과서 마스터하기 5. 사용자한테 얻은 정보를 변수에 저장

min seung moon·2021년 7월 9일
0

C#

목록 보기
5/54
post-thumbnail

https://www.youtube.com/watch?v=QSf4HUR0nPI&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=16

1. 문자열 입력 관련 메서드

  • Console.ReadLine()으로 콘솔(터미널)로 부터 데이터 입력받기
    • string 형태로 return
  • Console.Read()로 콘솔로 부터 데이터 1개 입력받기
    • int 형태로 return, 아스키코드의 정수형으로 반환이 된다
    • Convert.ToChar(); method를 사용하여 실제 값 확인할 수 있다
using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Write("성함을 입력해주세요 : ______\b\b\b\b\b\b");
            string str = ReadLine();
            WriteLine(str);
        }
    }
}

using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int asc = Read();
            WriteLine(asc);
            WriteLine(Convert.ToChar(asc));
        }
    }
}

2. 형식 변환

  • 작은 데이터 타입 -> 큰 데이터 타입으로 변환하는데에는 문제가 없지만
  • 큰 데이터 타입 -> 작은 데이터 타입으로 형변환을 하게 되면 데이터 손실이 일어날수 있다
  • 동일한 타입이 아닌 다른 타입일 경우 .NET 형식의 Parse() || Convert.ToXXX() method 활용
using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            long l = long.MaxValue;
            int i = (int)l;
            WriteLine(i);

            i = int.MaxValue;
            l = (long)i;
            WriteLine(l);
        }
    }
}

using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            string strNum = ReadLine();
            int number = Int32.Parse(strNum);
            WriteLine(strNum + number);
            WriteLine(number + number);

            strNum = ReadLine();
            number = Convert.ToInt32(strNum);
            WriteLine(strNum + number);
            WriteLine(number + number);
        }
    }
}

3. 이진수 다루기

  • 정수(10진수)를 2진수로 변환 할 때에는 Convert.ToString(정수값, 2);
  • 2진수를 정수(10진수)로 변환 할 때에는 Convert.ToInt32(2진수값, 2);
  • 0b || 0B를 활용해서 2진수 표현도 가능하다
using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 10; // -> 1010
            WriteLine(Convert.ToString(number, 2));
            WriteLine(Convert.ToString(number, 2).PadLeft(8, '0'));

            string str = "1010";
            WriteLine(Convert.ToInt32(str, 2));

            WriteLine(0b0001);
            WriteLine(0b1010_0001);
        }
    }
}

4. var 키워드로 암시적으로 형식화된 로컬 변수 만들기

  • var 키워드 : 암시적으로 형식화된 로컬 변수
    • 입력 받는 값에 따라서 변수의 타입이 결정된다
    • JS의 var와는 다르며 JS의 var는 dynamic 타입과 같다
using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            var type1 = 'a';
            WriteLine(type1.GetType());

            var type2 = "abc";
            WriteLine(type2.GetType());

            var type3 = 123;
            WriteLine(type3.GetType());
        }
    }
}

5. 변수의 기본값을 default 키워드로 설정하기

  • 각 타입에 지정된 default value가 대입된다
using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int i = default;
            WriteLine(i);

            double d = default;
            WriteLine(d);

            char c = default;
            WriteLine(c);

            string s = default;
            WriteLine(s);
        }
    }
}

profile
아직까지는 코린이!

0개의 댓글