[C#] 공용 형식 시스템.


C#은 다양한 종류의 데이터를 다룰수 있다.

byte, int, float, string, object와 같은 다양한 기본데이터 형식을 제공하며,

프로그래머가 이들을 조합해여 복잡한 데이터 형식을 만들어 사용할 수 있다.


또한, C#에서는 스택과 힙이라는 두 가지 메모리 영역을 활용함으로써 변수의 생명주기에 따라 변수를

값 형식이나 참조형식으로 만들어 사용할 수 있다.


이 모든 데이터 형식 체계는 사실 C#의 고유의 것이 아니다.

공용 형식 시스템(Common Type System)이라고 하는 .NET 프레임워크의 형식 체계의 표준을 그대로

따르고 있을 뿐이다.


공용 형식 시스템의 뜻을 풀어보자면 "모두가 함께 사용하는 데이터 형식 체계" 라고 할 수 있다.

C#을 비롯한 .NET 프레임워크를 지원하는 모든언어를 뜻한다.


마이크로소프트가 이러한 시스템을 도입한 이유는 .NET 언어들끼리 호환성을 갖도록 하기 위해서이다.



C#과 C++의 기본 데이터 형식을 살펴 보도록 하자.


클래스 이름 

C# 형식 

 C++ 형식

 비주얼 베이직 형식 

 System.Byte

 byte

 unsigned char

 Byte

 System.SByte

 sbyte

 char

 SByte

 System.Int16

 short

 short

 Short

 System.Int32

 int

 int 또는 long

 Integer

 System.Int64

 long

 __int64

 Long

 System.UInt16

 ushort

 unsigned short

 UShort

 System.UInt32

 uint

 unsigned int   또는 

 unsigned long

 UInteger

 System.UInt64

 ulong

 unsigned __int64

 ULong

 System.Single

 float

 float

 Single

 System.Double double double Double

 System.Boolean

 bool bool Boolean
 System.Char char wchar_t Char
 System..Decimal decimal Decimal Decimal
 System.IntPtr

 없음

 없음

 없음

 System.UIntPtr

 없음 없음 없음
 System.Object object Object*

 Object

 System..String string String* String




공용 형식 시스템의 형식은 각 언어에서 코드에 그대로 사용할수 있다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System;
 
namespace CTS
{
    class MainApp
    {
        static void Main(string[] args)
        {
            System.Int32 a = 123;
            int b = 456;
 
            Console.WriteLine("a type:{0}, value:{1}", a.GetType().ToString(), a);
            Console.WriteLine("b type:{0}, value:{1}", b.GetType().ToString(), b);
            
            System.String c = "abc";
            string d = "def";
 
            Console.WriteLine("c type:{0}, value:{1}", c.GetType().ToString(), c);
            Console.WriteLine("d type:{0}, value:{1}", d.GetType().ToString(), d);
 
            // 결과
            // a type: System.Int32, value: 123
            // b type: System.Int32, value: 456
            // c type: System.String, value: abc
            // d type: System.String, value: def
        }
    }
}
cs




※ GetType() 메소드 ToString() 메소드


모든 데이터 형식은 object 형식으로부터 상속받는다고 말한적이 있을것이다.

GetType()와 ToString()메소드는

System.Int32와 int, System.String과 string 형식이

object형식으로부터 물려받아 갖고 있는 것이다.