728x90
반응형
[예제1]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
// 클래스 기반
class Program
{
// 진입점(Entroy Point)
// Main도 함수인데 누군가가 호출을 해야한다.
// Main이 클래스 소속이다. 멤버 함수이다.
static void Main(string[] args)
{
Console.WriteLine("Hello WOrld!");
}
}
}
[예제2]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
// 클래스 기반
class Program
{
static void Main(string[] args)
{
// 클래스 기반.
// Console은 클래스이다.
Console.Write("1"); // 줄바꿈이 없다.
Console.WriteLine("2"); // 캐리지 리턴이 있다. (CR) : 개행
Console.Write("3"); // 줄바꿈이 없다.
Console.WriteLine(); // 개행만 일어난다.
Console.WriteLine("4"); // 캐리지 리턴이 있다. (CR) : 개행
Console.WriteLine();
}
}
}
[예제2]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// %d == {0} 동격
Console.WriteLine("독{0}수{1}리", 10, 20);
}
}
}
[예제3]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("독{0:d4}수{1:D4}리", 10, 20);
}
}
}
[예제4]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// 16진수
Console.WriteLine("독{0:x}수{1:x}리", 10, 20);
}
}
}
[이전 코드를 지워버린다]
Console.Clear();
[type]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
byte a01; // 0~255
Console.WriteLine(byte.MinValue);
Console.WriteLine(byte.MaxValue);
Console.WriteLine(byte.MinValue + " " + byte.MaxValue);
sbyte a02;
Console.WriteLine(sbyte.MinValue + " " + sbyte.MaxValue);
}
}
}
[Size를 알고 싶으면 아래와 같은 코드를 사용하면 된다]
char a09; // 2byte
Console.WriteLine(sizeof(char));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
decimal a13 = 3.14m;
Console.WriteLine(sizeof(float));
Console.WriteLine(sizeof(double));
Console.WriteLine(sizeof(decimal));
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int a = 10;
Console.WriteLine(a);
Console.WriteLine("a");
Console.WriteLine("{a}");
Console.WriteLine($"{a}"); // 자주 나온다.
Console.WriteLine($"a = {a}");
}
}
}
[문자출력]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
char a = '호'; // 문자
string b = "랑이";
Console.WriteLine(a);
Console.WriteLine(b);
}
}
}
[object : 모든 타입을 다 받아 준다]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
object a = 10;
object b = 3.14f;
object c = 3.14;
object d = true;
object e = '호';
object f = "랑이";
Console.WriteLine(a.GetType());
Console.WriteLine(b.GetType());
Console.WriteLine(c.GetType());
Console.WriteLine(d.GetType());
Console.WriteLine(e.GetType());
Console.WriteLine(f.GetType());
}
}
}
[문자열을 숫자로 변환 (통신이 일어날때)]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string a = "1234";
int b = int.Parse(a);
Console.WriteLine( a + 10); // 문자열 + 숫자 = 문자열
Console.WriteLine( b + 10);
}
}
}
[숫자를 문자열로 변환]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
int c = 5678;
string d = c.ToString();
Console.WriteLine(c + 10);
Console.WriteLine(d + 10);
string e = "" + c; // 자주나온다
Console.WriteLine(e + 10);
}
}
}
[초기화가 이루어져야 한다.]
const int a = 10;
C#에서 #define 문법은 없다. (const가 대신 사용된다)
enoum 문법은 있다.
enum : void Main 위에서 선언한다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
enum Color // 변수명
{
RED,
GREEN = 100,
BLUE
};
static void Main(string[] args)
{
Console.WriteLine(Color.GREEN);
Console.WriteLine((int)Color.GREEN);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
enum Color // 변수명
{
RED,
GREEN = 100,
BLUE
};
static void f1(Color c)
{
}
static void Main(string[] args)
{
f1(Color.GREEN);
Console.WriteLine(Color.GREEN);
Console.WriteLine((int)Color.GREEN);
Color c = Color.GREEN;
Console.WriteLine(c);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string a = null;
Console.WriteLine(a == null);
int? b = null;
Console.WriteLine(b == null);
}
}
}
728x90
반응형
'Education > Edu | .net' 카테고리의 다른 글
# 22.2) [C#] 문법1 (0) | 2021.02.16 |
---|---|
# 22.1) [C#] 시작하기2 (0) | 2021.02.16 |
# 20) [Oracle] install 및 setting 방법 (0) | 2021.02.03 |
# 19.3) [Windows Network Programming] 시작하기6 (0) | 2021.01.29 |
# 19.2) [Windows Network Programming] 시작하기5 (0) | 2021.01.29 |