C# 의 Data Types (Data 타입) 알아보기
C# - Data Types
C#은 강력한 형식의 언어입니다. 이는 integer, float, decimal, text (정수, 실수, 소수, 텍스트) 등과 같이 저장할 값의 종류를 나타내는 변수의 유형을 선언해야 함을 의미합니다.
다음은 다양한 데이터 유형의 변수를 선언하고 초기화합니다.
예: 다른 데이터 유형의 변수
string stringVar = "Hello World!!";
int intVar = 100;
float floatVar = 10.2f;
char charVar = 'A';
bool boolVar = true;
C#은 주로 데이터 유형을 값 유형과 참조 유형의 두 가지 유형으로 분류했습니다. 값 유형에는 단순 유형(예: int, float, bool 및 char), enum 유형, struct types 및 Nullable 값 유형이 포함됩니다. 참조 형식에는 클래스 types, 인터페이스 types, delegate types 및 array types 이 포함됩니다. 다음 장에서 값 유형 및 참조 유형 에 대해 자세히 알아보세요 .
C# 의 미리 정의된 Data Type
C# 에는 몇 가지 미리 정의된 값 형식과 참조 형식이 포함되어 있습니다. 다음 표에는 사전 정의된 데이터 유형이 나열되어 있습니다.
Type | Description | Range | Suffix |
byte | 8-bit unsigned integer | 0 to 255 | |
sbyte | 8-bit signed integer | -128 to 127 | |
short | 16-bit signed integer | -32,768 to 32,767 | |
ushort | 16-bit unsigned integer | 0 to 65,535 | |
int | 32-bit signed integer | -2,147,483,648 to 2,147,483,647 |
|
uint | 32-bit unsigned integer | 0 to 4,294,967,295 | u |
long | 64-bit signed integer | -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 |
l |
ulong | 64-bit unsigned integer | 0 to 18,446,744,073,709,551,615 | ul |
float | 32-bit Single-precision floating point type | -3.402823e38 to 3.402823e38 | f |
double | 64-bit double-precision floating point type | -1.79769313486232e308 to 1.79769313486232e308 | d |
decimal | 128-bit decimal type for financial and monetary calculations | (+ or -)1.0 x 10e-28 to 7.9 x 10e28 |
m |
char | 16-bit single Unicode character | Any valid character, e.g. a,*, \x0058 (hex), or\u0058 (Unicode) | |
bool | 8-bit logical true/false value | True or False | |
object | Base type of all other types. | ||
string | A sequence of Unicode characters | ||
DateTime | Represents date and time | 0:00:00am 1/1/01 to 11:59:59pm 12/31/9999 |
위의 표에서 볼 수 있듯이 각 데이터 유형(문자열 및 객체 제외)에는 값 범위가 포함되어 있습니다. 값이 데이터 유형의 허용 범위를 벗어나면 컴파일러에서 오류가 발생합니다. 예를 들어 int 데이터 유형의 범위는 -2,147,483,648에서 2,147,483,647입니다. 따라서 이 범위에 없는 값을 할당하면 컴파일러에서 오류가 발생합니다.
// compile time error: Cannot implicitly convert type 'long' to 'int'.
int i = 21474836470;
부호 없는 정수, long, float, double 및 decimal 유형의 값은 각각 u,l,f,d 및 m이 접미사여야 합니다.
uint ui = 100u;
float fl = 10.2f;
long l = 45755452222222l;
ulong ul = 45755452222222ul;
double d = 11452222.555d;
decimal mon = 1000.15m;
Alias vs .Net Type
미리 정의된 데이터 유형은 해당 .NET 유형(CLR 클래스) 이름의 별칭입니다. 다음 표에는 미리 정의된 데이터 유형의 별칭과 관련 .NET 클래스 이름이 나열되어 있습니다.
Alias | .Net Type | Type |
byte | System.Byte | struct |
sbyte | System.SByte | struct |
int | System.Int32 | struct |
uint | System.UInt32 | struct |
short | System.Int16 | struct |
ushort | System.UInt16 | struct |
long | System.Int64 | struct |
ulong | System.UInt64 | struct |
float | System.Single | struct |
double | System.Double | struct |
char | System.Char | struct |
bool | System.Boolean | struct |
object | System.Object | Class |
string | System.String | Class |
decimal | System.Decimal | struct |
DateTime | System.DateTime | struct |
이는 변수를 정의하든 int 또는 Int32 를 정의하든 둘 다 동일함을 의미합니다.
int i = 345;
Int32 i = 345;// same as above
Default Value (기본값)
모든 데이터 유형에는 기본값이 있습니다. 숫자 유형은 0이고 부울은 false이며 char은 '\0'기본값입니다. the 를 사용하여 default(typename)데이터 유형의 기본값을 할당하거나 C# 7.1 이상에서는 default literal 을 사용 하세요.
int i = default(int); // 0
float f = default(float);// 0
decimal d = default(decimal);// 0
bool b = default(bool);// false
char c = default(char);// '\0'
// C# 7.1 onwards
int i = default; // 0
float f = default;// 0
decimal d = default;// 0
bool b = default;// false
char c = default;// '\0'
Conversion(전환)
특정 데이터 형식의 값은 C# 에서 자동으로 다른 데이터 형식으로 변환됩니다. 이를 암시적 변환이라고 합니다.
int i = 345;
float f = i;
Console.WriteLine(f); //output: 345
의 예에서 정수 변수의 값 i은 float 유형의 변수에 할당됩니다. f이 변환 연산은 C#에서 미리 정의되어 있기 때문입니다.
다음은 암시적 데이터 유형 변환 테이블입니다.
Implicit Conversion From |
To |
sbyte | short, int, long, float, double, decimal |
byte | short, ushort, int, uint, long, ulong, float, double, decimal |
short | int, long, float, double, or decimal |
ushort | int, uint, long, ulong, float, double, or decimal |
int | long, float, double, or decimal. |
uint | long, ulong, float, double, or decimal |
long | float, double, or decimal |
ulong | float, double, or decimal |
char | ushort, int, uint, long, ulong, float, double, or decimal |
float | Double |
int, uint, long 또는 ulong 에서 float 로 변환하고, long 또는 ulong에서 double로 변환하면 정밀도가 손실될 수 있습니다. 암시적으로 char 유형으로 변환된 데이터 유형이 없습니다.
그러나 모든 데이터 유형이 암시적으로 다른 데이터 유형으로 변환되는 것은 아닙니다. 예를 들어 int 유형은 암시적으로 uint 로 변환할 수 없습니다. 아래와 같이 명시적으로 지정해야 합니다.
public static void Main()
{
int i = 100;
uint u = (uint) i;
Console.Write(i);
}
위의 예에서 integer 의 i 는 괄호 안(uint)에 uint 를 지정하여 명시적으로 uint 로 변환됩니다. integer 를 uint 로 변환합니다.
https://www.tutorialsteacher.com/csharp/csharp-data-types