C#교과서 마스터하기 4. 숫자 이외의 데이터 형식

min seung moon·2021년 7월 9일
0

C#

목록 보기
4/54

https://www.youtube.com/watch?v=zOPzGzQlyuY&list=PLO56HZSjrPTB4NxAsEP8HRk6YKBDLbp7m&index=14

1. 문자 데이터 형식 : char

  • 문자형 변수는 2byte의 공간에 하나의 문자를 저장
  • 문자형 변수는 char 키워드를 사용하여 선언하고 값을 초기화할 때에는 두개의 작은 따옴표(')로 문자 하나를 묶어준다
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            char word = 'a';
            char kor = '가';
            char es = '\n';
            WriteLine("{0}, {2}, {1}", word, kor, es);

        }
    }
}

2. 문자열 데이터 형식 : string

  • 문자열 변수는 string 키워드를 사용하여 선언하고 값을 초기화할 때에는 두개의 큰 따옴표(")로 문자 하나를 묶어준다
  • @ 키워드로 멀티라인 문자열을 지원해준다
  • StringInterpolation, 문자열 보간법(보간된 문자열)
    • $"{}" 형식으로 흔히 template Literal이라고도 한다
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "abcdef";
            string name = "문민승";
            string es = "\n\n\n\n";
            WriteLine($"{str}, {es}, {name}");

        }
    }
} }
}

using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = @"a
                            b
                            c
                            d
                            e
                            f";
            string str2 = "a" +
                "b" +
                "c";
            WriteLine(str);
            WriteLine(str2);

        }
    }
}

3. 논리 데이터 형식 : bool

  • true || flase 값만 가능, 0, "" 등의 논리적인 값(truthy, falsy)은 해당 안된다
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            bool check = true;
            bool success = false;

            if(check)
            {
                WriteLine("성공");
            }else
            {
                WriteLine("실패");
            }
            if(success)
            {
                WriteLine("성공");
            }
            else
            {
                WriteLine("실패");
            }

        }
    }
}

4. 변하지 않는 값 : 상수(Constant)

  • 상수(Constant), 변하지 않는 변수, 읽기 전용 변수
  • 상수 선언과 동시에 초기화, 변수 명은 대문자로 작성

5. 닷넷 데이터 형식

using System;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            Char c = 'A'; //char
            String s = "ABC"; // string
            Boolean b = true; // bool

            Console.WriteLine($"{c}, {s}, {b}");
        }
    }
}

6. 래퍼 형식

  • 래퍼형식 : int, string과 같은 기본 형식을 클래스 또는 구조체로 감싼 .net 형식
using System;
using static System.Console;

namespace testProject
{
    class Program
    {
        static void Main(string[] args)
        {
            int number1 = 1234; // int 키워드 : 기본형식
            Int32 number2 = 1234; // System.Int32 구조체 : .NET 형식

            WriteLine($"{number1}, {number2}");

            string str1 = "abc"; // string 키워드 : 기본 형식
            String str2 = "abc"; // System.String 구조체 : .NET 형식

            WriteLine($"{str1}, {str2}");
        }
    }
}

profile
아직까지는 코린이!

0개의 댓글