오늘의 C# 공부 : stack, 일반화, 예외처리들

////Stack

//using System;
//using System.Collections.Generic;

//namespace UsingGenericstack
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Stack stack = new Stack();

//            stack.Push(1);
//            stack.Push(2);
//            stack.Push(3);
//            stack.Push(4);
//            stack.Push(5);

//            while (stack.Count > 0)
//                Console.WriteLine(stack.Pop());
//        }
//    }
//}

////Dictionary <Tkey, TValue>

//using System;
//using System.Collections.Generic;

//namespace UsingDictionary
//{
//    class MainApp
//    {
//        static void Main(string[] args)
//        {
//            Dictionary<string, string> dic = new Dictionary<string, string>();
//            dic[“하나”] = “one”;
//            dic[“두울”] = “two”;
//            dic[“세엣”] = “three”;
//            dic[“네엣”] = “four”;
//            dic[“다섯”] = “five”;

//            Console.WriteLine(dic[“하나”]);
//            Console.WriteLine(dic[“두울”]);
//            Console.WriteLine(dic[“세엣”]);
//            Console.WriteLine(dic[“네엣”]);
//            Console.WriteLine(dic[“다섯”]);
//        }
//    }
//}

////foreach를 사용할 수 있는 일반화 클래스
//using System;
//using System.Collections;
//using System.Collections.Generic;

//namespace EnumerableGeneric
//{
//    class MyList : IEnumerable, IEnumerator
//    {
//        private T[] array;
//        int position = -1;

//        public MyList()
//        {
//            array = new T[3];
//        }

//        public T this[int index]
//        {
//            get
//            {
//                return array[index];
//            }

//            set
//            {
//                if (index >= array.Length)
//                {
//                    Array.Resize(ref array,index + 1);
//                    Console.WriteLine(“Array Resized :{0}",array.Length);
//                }

//                array[index] = value;
//            }
//        }

//        public int Length
//        {
//            get { return array.Length; }
//        }

//        public IEnumerator GetEnumerator()
//        {
//            for (int i = 0; i < array.Length; i++)
//            {
//                yield return (array[i]);
//            }
//        }

//        IEnumerator IEnumerable.GetEnumerator()
//        {
//            for (int i = 0; i < array.Length; i++)
//            {
//                yield return (array[i]);
//            }
//        }

//        public T current
//        {
//            get { return array[position]; }
//        }

//        object IEnumerator.Current
//        {
//            get { return array[position]; }
//        }

//        public bool MoveNext()
//        {
//            if (position == array.Length - 1)
//            {
//                Reset();
//                return false;
//            }

//            position++;
//            return (position < array.Length);
//        }

//        public void Reset()
//        {
//            position = -1;
//        }

//        public void Dispose()
//        {
//        }
//    }

//    class jpcorp
//    {
//        static void Main(string[] args)
//        {
//            MyList str_list = new MyList();
//            str_list[0] = “abc”;
//            str_list[1] = “def”;
//            str_list[2] = “ghi”;
//            str_list[3] = “jkl”;
//            str_list[4] = “mno”;

//            foreach (string str in str_list)
//                Console.WriteLine(str);

//        }
//    }
//}

////예외처리

//using System;
//namespace KillingProgram
//{
//    class jpcorp
//    {
//        static void Main(string[] args)
//        {
//            int[] arr = { 1, 2, 3 };

//            for (int i = 0; i < 5; i++)
//            {
//                Console.WriteLine(arr[i]);
//            }

//            Console.WriteLine(“종료”);
//        }
//    }
//}

////예외처리 실습
//using System;

//namespace TryCatch
//{
//    class jpcorp
//    {
//        static void Main(string[] args)
//        {
//            int[] arr = { 1, 2, 3 };

//            try
//            {
//                for (int i = 0; i < 5; i++)
//                {
//                    Console.WriteLine(arr[i]);
//                }
//            }

//            catch (IndexOutOfRangeException e)
//            {
//                Console.WriteLine(“예외가 발생했지롱 : {0}”, e.Message);
//            }

//            Console.WriteLine(“종료”);
//        }
//    }
//}

////예외 던지기
//using System;

//namespace Throw
//{
//    class jpcorp
//    {
//        static void DoSomething(int arg)
//        {
//            if (arg < 10)
//                Console.WriteLine(“arg : {0}”, arg);
//            else
//                throw new Exception(“arg가 10보다 큽니다”);
//        }

//        static void Main(string[] args)
//        {
//            try
//            {
//                DoSomething(1);
//                DoSomething(2);
//                DoSomething(3);
//                DoSomething(4);
//                DoSomething(5);
//                DoSomething(11);
//                DoSomething(31);//예외가 발생해서 실행되지 않음.

//            }

//            catch (Exception e)
//            {
//                Console.WriteLine(e.Message);
//            }
//        }
//    }
//}

////try ~ catch와 finally

//using System;

//namespace Finally
//{
//    class jpcorp
//    {
//        static int Divide(int divisor, int divided)
//        {
//            try
//            {
//                Console.WriteLine(“Divide() 시작”);
//                return divisor / divided;
//            }

//            catch (DivideByZeroException e)
//            {
//                Console.WriteLine(“Divide() 예외발생 :  0으로 나눴네 “);
//                throw e;
//            }

//            finally
//            {
//                Console.WriteLine(“Divide() 끝”);
//            }
//        }

//        static void Main(string[] args)
//        {
//            try
//            {
//                Console.Write(“제수를 입력하세요 “);
//                String temp = Console.ReadLine();
//                int divisor = Convert.ToInt32(temp);

//                Console.Write(“피제수를 입력하세요”);
//                temp = Console.ReadLine();
//                int divided = Convert.ToInt32(temp);

//                Console.WriteLine(” {0}/{1} = {2}”, divisor, divided, Divide(divisor, divided));
//            }

//            catch (FormatException e)
//            {
//                Console.WriteLine(“에러 : “, e.Message);
//            }

//            catch (DivideByZeroException e)
//            {
//                Console.WriteLine(“에러 : “, e.Message);
//            }

//            finally
//            {
//                Console.WriteLine(“프로그램을 종료합니다”);
//            }
//        }
//    }
//}

using System;
namespace MyException

{
    class InvalidArguementException : Exception
    {
        public InvalidArguementException()
        {
        }

        public InvalidArguementException(string message) : base(message)
        {
        }

        public object Argument
        {
            get;
            set;
        }
        public string Range
        {
            get;
            set;
        }
    }

    class MainApp
    {
        static uint MergeARGB ( uint alpha, uint red, uint green, uint blue)
        {
            uint[] args = new uint[] {alpha,red,green, blue};

            foreach ( uint arg in args)
            {
                if (arg >255)
                    throw new InvalidArguementException()
                    {
                        Argument = arg, Range = “0~255”
                    };
            }

            return (alpha « 24 & 0xFF000000) |
                     (red « 16 & 0x00FF0000) |
                    (green « 8 & 0x0000FF00) |
                    (blue  &0x000000FF);
        }

        static void Main(string[] args)
        {
            try
            {
                Console.WriteLine(“0x{0:X}”, MergeARGB(255, 111, 111, 111));
                Console.WriteLine(“0x{0:X}”, MergeARGB(1, 65, 192, 128));
                Console.WriteLine(“0x{0:X}”, MergeARGB(0, 255, 255, 300));
            }

            catch (InvalidArguementException e)
            {
                Console.WriteLine(e.Message);
                Console.WriteLine(“Argument : {0}, Range:{1}”, e.Argument,e.Range);
            }
        }
    }
}

Hugo로 만듦
JimmyStack 테마 사용 중