해당 문서는 인프런 [C#과 유니티로 만드는 MMORPG 게임 개발 시리즈] Part1: C# 기초 프로그래밍 입문을 듣고 정리한 필기 노트입니다.
int hp = 100;
bool isDead = (hp <= 0);
if (isDead)
{
Console.WriteLine("You are Dead!");
} else {
Console.WriteLine("You are alive!");
}
// else if ()
int choice = 0;
switch (choice)
{
case 0:
Console.WriteLine("가위입니다.");
break;
case 1:
Console.WriteLine("바위입니다.");
break;
case 2:
Console.WriteLine("보입니다.");
break;
case 3:
Console.WriteLine("치트키입니다.");
break;
default:
Console.WriteLine("다 실패했습니다.");
break;
}
const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 0;
switch (choice)
{
case SCISSORS:
Console.WriteLine("가위");
break;
case ROCK:
Console.WriteLine("바위");
break;
case PAPER:
Console.WriteLine("보");
break;
}
```csharp
int number = 25;
bool isPair = ((number % 2) == 0 ? true : false);
```
enum Choice
{
Rock = 1;
Paper = 2;
Scissors = 0
}
static void Main(string[] args)
{
switch (choice)
{
case (int)Choice.Scissors:
...
}
}
int count = 5;
while (count > 0) // 조건 확인 후 시작
{
Console.WriteLine("HelloWorld");
count--;
}
string answer;
do // 일단 한 번 실행 후
{
Console.WriteLine("잘생김? (y/n) : ");
answer = Console.ReadLine();
} while (answer != "y"); // 다시 돌아갈 때 조건 확인
// i = 0 일 때, i < 5 인지 확인 후 실행, i++
// 따라서 i++나 ++i나 상관없음
for (int i = 0; i < 5; i++)
{
Console.WriteLine("GO");
}
한정자 반환형식 이름(매개변수목록)
{
}
static void HelloWorld()
{
Console.WriteLine("Hello World");
}
static void Main(string[] args)
{
Program.Helloworld();
}
static void AddOne(int number)
{
number = number + 1;
}
static void Main(string[] args)
{
int a = 0;
Program.AddOne(a);
Console.WriteLine(a);
// Output : 0
}
static void AddOne(ref int number)
{
number = number + 1;
}
static void Main(string[] args)
{
int a = 0;
Program.AddOne(ref a);
Console.WriteLine(a);
// Output : 1
}
static void AddOne(ref int number)
{
number = number + 1;
}
static int AddOne2(int number)
{
return number = number + 1;
}
static void Main(string[] args)
{
int a = 0;
Program.AddOne(ref a);
Program.AddOne2(a); // 추천
Console.WriteLine(a);
// Output : 1
}
static void Divide(int a, int b, out int result1, out int result2)
{
result1 = a / b;
result2 = a % b;
}
static void Main(string[] args)
{
int num1 = 10;
int num2 = 3;
int result1;
int result2;
Divide(10, 3, out result1, out result2);
}
= 함수 이름의 재사용
static int Add(int a, int b)
{
return a + b;
}
static int Add(int a, int b, int c)
{
return a + b + c;
}
static void Add(int a, int b)
{
Console.WriteLine("Add int void");
}
static float Add(float a, float b)
{
return a + b;
}
static void Main(string[] args)
{
int ret = Program.Add(2, 3);
float ret2 = Program.Add(2.0f, 3.0f);
Console.WriteLine(ret);
}
static int Add(int a, int b, int c = 0, float d = 1.0f, double e = 3.0)
{
return a + b + c;
}
static void Main(string[] args)
{
int ret = Program.Add(2, 3, d:2.0f);
Console.WriteLine(ret);
}
=```