int num;
var anyType = 123; // var은 선언과 동시에 초기화가 진행되어야한다!!
Console.WriteLine(anyType);
num=256;
Console.WriteLine(num);
Console.WriteLine("\tHello"); // \t = tab
Console.WriteLine("Hello \t world");
Console.WriteLine("C:\\name\\alzio.c");
Console.WriteLine("She says, \"Good Morning\"");
Console.WriteLine(@"She says, 'Good Morning.'
Hi");
->
Hello
Hello world
C:\name\alzio.c
She says, "Good Morning"
She says, 'Good Morning.'
Hi
string info = " Main Information ";
Console.WriteLine($"*{info}*");
Console.WriteLine($"*{info.TrimStart()}*");
Console.WriteLine($"*{info.TrimEnd()}*");
Console.WriteLine($"*{info.Trim()}*");
->
* Main Information *
*Main Information *
* Main Information*
*Main Information*
string info = "\tMain Information\t";
Console.WriteLine($"*{info}*");
Console.WriteLine(info.Remove(5,7));
->
* Main Information *
Maination
string info = "\tHappy*Lovely Day\t";
Console.WriteLine(info);
Console.WriteLine(info.Replace("*", " "));
Console.WriteLine(info.Replace("\t", ""));
->
Happy*Lovely Day
Happy Lovely Day
Happy*Lovely Day
string greeting = "Hello";
Console.WriteLine(greeting.PadLeft(10));
//전체의 길이가 10이되고 왼쪽으로 공백을 채워넣겠다.
Console.WriteLine(greeting.PadRight(10));
//전체의 길이가 10이되고 오른쪽으로 공백을 채워넣겠다.
Console.WriteLine(greeting.PadLeft(10,'*'));
//전체의 길이가 10이되고 왼쪽으로 *을 채워넣겠다.
Console.WriteLine(greeting.PadRight(10,'*'));
//전체의 길이가 10이되고 오른쪽으로 *을 채워넣겠다.
->
Hello
Hello
*****Hello
Hello*****
string string1 = "ABCE";
Console.WriteLine(String.Compare(string1, "ABCD"));
Console.WriteLine(String.Compare(string1, "ABCE"));
Console.WriteLine(String.Compare(string1, "ABCF"));
->
1
0
-1
string string1 = "ABCE";
string string2 = "ABCD";
int result = string1.CompareTo(string2);
Console.WriteLine(result);
->
1
string string1 = "ABCE";
Console.WriteLine(string1.Equals("ABCD"));
string string1 = "ABCD";
string string2 = "ABCD";
Console.WriteLine(string1.Equals(string2));
->
False
True
string string1 = "Hello Alzio";
Console.WriteLine(string1.StartsWith("Hello"));
Console.WriteLine(string1.EndsWith("Alzip"));
->
True
False
//Contains
string greetings = "Hello Alzio. I Love C#";
Console.WriteLine(greetings.Contains("Alzio"));
Console.WriteLine(greetings.Contains("alzio"));
//IndexOf
Console.WriteLine(greetings.IndexOf('l'));
Console.WriteLine(greetings.LastIndexOf('l'));
Console.WriteLine(greetings.Length);
->
True
False
2
7
22
// Insert
string greetings = "I Love C#";
Console.WriteLine(greetings.Remove(1,5));
Console.WriteLine(greetings);
greetings = greetings.Remove(1, 5);
Console.WriteLine(greetings);
string love = " LOVE";
Console.WriteLine(greetings.Insert(1, love));
->
I C#
I Love C#
I C#
I LOVE C#
int a = 10;
int b = 5;
int c = a + +b;
string string1 = "hello ";
Console.WriteLine(string1+ a + b + string1);
Console.WriteLine(string1+ (a + b) + string1);
Console.WriteLine(a-b);
Console.WriteLine(a*b);
Console.WriteLine(a/b);
->
hello 105hello
hello 15hello
5
50
2
int quotient = 17 / 5;
int remain = 17 % 5;
Console.WriteLine("나누기 결과:" + remain);
->
나누기 결과:2
몫 결과:3
decimal div = 17 / 5m;
Console.WriteLine("나누기 결과:" + div);
double div2 = 17.0 / 5.0;
Console.WriteLine("나누기 결과:" + div2);
->
나누기 결과:3.4
나누기 결과:3.4
int op = 10 + 20 * 30;
Console.WriteLine(op);
->
610
int num1 = 10;
Console.WriteLine(num1++);
Console.WriteLine(num1);
->
10
11
int num = 1;
Console.WriteLine("1: "+ num++);
Console.WriteLine("2: "+ ++num);
Console.WriteLine("3: "+ num);
Console.WriteLine("4: "+ (num++));
Console.WriteLine("5: "+ num);
->
1: 1
2: 3
3: 3
4: 3
5: 4
//클래스 라이브러리 메서드
Console.WriteLine("Hello");
Random number = new Random(); // 객체인데 Random이라는 클래스
int random = number.Next(1, 10);
Console.WriteLine(random);
->
Hello
7
// Math 라이브러리
int result;
result = Math.Abs(-25);
Console.WriteLine(result);
result = Math.Max(10, -30);
Console.WriteLine(result);
result = Math.Min(10, -30);
Console.WriteLine(result);
Console.WriteLine(Math.PI);
->
25
10
-30
3.141592653589793
namespace MyLibrary
{
public class MyClass
{
public double Add(double i, double j)
{
return i + j;
}
public double Sub(double i, double j)
{
return i - j;
}
public double Mul(double i, double j)
{
return i * j;
}
public double Div(double i, double j)
{
return i / j;
}
}
}
using MyLibrary;
double i = 10.0;
double j = 5.0;
MyClass myclass = new MyClass();
Console.WriteLine(myclass.Add(i,j));
Console.WriteLine(myclass.Sub(i,j));
Console.WriteLine(myclass.Mul(i,j));
Console.WriteLine(myclass.Div(i,j));
->
15
5
50
2
int i = 10;
if(i>3)
{
Console.WriteLine("참입니다.");
Console.WriteLine(i);
}
else
{
Console.WriteLine("거짓입니다.");
}
->
참입니다.
10
Random rand = new Random();
int number = rand.Next(1, 101);
if (number >= 50)
{
if (number % 2 == 0)
{
Console.WriteLine($"짝수입니다 : {number}");
}
else
{
Console.WriteLine($"홀수입니다 : {number}");
}
}
else
{
Console.WriteLine($"50미만의 숫자({number})는 포함될 수 없습니다");
}
->
50미만의 숫자(27)는 포함될 수 없습니다
string color = "Black";
//Blue Yellow Green
if(color == "Red")
{
Console.WriteLine("빨간새입니다.");
}
else if (color == "Blue")
{
Console.WriteLine("파란색입니다.");
}
else if (color == "Yellow")
{
Console.WriteLine("노랑색입니다.");
}
else
{
Console.WriteLine("일치하는 색상이 없습니다.");
}
->
파란색입니다.
노랑색입니다.
//int[] numbers = new int[5] { 10, 20, 30, 40, 50 };
int[] numbers ={ 10, 20, 30, 40, 50 };
Console.WriteLine(numbers[0]);
Console.WriteLine(numbers[4]);
->
10
50
//int[,] numbers2 = new int[2, 3]
{
{ 10, 5, 30 },
{ 50, 20, 48}
};
코드를 입력하세요
int[,] numbers2 =
{
{ 10, 5, 30 },
{ 50, 20, 48}
};
Console.WriteLine($"{numbers2[0,0]} {numbers2[0,1]} {numbers2[0, 2]}");
Console.WriteLine($"{numbers2[1,0]} {numbers2[1,1]} {numbers2[1, 2]}");
->
10 5 30
50 20 48
string[][] emptyHouse =
{
new string[] { "101호", "103호" },
new string[] { "202호" },
new string[] { "301호", "303호", "304호"},
};
Console.WriteLine(emptyHouse[1][0]);
->
202호