본문 바로가기

C#/공부

1. C# 시작 - Hello, world! 와 변수, 자료형, 서식 지정자

1. Hello, world!

 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello, world!");
            Console.Write("Hello, world!");
        }
    }
}

 

 

 

2. 정수 자료형

 

C#에서 개행 문자는 \r\n 이다.

 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            byte b = 200;
            sbyte sb = -126;
            short sh = 30000;
            ushort ush = 40000;
            int i = 100000000;
            uint ui = 1000000000;
            long l = 1000000000000000000;
            ulong ul = 10000000000000000000;

            Console.WriteLine("byte={0},\r\nsigned byte={1},\r\nshort={2},\r\nunsigned short={3},\r\nint={4},\r\nunsigned int={5},\r\nlong={6},\r\nunsighned long={7}", b, sb, sh, ush, i, ui, l, ul);
        }
    }
}

 

 

 

3.  실수, 문자/문자열, 논리 자료형

 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //실수 자료형
            float f = 123.567891011f;
            double d = 1234.567878910111213;
            decimal de = 1234.56789101112131415161718m;
            //문자, 문자열 자료형
            char c = 'H';
            string s = "ello";
            //논리 자료형
            bool T = true;
            bool F = false;

            Console.WriteLine("float={0},\r\ndouble={1},\r\ndecimal={2}", f, d, de);
            Console.WriteLine("\r\n{0}{1}", c, s);
            Console.WriteLine("\r\nT={0}, F={1}", T, F);
        }
    }
}

 

 

 

4. 객체 자료형

 

Object는 모든 자료형의 부모 클래스이다.

 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            object i = 12345678910;
            object d = 12345.67891011;
            object T = true;
            object s = "안녕하세요";

            Console.WriteLine("O={0}, \r\nO={1}, \r\nO={2}, \r\nO={3}", i, d, T, s);
        }
    }
}

 

 

 

5. 여러가지 서식 지정자

 

서식 지정자 기본 형식: {인덱스[, 출력 너비][:서식 문자열]}

 

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 81, a2 = 27;
            //기본적 사용법
            Console.WriteLine("{0} + {1} = {2}", a, a2, a + a2);
            Console.WriteLine("{1} - {0} = {2}", a, a2, a2 - a);
            Console.WriteLine();

            a = 12345678;
            double b = 12.345678;
            //수치 서식 문자열
            Console.WriteLine("통화 (C) . . . : {0:C}", a);
            Console.WriteLine("10진법 (D) . . : {0:D}", a);
            Console.WriteLine("지수 표기법 (E): {0:E}", b);
            Console.WriteLine("고정 소수점 (F): {0:F}", b);
            Console.WriteLine("일반 (G) . . . : {0:G}", a);
            Console.WriteLine("숫자 (N) . . . : {0:N}", a);
            Console.WriteLine("16진법 (X) . . : {0:X}", a);
            Console.WriteLine("백분율 (P) . . : {0:P}", b);
            Console.WriteLine();
                
            a = 1234;
            b = 12.345678;
            //사용자 지정 수치 서식 문자열
            Console.WriteLine("0 자리 표시 (0) . . . .: {0:00000}", a);
            Console.WriteLine("10진수 자리 표시 (#) . : {0:#####}", a);
            Console.WriteLine("소수점 (.) . . . . . . : {0:0.00000}", b);
            Console.WriteLine("천 단위 자릿수 표시 (,): {0:0,0}", a);
            Console.WriteLine("백분율 자리 표시 (%) . : {0:0%}", b);
            Console.WriteLine("지수 표기법 (e) . . . .: {0:0.000e+0}", b);
            Console.WriteLine();

            a = 12345;
            //출력 너비 지정
            Console.WriteLine("|{0,15}|", a);
            Console.WriteLine("|{0,-15}|", a);
            Console.WriteLine("|{0,15:N0}|", a);    //{인덱스, 출력너비:서식 문자열}
            Console.WriteLine("|{0,-15:N0}|", a);   
        }
    }
}

 

'C# > 공부' 카테고리의 다른 글

C# 실력 늘리기 1일차  (0) 2022.07.19
시리얼 통신  (0) 2022.07.11
4. 클래스와 메소드  (0) 2022.07.06
3. 조건문 / 반복문 / 제어문  (0) 2022.07.06
2. 연산자와 배열  (0) 2022.07.05