개발자 성장일지

제 5강 C#의 지식창고 MSDN 본문

인프런/C# 프로그래밍 입문

제 5강 C#의 지식창고 MSDN

hdigimon 2023. 11. 8. 14:09

1. MSDN

 

C# docs - get started, tutorials, reference.

Learn C# programming - for beginning developers, developers new to C#, and experienced C# / .NET developers

learn.microsoft.com

2. 배열

  • 여러 값을 저장할 수 있는 자료 구조

 

코드

더보기
class play
{
    public static void Main(string[] args)
    {
        // 배열을 초기화 하는 첫 번째 방법
        int[] array1 = new int[3];
        array1[0] = 1;
        array1[1] = 2;
        array1[2] = 3;

        // 배열을 초기화하는 두 번째 방법
        int[] array2 = new int[3] {10, 11, 12};

        // 배열을 초기화 하는 세 번째 방법
        int[] array3 = { 4, 5, 6 };

/*      배열 내의 정보를 확인 할 수 없음
        Console.WriteLine(array1);
        Console.WriteLine(array2);
        Console.WriteLine(array3);*/

        for(int i = 0; i < array2.Length; i++)
        {
            Console.WriteLine(array2[i]);
        }

        // foreach 문을 통해 배열에 담긴 값 출력
        foreach(int i in array3)
        {
            Console.WriteLine(i);
        }
    }
}

3. 컬렉션

  • 여러 데이터 형을 포함해 입력과 출력, 데이터 처리를 수행할 수 있는 자료 구조

 

  • ArrayList 클래스

코드

더보기
using System.Collections;

class MainClass
{
    public static void Main(string[] args)
    {
        ArrayList al = new ArrayList();

        // add  메소드를 통해 아이템 추가
        al.Add(1);
        al.Add("Hello");
        al.Add(3.3);
        al.Add(true);

        foreach(var item in al)
        {
            Console.WriteLine(item);
        }
        Console.WriteLine();

        // Remove 메소드를 통해 아이템 삭제
        al.Remove("Hello");

        foreach(var item in al)
        {
            Console.WriteLine(item);
        }
    }
}

 

4.  Queue 클래스

  • 선입선출 - 먼저 입력된 값이 먼저 출력 된다.

코드

더보기
using System.Collections;

class MyClass
{
    public static void Main(string[] args)
    {
        Queue qu = new Queue();

        // Equeue 메소드를 통해 아이템 추가
        qu.Enqueue(1);
        qu.Enqueue(2);
        qu.Enqueue(3);

        // Dequeue 메소드를 통해 아이템을 제거
        while(qu.Count > 0)
        {
            Console.WriteLine(qu.Dequeue());
        }
    }
}

5.  Stack 클래스

코드

더보기
using System.Collections;

class MainClass
{
    public static void Main(string[] args)
    {
        Stack st = new Stack();

        // push 메소드를 통해 Stack에 아이템 추가
        st.Push(1);
        st.Push(2);
        st.Push(3);

        //Console.WriteLine(st.ToString());

        // Pop 메소드를 통해 아이템을 제거
        while (st.Count > 0)
        {
            Console.WriteLine(st.Pop());
        }
    }
}

6.  Hashtable 클래스

코드

더보기
using System.Collections;

public class MainClass
{
    public static void Main(string[] args)
    {
        Hashtable ht = new Hashtable();

        ht["apple"] = "사과";
        ht["banana"] = "바나나";
        ht["orange"] = "오렌지";

        Console.WriteLine(ht["apple"]);
        Console.WriteLine(ht["banana"]);
        Console.WriteLine(ht["orange"]);
    }
}

7.  예외 처리하기

  • 정상적으로 처리되지 않고, 예상하지 못한 결과를 도출하는 것을 방지하는 기능

코드

더보기
class MainClass
{
    public static void Main(string[] args)
    {
        Console.WriteLine("나눌 숫자를 입력하세요");
        int divider = int.Parse(Console.ReadLine());




        // try catch (Exception)
        try {
            Console.WriteLine(10/divider); 
        } 
        catch(Exception e) {
            Console.WriteLine("예외 : " + e.Message);
        }

        //Console.WriteLine(10/divider);
    }
}