728x90
반응형
01. 기본 데이터형
-
C#의 데이터형 object로부터 파생된 객체
System.Object == object
-
데이터형은 CTS에서 정의된 객체
* CTS란?
공용 형식(타입) 시스템, 닷넷 호환 언어가 지켜야 할 형식(type)의 표준 규격
- 정수형
bool (true/false) | System.Boolean | 1 byte |
char | System.Char | 2 byte |
byte (범위: 부호없는 0~255) | System.Byte | 1 byte |
sbyte (범위: -128~127) | System.SByte | 1 byte |
short (범위: -32768~32767) | System.Int16 | 2 byte |
ushort (범위: 0~65535) | System.UInt16 | 2 byte |
int (범위: -2147483648~2147483647) | System.Int32 | 4 byte |
uint (범위: 0~4,294,967,295) | System.UInt32 | 4 byte |
long | System.Int64 | 8 byte |
ulong | System.UInt64 | 8 byte |
* 외울필요는 없으나 메서드를 사용하는데 이해가 필요하다.
- 실수형
float | System.Single | 4 byte |
double | System.Double | 8 byte |
decimal | System.Decimal | 16 byte |
- 문자열형
string | System.String |
3-1 예제) [bool] 클래스의 정적 변수 bool형의 기본 값과 지역 bool 변수 값을 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1 // bool은 0과1의 개념이 아닌 true/false 개념
{
class Program
{
static bool BoolVar; // 기본이 false
static void Main(string[] args)
{
bool LocalBoolVar = true;
Console.WriteLine("{0} {1}", BoolVar, LocalBoolVar); // false true
}
}
}
3-2 예제) [char] 문자 상수 7을 int형으로 변환하고 1을 더한 결과를 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
char Mun = '7'; // 문자상수값 = 55
int Num = Mun; // 55
Num = Num + 1; // 55 + 1 = 56
Console.WriteLine("Mun = {0} 문자상수값 = {1} 유니코드문자 = {2}",
(int)Mun, Num, (char)Num);
// (int)Mun: 55 Num: 56 (char)Num): 8
Mun = (char)Num;
Console.WriteLine(Mun); // 8
}
}
}
3-3 예제) [byte / sbyte] byte형 두값을 더하고 그 값을 int형 변수에 대입하여 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int Result;
// ex) byte x; x =1; 두 가지 선언을 해도 두가지 다 쓸수 있다.
byte x = 1, y = 2;
Result = x + y;
Console.WriteLine(Result); // 3 출력
}
}
}
3-4 예제) [short / ushort] short 형의 유효 범위 값을 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("{0} ~ {1}", short.MinValue, short.MaxValue);
// -32768 ~ 32767
}
}
}
// public const short MinValue
// public const short MaxValue
3-5 예제1) [string] 다음과 같이 두 문자열을 합친 후에 합친 문자열과 다섯 번째 문자를 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
string str1 = "Hello ";
string str2 = "World!";
string str3 = str1 + str2;
string str4 = "program" + "ming";
Console.WriteLine("{0} {1}", str3, str4);// programming
Console.WriteLine(str3[4]); // o
if (str3 == "Hello World!")
{
Console.WriteLine("같음"); //
}
else {
Console.WriteLine("다름");
}
}
}
}
3-5 예제2) 문자열의 문자 개수를 Length로 확인
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
string str1 = "Hello World!"; // 12개의 문자
Console.WriteLine("문자 개수: {0}", str1.Length);
// .length : 문자개수출력
}
}
}
3-5 예제3) 백스페이스가 있는 문자열을 출력
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication7
{
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);
// 출력값: C:\temp\test.txt // C:\temp\test.txt
}
}
}
본 글은 C#으로 살아남기 _나우캠퍼스를 보고 정리하였습니다.
728x90
반응형
'C# > C#으로 살아남기_나우캠퍼스_이태성강사' 카테고리의 다른 글
[C# 언어 3강] 데이터형 (4/5) 사용자 지정형 (0) | 2021.02.23 |
---|---|
[C# 언어 3강] 데이터형 (3/5) 표준입력 (0) | 2021.02.22 |
[C# 언어 3강] 데이터형 (2/5) var, nullable 형, 변환1 (0) | 2021.02.20 |
[C# 언어 2강] C# 프로그래밍의 기본 구조(2/2) 표준출력 (0) | 2021.02.18 |
[C# 언어 2강] C# 프로그래밍의 기본 구조(1/2) (0) | 2021.02.18 |