[C#] 데이터

vector·2025년 8월 29일

C# 공부

목록 보기
1/2

1. 변수

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            // [데이터 타입] [이름];
            //int hp;
            // [이름] = [값]
            //hp = 100;
            // [데이터 타입] [이름] = [값]
            int hp = 100;
            int maxHp;

            // TODO
            maxHp = hp;


            Console.WriteLine(hp);
        }
    }
}

2. 정수 형식

3. 2진수, 10진수, 16진수

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            // 십진법
            // 0 1 2 3 4 5 6 7 8 9 10
            int hp = 100;
            // 이진법
            // 0b00 0b01 0b10 0b11
            hp = 0b01100100;
            // 십육진법
            // 0 1 2 3 4 5 6 7 8 9 A B C D E F
            hp = 0x64;
        }
    }
}

4. float

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            // 4byte
            float a = 3.14223424f;

            // 8byte
            double b = 3.14;

            Console.WriteLine(a);
        }
    }
}

5. string

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            string name;
            name = "YIDO";

            char ch;
            ch = 'Y';

            Console.WriteLine(name);
            // 출력 : YIDO
            Console.WriteLine(ch);
            // 출력 : Y
        }
    }
}

6. bool

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            // ex
            // isAdmin = true / false;
            // soundOnOff = true / fasle;
            // autoPlay = true / false;
            bool a;

            a = true;
            a = false;
        }
    }
}

7. 캐스팅

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            // 4byte
            int a = 100;
            // 2byte
            //short b = a; // 용량이 작아서 에러 발생
            short b = (short)a;

            float c = a;
            int d = (int)c;
        }
    }
}

8. 스트링 포맷

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // int      정수형 (1 2 3 4 5)
            // float    실수형 (3.14)
            // string   문자열 ("YIDO")
            // Bool     불리언 (true / false)

            // int a = 100;
            // string b = (string)a; 불가능 (string은 클래스이기에)

            // string -> int
            //string input;
            //input = Console.ReadLine();
            //int number = int.Parse(input); // string을 int로 변환
            //Console.WriteLine(input);

            // int -> string
            int hp = 100;
            int maxHp = 100;

            //string.Format("당신의 HP는 {0}/{1} 입니다.", hp, maxHp);


            //string message = string.Format("당신의 HP는 {0}/{1} 입니다.", hp, maxHp);
            //Console.WriteLine(message);


            string message = $"당신의 HP는 {hp} / {maxHp}입니다.";
            Console.WriteLine(message);
        }
    }
}

9. 산술 연산

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            int hp = 100;

            hp = hp - 5;

            Console.WriteLine(hp);

            // 산술 연산
            // + - * / %

            // 몫 나머지

            int a = 10 / 3;
            int b = 10 % 3;
            Console.WriteLine(a);
            Console.WriteLine(b);
            
            int c = 10;
            c += 5; // c = c + 5;
            c++; // c += 1; c = c + 1;

            Console.WriteLine(c++); // c를 출력하고 ++ 하겠다. -> c = 16
            Console.WriteLine(++c); // ++하고 c를 출력하겠다. -> c = 18
        }
    }
}

10. 비교 연산

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            // 비교 연산
            // < <= > >= == !=

            int hp = 100;
            int maxHp = 100;

            bool isFullHp = (hp == maxHp);
            Console.WriteLine(isFullHp);

            bool isAlive = (hp > 0);
            Console.WriteLine(isAlive);
            int level = 10;

            bool canEnterDungeon = (level <= 5);
            Console.WriteLine(canEnterDungeon);
        }
    }
}

11. 논리 연산

namespace Cshap
{
    class Program
    {
        static void Main(string[] args)
        {
            bool isTall = true; // 키가 크다
            bool isSmart = false; // 똑똑하다 

            // and && / or || / not !

            int level = 1;
            int gold = 100;

            bool isHighLevel = (level >= 10);
            bool isRich = (gold >= 1000);

            bool cnaEnter = isHighLevel && isRich;
        }
    }
}
profile
게임 클라이언트 프로그래머 준비중 (공부 및 기록용)

0개의 댓글