728x90
C#기본 자료형
자료형 | .NET 데이타 타입 | 크기 (바이트) | 범위 |
sbyte | System.SByte | 1 | -128 ~ 127 |
byte | System.Byte | 1 | 0 ~ 255 |
short | System.Int16 | 2 | -32,768 ~ 32,767 |
ushort | System.UInt16 | 2 | 0 ~ 65,535 |
int | System.Int32 | 4 | -2,147,483,648 ~ 2,147,483,647 |
uint | System.UInt32 | 4 | 0 ~ 4,294,967,295 |
long | System.Int64 | 8 | -9,223,372,036,854,775,808 ~ 9,223,372,036,854,775,807 |
ulong | System.UInt64 | 8 | 0 ~ 18,446,744,073,709,551,615 |
float | System.Single | 4 | ±1.5 × 10^-45 ~ ±3.4 × 10^38 |
double | System.Double | 8 | ±5.0 × 10^-324 ~ ±1.7 × 10^308 |
decimal | System.Decimal | 16 | ±1.0 × 10^-28 ~ ±7.9 × 10^28 |
char | System.Char | 2 | 유니코드 문자 |
string | System.String | 유니코드 문자열 | |
bool | System.Boolean | 1 | true 또는 false |
변수 선언 , 초기화
// 변수 선언
int num;
// 변수 여러개 선언
int num1, num2, num3;
변수 초기화
일반
int num; // 변수 선언
num = 10; // 변수 초기화
int num0 = 10;
// 여러개
int num1, num2, num3 = 10; // (X) 여러개를 같은 값으로 초기화 할 때 선언은 따로
num1 = num2 = num3 = 10;
방법 1. 각각 명시적으로 초기화
int num1 = 10, num2 = 20, num3 = 30;
방법 2. 배열 사용
int[] nums = new int[] { 10, 20, 30 };
int num1 = nums[0];
int num2 = nums[1];
int num3 = nums[2];
또는:
var nums = new[] { 10, 20, 30 };
방법 3. 튜플 활용 (C# 7.0 이상)
(int num1, int num2, int num3) = (10, 20, 30);
변수명
키워드 (Keywords)
C#에서 이미 예약된 단어들이 있기 때문에 변수, 메소드, 클래스 등의 이름으로 사용할 수 없는 단어.
abstract as base bool break byte case catch
char checked class const continue decimal
default delegate do double else enum event
explicit extern false finally fixed float for
foreach goto if implicit in int interface
internal is lock long namespace new null
object operator out override params private
protected public readonly ref return sbyte
sealed short sizeof stackalloc static string
struct switch this throw true try typeof uint
ulong unchecked unsafe ushort using virtual
void volatile while
식별자 (Identifiers)
변수, 메서드, 클래스, 인터페이스 등에 사용되는 이름. 이 이름은 키워드와 동일하게 사용할 수 없다.
1. 첫 문자는 알파벳, 언더스코어(_)가 올 수 있다.
2. 두번째 문자부터는 알파벳, 언더스코어, 숫자가 올 수 있다.
3. 대소문자를 구분한다.
4. 키워드와 같은 이름으로 사용할 수 없다.
// 좋은 예시
int playerScore;
string playerName;
float itemPrice;
// 나쁜 예시 (중요 의미 있는 변수명 짓기)
int x1; // 변수명이 의미를 알기 어려움
string a; // 변수명이 명확하지 않음
// 오류 예시
int 1stNumber; // 변수명은 숫자로 시작할 수 없음
string my-name; // 변수명에 하이픈(-)을 사용할 수 없음
float total$; // 변수명에 특수문자($)를 사용할 수 없음
'C# > C# TIP' 카테고리의 다른 글
C# 코드 컨벤션(Code convention) (0) | 2025.05.21 |
---|---|
주석 // /**/ /// (0) | 2025.02.09 |
.net 터미널 명령어 (0) | 2025.02.09 |