VioletaBabel

1일차 본문

기본개념/C#
1일차
Beabletoet 2018. 1. 15. 12:28

형변환

1
2
3
4
5
6
7
int i;
double r = 20;
string t;
 
= Convert.ToInt32(10.1); // i = (int)10.1;
= Convert.ToString(r); // t = r.ToString();
= Convert.ToInt32(t); // i = int.Parse(t);
cs

--

출력문

1
2
Console.Write("Hello, ");
Console.WriteLine("World!");
cs


커서 이동

1
Console.SetCursorPosition(1010); // 커서를 10,10 위치로 이동.
cs


--

입력문


1
2
3
int i = Console.Read(); // Console.Read()는 한 글자만을 입력받아 int형으로 저장
char s = (char)i; // 그렇기 때문에 출력할 때 형변환이 필요
Console.WriteLine(s);
cs

1
2
string s = Console.ReadLine(); // 엔터키를 누를 때까지 입력된 것을 string형으로 저장
Console.WriteLine(s);
cs



1
2
3
ConsoleKeyInfo k = Console.ReadKey(); 
// 엔터없이 입력된 한 글자를 ConsoleKeyInfo의 Key 속성에 저장
Console.WriteLine(k.Key);
cs



--

배열


1
int[] ar = new int[5]; //배열 선언법

cs


1
2
int[,] ar = new int[25];//2차원 배열
int[,,] ar2 = new int[325];//3차원 배열
cs


--

함수


1
2
3
4
5
6
7
8
9
10
11
class Program
    {
        static int plus(int a, int b)
        {
            return a + b;
        }
        static void Main(string[] args)
        {
            Console.WriteLine(plus(1020));
        }
    } //덧셈 함수 예제 olored by Color Scripter

cs


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
static void swap(ref int a, ref int b)
        {
            int c = a;
            a = b;
            b = c;
        }
        static void Main(string[] args)
        {
            int a = 10, b = 20;
            Console.WriteLine(a + " " + b);
            swap(ref a, ref b);
            Console.WriteLine(a + " " + b);
        } 
// ref를 붙이면 레퍼런스 변수가 되어
// 리턴 없이도 함수 밖까지 값 변경 가능
cs


1
2
3
4
5
6
7
8
9
10
11
12
13
static void calc(int a, int b, out int p, out int m)
{
    p = a + b;
    m = a - b;
}
static void Main(string[] args)
{
    int a = 3, b = 1, c, d;
    calc(a, b, out c, out d);
    Console.Write("{0} {1} {2} {3}", a, b, c, d);
}
//out은 출력 전용 매개 변수로, 초기화를 하지 않아도 된다는 장점이 있다.
//ref와 비슷해보이나 세세하게  다르다
cs

--

랜덤


1
2
3
4
5
6
Random r = new Random();
for (int i = 0, num; i++ < 10;)
{
    num = r.Next(14); //1~3까지의 수 랜덤
    Console.WriteLine(num);
}
cs


--

실수


1
2
3
4
float f = 3.141592653589793238462643383279f;
double d = 3.141592653589793238462643383279;
decimal m = 3.141592653589793238462643383279m;
Console.WriteLine(f + "\n" + d + "\n" + m);
cs

decimal은 부동소수점 방식과 다른 방식으로 다루며, 정밀도가 높다.(뒤에 m 붙여야함)

float은 뒤에 f를 붙여야 한다.


--

object

object는 모든 데이터를 다 담을 수 있음. 모든 데이터 형식은 object 형식을 상속 받기 때문.

1
2
3
4
object a = 20;
Console.WriteLine(a);
= "hi";
Console.WriteLine(a);
cs


--

nullable 형식

null 상태로 있을 수 있는 변수

1
2
3
4
 //물음표를 붙이면 null을 가질 수 있으나
int? a = null//꼭 바로 이니셜라이즈 해줘야함
Console.Write(a.HasValue);
//HasValue는 값을 가지고 있는지 아닌지를 알려줌
cs


--

var

컴파일러가 자동으로 변수의 자료형을 지정. 역시 nullable처럼 바로 이니셜라이즈 해줘야 함

1
2
3
var a = 1;
var b = "A";
Console.WriteLine("{0} {1}", a, b);
cs


--

foreach문


1
2
3
int[] a = new int[] { 01234 };
foreach(int i in a)
    Console.WriteLine(i);
cs



'기본개념 > C#' 카테고리의 다른 글

4일차  (0) 2018.01.21
3일차  (0) 2018.01.19
2일차  (0) 2018.01.18
C# 코딩의 기술 : 똑똑하게 코딩하는 법 - 기본편  (0) 2017.06.25
c#  (0) 2017.06.18
Comments