728x90
01. Hello World 로 기본 코드 구조 확인
✔️ 우리의 첫 코드였던 “Hello World”를 확인해보자
// HelloWorld.cs
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
- using System;
→ Console 클래스 등 기본 API 사용을 위한 네임스페이스 등록 - namespace HelloWorld
→ 클래스·타입을 구분하는 컨테이너 - class Program
→ C# 프로그램 단위(클래스) 선언 - static void Main(...)
→ 프로그램 진입점(Entry Point) - Console.WriteLine(...)
→ 콘솔에 문자열 출력 후 줄 바꿈 - 모든 C# 문장(statement)은 세미콜론(;)으로 끝납니다.
02. 출력 방법
2-1. Console.WriteLine
- 출력을 하고 줄 바꿈
Console.WriteLine("안녕하세요!");
Console.WriteLine(2025);
Console.WriteLine(3 + 4);
2-2. Console.Write
- 출력 후 줄 바꿈 하지 않음
Console.Write("첫 번째 문장 ");
Console.Write("두 번째 문장");
Console.WriteLine(" 세 번째 문장"); // 이어서 출력 후 줄 바꿈
2-3. 이스케이프 시퀀스 (Escape Sequence)
- 특별문자 삽입
- \n (줄바꿈), \t (탭), \", \\ 등
Console.WriteLine("C# 학습\n시작!");
Console.WriteLine("경로: C:\\Projects\\Hello");
Console.WriteLine("탭\t구분");
이스케이프 시퀀스 | 설명 |
\' | 작은따옴표 (') 삽입 |
\" | 큰따옴표 (") 삽입 |
\\ | 역슬래시 (\) 삽입 |
\n | 새 줄 (줄바꿈) |
\r | 현재 줄 맨 앞으로 이동 |
\t | 탭 |
\b | 백스페이스 |
Console.WriteLine("Hello\nWorld");
// Hello
// World
Console.WriteLine("Name\tAge");
Console.WriteLine("Kero\t30");
Console.WriteLine("Young\t25");
// Name Age
// Kero 30
// Young 25
Console.WriteLine("We learn \"C# Programming\"");
// We learn "C# Programming"
Console.WriteLine("He said, \'Hello\' to me.");
// He said, 'Hello' to me.
Console.WriteLine("C:\\MyDocuments\\Project\\");
// C:\MyDocuments\Project\
03. 주석
- 한 줄 주석: // (해당 줄 끝까지)
- 여러 줄 주석: /* ... */
04. 자동 완성 기능과 보조 기능
- Tab 자동 완성: 클래스/메서드/변수 이름 일부 입력 후 Tab
- Console. 입력 후 Tab → WriteLine 완성
- Ctrl + Space (IntelliSense): 메서드 정보 및 예제 확인
- 코드 스니펫:
- for + Tab+Tab → 기본 for문 템플릿 생성
- 리팩토링 지원: 변수명 변경, 메서드 추출 등
단축키 확인하기
도구 > 사용자지정
05. 변수 & 데이터 타입
- 변수 선언 예시
int age = 25;
string name = "Alice";
double pi = 3.14;
bool isActive = true;
- 주요 기본 타입
- 정수: int, long
- 실수: float, double, decimal
- 문자열: string
- 불리언: bool
- 형 변환
int x = 10;
double y = x; // 암묵적 변환
string s = x.ToString(); // 명시적 변환
06. 연산자
종류 | 예시 |
산술 | +, -, *, /, % |
비교 | ==, !=, >, <, >=, <= |
논리 | &&, ` |
할당 | =, +=, -= |
int a = 5, b = 3;
Console.WriteLine(a + b); // 8
Console.WriteLine(a > b); // True
Console.WriteLine(!(a == b)); // True
07. 제어문
7-1. 조건문
int score = 85;
if (score >= 90)
Console.WriteLine("A 학점");
else if (score >= 80)
Console.WriteLine("B 학점");
else
Console.WriteLine("C 이하");
7-2. 반복문
for
for (int i = 0; i < 5; i++)
Console.WriteLine(i);
while
int i = 0;
while (i < 5)
{
Console.WriteLine(i);
i++;
}
foreach (배열·리스트 순회)
string[] fruits = { "사과", "바나나", "체리" };
foreach (var fruit in fruits)
Console.WriteLine(fruit);
'C# > C# 문법' 카테고리의 다른 글
c# 배열과 컬렉션 (0) | 2025.06.13 |
---|---|
C# 실습 예제 - (0) | 2025.05.24 |
C#에서 자주 사용되는 코드 (1) | 2025.05.22 |
C# 환경설정, 프로젝트 생성 (2) | 2025.05.21 |