기본 문법은 타 언어들과 거의 비슷하다고 생각한다.
string aFriend = "Bill";
Console.WriteLine(aFriend);
===========================
출력
Bill
aFriend = "Maria";
Console.WriteLine(aFriend);
===========================
출력
Maria
Console.WriteLine("Hello " + aFriend);
===========================
출력
Hello Bill
Console.WriteLine($"Hello {aFriend}");
=====================================
출력
Hello Bill
string firstFriend = "Maria";
string secondFriend = "Sage";
Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");
=====================================================================
출력
My friends are Maria and Sage
Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");
Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");
출력
The name Maria has 5 letters.
The name Sage has 4 letters.
string greeting = " Hello World! ";
Console.WriteLine($"[{greeting}]");
=======================================================
출력
[ Hello World! ]
=======================================================
string trimmedGreeting = greeting.TrimStart();
Console.WriteLine($"[{trimmedGreeting}]");
=======================================================
출력
[Hello World! ]
=======================================================
trimmedGreeting = greeting.TrimEnd();
Console.WriteLine($"[{trimmedGreeting}]");
=======================================================
출력
[ Hello World!]
=======================================================
trimmedGreeting = greeting.Trim();
Console.WriteLine($"[{trimmedGreeting}]");
=======================================================
출력
[Hello World!]
=======================================================
string sayHello = "Hello World!";
Console.WriteLine(sayHello);
sayHello = sayHello.Replace("Hello", "Greetings");
Console.WriteLine(sayHello);
=====================================================
출력
Hello World!
Greetings World!
====================================================
Console.WriteLine(sayHello.ToUpper());
Console.WriteLine(sayHello.ToLower());
=====================================================
출력
GREETINGS WORLD!
greetings world!
string songLyrics = "You say goodbye, and I say hello";
Console.WriteLine(songLyrics.Contains("goodbye"));
Console.WriteLine(songLyrics.Contains("greetings"));
=====================================================
출력
True
False
int a = 18;
int b = 6;
int c = a + b;
Console.WriteLine(c);
int d = a - b;
Console.WriteLine(d);
int e = a * b;
Console.WriteLine(e);
int f = a / b;
Console.WriteLine(f);
=====================================================
출력
24
12
24
24
int a = 5;
int b = 4;
int c = 2;
int d = a + b * c;
Console.WriteLine(d);
=====================================================
출력
13
int a = 5;
int b = 4;
int c = 2;
int d = (a + b) * c;
Console.WriteLine(d);
=====================================================
출력
18
int a = 5;
int b = 4;
int c = 2;
int d = (a + b) - 6 * c + (12 * 4) / 3 + 12;
Console.WriteLine(d);
=====================================================
출력
25
int a = 7;
int b = 4;
int c = 3;
int d = (a + b) / c;
int e = (a + b) % c;
Console.WriteLine($"quotient: {d}");
Console.WriteLine($"remainder: {e}");
=====================================================
출력
quotient: 3
remainder: 2
int max = int.MaxValue;
int min = int.MinValue;
Console.WriteLine($"The range of integers is {min} to {max}");
=====================================================
출력
The range of integers is -2147483648 to 2147483647
int what = max + 3;
Console.WriteLine($"An example of overflow: {what}");
=====================================================
출력
(1,12): error CS0103: The name 'max' does not exist in the current context
double a = 5;
double b = 4;
double c = 2;
double d = (a + b) / c;
Console.WriteLine(d);
=====================================================
출력
4.5
double a = 19;
double b = 23;
double c = 8;
double d = (a + b) / c;
Console.WriteLine(d);
=====================================================
출력
5.25
double max = double.MaxValue;
double min = double.MinValue;
Console.WriteLine($"The range of double is {min} to {max}");
=====================================================
출력
The range of double is -1.79769313486232E+308 to 1.79769313486232E+308
double third = 1.0 / 3.0;
Console.WriteLine(third);
=====================================================
출력
0.333333333333333
decimal min = decimal.MinValue;
decimal max = decimal.MaxValue;
Console.WriteLine($"The range of the decimal type is {min} to {max}");
=====================================================
출력
The range of the decimal type is -79228162514264337593543950335 to 79228162514264337593543950335
double a = 1.0;
double b = 3.0;
Console.WriteLine(a / b);
decimal c = 1.0M;
decimal d = 3.0M;
Console.WriteLine(c / d);
=====================================================
출력
0.333333333333333
0.3333333333333333333333333333
int a = 5;
int b = 6;
if (a + b > 10)
Console.WriteLine("The answer is greater than 10.");
=====================================================
출력
The answer is greater than 10.
int a = 5;
int b = 3;
int c = 4;
if ((a + b + c > 10) && (a == b))
{
Console.WriteLine("The answer is greater than 10");
Console.WriteLine("And the first number is equal to the second");
}
else
{
Console.WriteLine("The answer is not greater than 10");
Console.WriteLine("Or the first number is not equal to the second");
}
=====================================================
출력
The answer is not greater than 10
Or the first number is not equal to the second
int counter = 0;
while (counter < 10)
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
}
=====================================================
출력
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
int counter = 0;
do
{
Console.WriteLine($"Hello World! The counter is {counter}");
counter++;
} while (counter < 10);
=====================================================
출력
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
for(int counter = 0; counter < 10; counter++)
{
Console.WriteLine($"Hello World! The counter is {counter}");
}
=====================================================
출력
Hello World! The counter is 0
Hello World! The counter is 1
Hello World! The counter is 2
Hello World! The counter is 3
Hello World! The counter is 4
Hello World! The counter is 5
Hello World! The counter is 6
Hello World! The counter is 7
Hello World! The counter is 8
Hello World! The counter is 9
for (int row = 1; row < 11; row++)
{
Console.WriteLine($"The row is {row}");
}
=====================================================
출력
The row is 1
The row is 2
The row is 3
The row is 4
The row is 5
The row is 6
The row is 7
The row is 8
The row is 9
The row is 10
for (char column = 'a'; column < 'k'; column++)
{
Console.WriteLine($"The column is {column}");
}
=====================================================
출력
The column is a
The column is b
The column is c
The column is d
The column is e
The column is f
The column is g
The column is h
The column is i
The column is j
for (int row = 1; row < 11; row++)
{
for (char column = 'a'; column < 'k'; column++)
{
Console.WriteLine($"The cell is ({row}, {column})");
}
}
=====================================================
출력
he cell is (1, a)
The cell is (1, b)
The cell is (1, c)
The cell is (1, d)
The cell is (1, e)
The cell is (1, f)
The cell is (1, g)
The cell is (1, h)
The cell is (1, i)
The cell is (1, j)
The cell is (2, a)
The cell is (2, b)
The cell is (2, c)
The cell is (2, d)
The cell is (2, e)
The cell is (2, f)
The cell is (2, g)
The cell is (2, h)
The cell is (2, i)
The cell is (2, j)
The cell is (3, a)
The cell is (3, b)
The cell is (3, c)
The cell is (3, d)
The cell is (3, e)
The cell is (3, f)
The cell is (3, g)
The cell is (3, h)
The cell is (3, i)
The cell is (3, j)
The cell is (4, a)
The cell is (4, b)
The cell is (4, c)
The cell is (4, d)
The cell is (4, e)
The cell is (4, f)
The cell is (4, g)
The cell is (4, h)
The cell is (4, i)
The cell is (4, j)
The cell is (5, a)
The cell is (5, b)
The cell is (5, c)
The cell is (5, d)
The cell is (5, e)
The cell is (5, f)
The cell is (5, g)
The cell is (5, h)
The cell is (5, i)
The cell is (5, j)
The cell is (6, a)
The cell is (6, b)
The cell is (6, c)
The cell is (6, d)
The cell is (6, e)
The cell is (6, f)
The cell is (6, g)
The cell is (6, h)
The cell is (6, i)
The cell is (6, j)
The cell is (7, a)
The cell is (7, b)
The cell is (7, c)
The cell is (7, d)
The cell is (7, e)
The cell is (7, f)
The cell is (7, g)
The cell is (7, h)
The cell is (7, i)
The cell is (7, j)
The cell is (8, a)
The cell is (8, b)
The cell is (8, c)
The cell is (8, d)
The cell is (8, e)
The cell is (8, f)
The cell is (8, g)
The cell is (8, h)
The cell is (8, i)
The cell is (8, j)
The cell is (9, a)
The cell is (9, b)
The cell is (9, c)
The cell is (9, d)
The cell is (9, e)
The cell is (9, f)
The cell is (9, g)
The cell is (9, h)
The cell is (9, i)
The cell is (9, j)
The cell is (10, a)
The cell is (10, b)
The cell is (10, c)
The cell is (10, d)
The cell is (10, e)
The cell is (10, f)
The cell is (10, g)
The cell is (10, h)
The cell is (10, i)
The cell is (10, j)