C# Data Type 1 - 값형식에 대하여 공부해 보겠습니다.
Data Type
C#에서 지원하는 데이터 형식
구분 |
형식 |
크기(Bit) |
범위 |
정수- signed |
sbyte |
8 |
-128 ~ 127 |
short |
16 |
-32,768 ~ 32,767 |
|
int |
32 |
-2,147,483,648 ~ 2,147,483,647 |
|
long |
64 |
-9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
|
정수- unsigned |
byte |
8 |
0 ~ 255 |
ushort |
16 |
0 ~ 65,535 |
|
uint |
32 |
0 ~ 4,294,967,295 |
|
ulong |
64 |
0 ~ 18,446,744,073,709,551,615 |
|
실수 |
float |
32 |
±1.5e-45 ~ ±3.4e38 |
double |
64 |
±5.0e-324 ~ ±1.7e308 |
|
decimal |
128 |
(-7.9 x 1028 - 7.9 x 1028) / (100 - 28) |
|
문자 |
char |
16 (Unicode) |
U + 0000 ~ U + ffff |
Boolean |
bool |
1 |
true, false |
Data Type 실습 - 1. 일반 형식
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _160815_01_Test { class Program { static void Main(string[] args) { /* int형 실습 */ int number1 = 123; System.Int32 number2 = 1234; /* .NET Framework식 int표현(똑같음) */ Console.WriteLine("number1: {0}", number1); Console.WriteLine("number2: {0}", number2); /* double형 실습 */ double number3 = 123D; double number4 = 12; Console.WriteLine("number3: {0}", number3); Console.WriteLine("number4: {0}", number4); } } } | cs |
실습 코드이며, 설명은 주석으로 달았습니다
실행 화면입니다.
number2는 .NET Framework식 선언이며 C#에도 사용가능함을 확인할 수 있습니다.
number3은 실수형 double로 선언해서 D가 출력되지 않았음을 확인할 수 있습니다.
Data Type 실습 - 2. 열거형
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 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _160815_01_DataType_Enumeration { /* 열거형 선언 */ public enum Country { Korea, China, Japan } /* Korea = 0, China = 1, Japan = 2 으로 자동설정*/ class Program { static void Main(string[] args) { Country location1 = Country.Korea; /* Country안에 Korea를 location에 저장합니다 */ Country location2 = (Country)1; /* emum Country의 1번은 China */ int location3 = (int)location1; /* 정수형으로 저장 */ Console.WriteLine("location1: {0}", location1); Console.WriteLine("location2: {0}", location2); Console.WriteLine("location3: {0}", location3); /* 정수형을 출력하게 됩니다. */ Console.WriteLine("location(Country): {0}", (Country)location3); /* 정수형 location3을 가져오는데 3번째 콘솔과 다른점은 (Country)를 넣어서 Country를 사용하여 location3을 불러온다는 점입니다 */ | cs |
열거형 실습 코드와 설명입니다.
실행 화면입니다.
유심히 봐야할 구간은 3번째와 4번째 라인입니다.
코드상에선 location3이냐 (Country)location3)이냐 차이입니다.
정수형으로 선언했지만 정수형으로 받아와 숫자를 출력하느냐 문자를 출력하느냐 차이입니다.
Data Type 실습 - 3. Nullable형식
실습은 2가지경우를 예로 들겠습니다. 숫자를 입력하여 값을 판별할 수 있는 경우도 있지만 기본개념을 잡는 경우라서 일반숫자, null값 2가지를 수동으로 바꿔서 확인하겠습니다
일반 숫자일 경우
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 29 30 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _160815_01_Nullable { class Program { static void Main(string[] args) { /* null이 선언 가능하도록 '?'를 붙여줌 */ int? number = null; number = 10; /* number안에 10을 넣어줌 */ /* HasValue는 null일경우 false, null이 아니면 true */ if (number.HasValue) { /* Value는 HasValue가 true여야 가지고 있는 값을 반환 */ Console.WriteLine(number.Value); /* 즉 null이 아니여야 값을 반환한다는 뜻임 */ } else { Console.WriteLine("값이 null입니다."); } } } } | cs |
number에 10을 넣어줬기 때문에 10을 출력합니다.
null값을 넣은경우
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 29 30 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _160815_01_Nullable { class Program { static void Main(string[] args) { /* null이 선언 가능하도록 '?'를 붙여줌 */ int? number = null; number = null; /* number안에 null을 넣어줌 */ /* HasValue는 null일경우 false, null이 아니면 true */ if (number.HasValue) { /* Value는 HasValue가 true여야 가지고 있는 값을 반환 */ Console.WriteLine(number.Value); /* 즉 null이 아니여야 값을 반환한다는 뜻임 */ } else { Console.WriteLine("값이 null입니다."); } } } } | cs |
코드와 설명입니다
유심히 보셔야 할 점은 number = null; 입니다
number에 숫자대신 null을 넣어줬습니다
null값이 들어왔기 때문에 null이라고 출력해줍니다.
?를 지우고 null값을 넣었을 경우
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 29 | using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace _160815_01_Nullable { class Program { static void Main(string[] args) { int number = null; /* ?를 */ number = null; /* HasValue는 null일경우 false, null이 아니면 true */ if (number.HasValue) { /* Value는 HasValue가 true여야 가지고 있는 값을 반환 */ Console.WriteLine(number.Value); /* 즉 null이 아니여야 값을 반환한다는 뜻임 */ } else { Console.WriteLine("값이 null입니다."); } } } } | cs |
number에서 ?만 지웠습니다
실행해보니 오류가 났습니다.
즉 정수형으로 선언할때 null값을 사용하고 싶으면 변수명 옆에 '?'를 반드시 붙여야 함을 확인할 수 있습니다.
'프로그래밍 > C#' 카테고리의 다른 글
[C#] C#데이터 타입 - 참조형식, 변수, 상수 (0) | 2016.08.15 |
---|---|
[C#] C#의 기초개념과 예제로 보는 텍스트 출력 (0) | 2016.08.15 |