07
20
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200717_002
{
    class Program
    {
        static void Main(string[] args)
        {
            int iNum1, iNum2;
            double dNum3;
            //Console.WriteLine(iNum1);
            //Console.WriteLine(iNum2);

            while (true)
            {
                try
                {
                    Console.Write("숫자를 입력해주세요 : ");
                    iNum1 = int.Parse(Console.ReadLine());

                    if(iNum1 < 0)
                    {
                        Exception exception = new Exception();
                        throw exception;
                    }

                    Console.Write("숫자를 입력해주세요 : ");
                    iNum2 = int.Parse(Console.ReadLine());

                    if(iNum2 < 0)
                    {
                        Exception exception = new Exception();
                        throw exception;
                    }

                    dNum3 = iNum1 / iNum2;
                    Console.WriteLine(dNum3);
                }
                catch (FormatException)
                {
                    Console.WriteLine("숫자가 아닌 것을 입력하였습니다.");
                    continue;
                }
                catch (DivideByZeroException)
                {
                    Console.WriteLine("0으로 나눌 수 없습니다.");
                    continue;
                }
                catch (Exception)
                {
                    Console.WriteLine("오류 발생!");
                    continue;
                }
                finally
                {
                    Console.WriteLine("무조건 실행함");
                }
                break;
            }
        }
    }
}

 

예제 실행 화면

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _20200717_003
{
    class Coordinate
    {
        public int x;
        public int y;
    }

    class Program
    {
        static Coordinate test()
        {
            return new Coordinate();
        }

        static void Main(string[] args)
        {
            Coordinate c1 = new Coordinate();
            Coordinate c2 = new Coordinate();

            c1.x = 1;
            c1.y = 2;
            c2.x = 1;
            c2.y = 2;

            Console.WriteLine($"c1 = {c1.x}, {c1.y}");
            Console.WriteLine($"c2 = {c2.x}, {c2.y}");

            if(c1 == c2)
            {
                Console.WriteLine("c1, c2는 같습니다.");
            }
            else
            {
                Console.WriteLine("c1, c2는 다릅니다.");
            }

            c1 = c2;

            if (c1 == c2)
            {
                Console.WriteLine("c1, c2는 같습니다.");
            }
            else
            {
                Console.WriteLine("c1, c2는 다릅니다.");
            }

            c1.x = 100;

            Console.WriteLine($"c1 = {c1.x}, {c1.y}");
            Console.WriteLine($"c2 = {c2.x}, {c2.y}");
        }

        static void Main1(string[] args)
        {
            Coordinate c = test();

            c.x = 1;
            c.y = 2;

            Console.WriteLine($"c = {c.x}, {c.y}");

            try
            {
                c = null;
                c.x = 1;
            }
            catch (NullReferenceException)
            {

                Console.WriteLine("객체가 없습니다.");
            }
            
        }
    }
}

 

예제 실행 화면

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    class Vehicle
    {
        public void Test()
        {
            Console.WriteLine("탈 것");
        }
    }

    class Car : Vehicle
    { 
        public new void Test()
        {
            Console.WriteLine("자동차");
        }
    }

    class Program
    {

        static void Main(string[] args)
        {
            //Vehicle[] array = new Vehicle[] { new Vehicle(), new Car(), new Vehicle() };

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

            //Vehicle avehicle = new Vehicle();
            //Car acar = new Car();

            //if(avehicle is Car)
            //{
            //    acar = (Car)avehicle;
            //}

            //acar = avehicle as Car;

            //if(acar == null)
            //{
            //    Console.WriteLine("null");
            //}
            //Console.WriteLine(acar);


            Object[] aObject = new Object[] { new Vehicle(), new Car(), new Vehicle(), new Car(), new Vehicle(), new Vehicle() };
            
            Car acar;
            foreach (Object item in aObject)
            {
                if((acar = item as Car) == null)
                {
                    Vehicle avehicle = (Vehicle)item;
                    Console.WriteLine("Vehicel 타입");
                }
                else
                {
                    Console.WriteLine("Car 타입");
                }
            }
        }


        static void Main2(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            Car car = new Car();

            try
            {
                car = (Car)vehicle;
                car.Test();
            }
            catch (InvalidCastException)
            {

                Console.WriteLine("잘못된 캐스팅입니다.");
            }
            
        }

        static void Main1(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            Car car = new Car();

            Vehicle a = car;
            Car b = (Car)a;
        }
    }
}

 

예제 실행 화면

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Asian { }
    class American { }
    class Program
    {
        static void Main(string[] args)
        {
            Object aHuman = new Asian();

            Asian aAsian;
            American aAmerican;

            aAsian = (Asian)aHuman;
            aAmerican = (American)aHuman;
        }
    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            int iNum = 100;

            Object aObject = iNum;

            int iNum2 = (int)aObject;

            Console.WriteLine(iNum);
            Console.WriteLine(aObject);
            Console.WriteLine(iNum2);
        }
    }
}

 

예제 실행 화면

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp4
{
    public class PropertyTest
    {
        private string _name;
        private int _readonly;
        private int _writeonly;
        private static int _Static;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public int Readonly
        {
            get { return _readonly; }
        }

        public int Writeonly
        {
            set { _writeonly = value; }
        }

        public static int Static
        {
            get { return _Static; }
            set { _Static = value; }
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            PropertyTest pt = new PropertyTest();
            pt.Name = "헬로 키티";
            Console.WriteLine(pt.Name);
            
            //pt.Readonly = 100;
            Console.WriteLine(pt.Readonly);

            pt.Writeonly = 100;
            //Console.WriteLine(pt.Writeonly);

            PropertyTest.Static = 200;
            Console.WriteLine(PropertyTest.Static);
        }
    }
}

 

예제 실행 화면

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp5
{
    class Test
    {
        private string[] _array;

        public string this[int Index]
        {
            get { return _array[Index]; }
            set { _array[Index] = value; }
        }
    }
    class IndexerTest
    {
        
        private Hashtable myFavorite = new Hashtable();
        public string this[string kind]
        {
            get { return (string)myFavorite[kind]; }
            set { myFavorite[kind] = value; }
        }

       

        static void Main(string[] args)
        {

            IndexerTest it = new IndexerTest();
            it["fruit"] = "apple";
            it["color"] = "blue";
            Console.WriteLine(it["fruit"]);
            Console.WriteLine(it["color"]);

            Test test = new Test();
            test[0] = "test";
        }
    }
}

 

예제 실행 화면

'스마트팩토리 > C#' 카테고리의 다른 글

24. 복습  (0) 2020.07.24
23. Indexer  (0) 2020.07.23
21. 게임제작, 복습  (0) 2020.07.17
20. 게임제작, 복습  (0) 2020.07.15
19. 게임 제작, 복습  (0) 2020.07.14
COMMENT