티스토리 뷰

LANGUAGE/C#

[C#] 데이터형

진심스테이크 2018. 6. 12. 20:40

기본 데이터형

- object로부터 파생된 객체

ex) System.Object == object;

- CTS에서 정의된 객체

 

 

 형태

CTS 

byte 

 

정수형

 bool

System.Boolean 

1 byte 

char

System.Char 

2 byte 

byte 

System.Byte 

1 byte 

sbyte 

System.SByte 

1 byte 

short

System.Int16 

2 byte

ushort

 System.UInt16

2 byte 

int

System.Int32

4 byte 

uint

System.UInt32 

4 byte 

long 

System.Int64 

8 byte 

ulong 

System.UInt64 

8 byte 

#s (signed) : 음의 부호

#u (unsigned) : 양의 부호

 

 

실수형

float 

System.Single 

4 byte 

double 

System.Double 

8 byte 

decimal 

System.Demical 

16 byte 

 

 

문자열형

- 배열, 포인터를 쓰지 않고 문자열형을 제공

string 

System.String 

 

 


 

 

bool

- true, false 대신 0과 그 외의 값은 사용 금지

namespace _0612
{
    class Program
    {
        static bool BoolVar; //false
        static void Main(string[] args)
        {
            bool LocalBoolVar = true;
            Console.WriteLine("{0} {1}", BoolVar, LocalBoolVar);
        }
    }
}

 

 

char

- 유니코드 : 2 byte

- char 형의 암시적 값 변환 : ushort, int, uint, long, ulong, float, double, decimal

ex) int Number = 7;

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            char Num = '7'; //문자 상수값 : 55
            int Num1 = Num;
            Num1 = Num + 1;
            Console.WriteLine("Num = {0}, 문자상수값 = {1}, 유니코드 문자 = {2}", (int)Num, Num1, (char)Num1);
        }
    }
}

 

 

byte, sbyte

- byte 유효 범위 : 부호 없는 0 ~ 255

- sbyte 유효 범위 : -128 ~ 127

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            int result;
            byte x = 1, y = 2;
            result = x + y;
            Console.WriteLine(result);
        }
    }
}

 

 

short, ushort

- short 유효 범위 : -32768 ~ 32767

- ushort 유효 범위 : 0 ~ 65535

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("{0} ~ {1}", short.MinValue, short.MaxValue);
        }
    }
}

 

 

int, uint

- int 유효 범위 : -2,147,483,648 ~ 2,147,483,647

- uint 유효 범위 : 0 ~ 4,294,967,295

 

 

float 

- 소수점 뒤에 f, F 접미사 명시 없으면 double형

ex) float num = 3.14f;

 

 

string

- 문자열 끝에 0, '\0'

- '+' : 문자열 연결

- '==' : 뮨저열 비교

- [인덱스] : 문자

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "Hello world";
            Console.WriteLine("문자 갯수 : {0}", str.Length);
        }
    }
}
namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "C:\\temp\\test.txt";
            string str2 = @"C:\temp\test.txt";
            Console.WriteLine("{0} {1}", str1, str2);
        }
    }
}

 

 

var

- 암시적 데이터형

- 대입되는 데이터에 따라 데이터형 결정

- var를 사용 할 수 없는 경우

1. null값 초기화, 매개변수로는 사용 못함

2. var는 지역변수로만 사용 -> 클래스 맴버로는 사용 못함

3. 연속적으로 초기화 하는경우

ex) var m = 10, n = 20;

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            var value = 3.14f; //float
            float value2 = 10.12f;
            float sum = value + value2;
            Console.WriteLine("{0} {1:f1} {2}", value, value2, sum); //f1 :소수점 첫째자리까지 출력
        }
    }
}

 

 

nullable

- null을 허용하지 않는 데이터형이 null값을 허용

- 형식

  데이터형? 변수명;

 ex) int? var1;

      bool? var2 = null; //true, false, null

- 속성

  - .HasValue // true, false

  - .Value // 읽기 전용

namespace _0612
{
    class Program
    {
        static void Main(string[] args)
        {
            int? num = null;
            if (num.HasValue)
                Console.WriteLine("올바른 값");
            else
                Console.WriteLine("null 값");

            Console.WriteLine("null : {0}", num);
        }
    }
}

 

 

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

[C#] 값 형식 / 참조 형식  (0) 2018.06.12
[C#] 사용자 지정형  (0) 2018.06.12
[C#] 표준 입력  (0) 2018.06.12
[C#] 변환  (0) 2018.06.12
[C#] .NET FRAMEWORK  (0) 2018.06.12
댓글