본문 바로가기

C#

C# 문법 데이터 형변환과 프로퍼티에 대하여...

반응형

주수홍강사, [17.07.20 16:50]
using System;
class Program
{
    static void Main(string[] args)
    {
        int iNum1 = 100;

        Object aObject = iNum1; // Boxing

        int iNum2 = (int)aObject;   // Unboxing

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

 

 

 

using System;

namespace ConsoleApp12
{
    class Vehicle
    {
    }
    class Car : Vehicle
    {
    }
    class Program
    {


        static void Main(string[] args)
        {

            Vehicle vehicle = new Vehicle();
            Car car = new Car();
            car = vehicle as Car; // 내부에 Car를 가지고 있는지 확인 해주는 역할 
            //-> car가 없으면 null 값이 들어간다 // 자동으로 형변환 까지 해준다. 
            if (car == null)
            {
                Console.WriteLine("널보내 줄께" );
            }
            else
            {
                Console.WriteLine(car);
            }​

 

 

 

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

namespace ConsoleApp12
{
    class Vehicle
    {

    }
    class Car : Vehicle
    {

    }
    class Program
    {/// <summary>
    /// 데이터의 형변환
    /// </summary>
    /// <param name="args"></param>
        static void Main(string[] args)
        {
            Vehicle vehicle = new Vehicle();
            Car car = new Car();
            Vehicle a = car; // a <= car  -- 형변환  : 묵시적/암시적/자동
            Car b = (Car)a;  // b <= a    -- 형변환  : 명시적/강제적/수동
        }
    }
}

 

 

 

 

 

 

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

namespace ConsoleApp12
{
    class Asian
    {
    }
    class America : Asian
    {
    }
    class Program
    {


        static void Main(string[] args)
        {
            Asian[] array = new Asian[] { new Asian(), new America(), new Asian() };

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




            Object human = new Asian();
            Asian asia;
            America america;

            asia = (Asian)human;
            america = (America)human;

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


            //try
            //{
            //    acar = (Car)avehicle;
            //}
            //catch (InvalidCastException)
            //{

            //    Console.WriteLine("허용되지 않는 형변환 예외처리");
            //}


        }
    }

 

 

 

반응형

 

 

 

using System;

namespace ConsoleApp12
{
    class Vehicle
    {
    }
    class Car : Vehicle
    {
    }
    class Program
    {


        static void Main(string[] args)
        {

            Object[] aobject = new Object[] {    new Vehicle(),
                                                 new Vehicle(),
                                                 new Car(),
                                                 new Vehicle(),
                                                 new Car(),
                                                 new Vehicle() };
            Car car;
            for (int i = 0; i < aobject.Length ; i++)
            {
                if ((car= aobject[i] as Car) == null)
                {
                    Vehicle vehicle = (Vehicle)aobject[i];
                    Console.WriteLine("vehicle 타입");
                }
                else
                {
                    Console.WriteLine("car타입");
                }
            }
            

주수홍강사, [17.07.20 16:08]
if((aCar = aObject[i] as Car) == null)

주수홍강사, [17.07.20 16:08]
(aCar = aObject[i] as Car)

주수홍강사, [17.07.20 16:08]
aCar = aObject[i] as Car

주수홍강사, [17.07.20 16:09]
카 객체가 aObject에 있는 경우는
if( 카객체 == null)

주수홍강사, [17.07.20 16:09]
카 객체가 aObject에 없는 경우는
if( null == null)

주수홍강사, [17.07.20 16:11]
if((aCar = aObject[i] as Car) == null)


aCar = aObject[i] as Car

카 객체가 aObject에 있는 경우는
aCar = aObject[i]
카 객체가 aObject에 없는 경우는
aCar = null.

 

using System;

namespace ConsoleApp12
{
    class Vehicle
    {
    }
    class Car : Vehicle
    {
    }
    class Program
    {


        static void Main(string[] args)
        {

            Object[] aobject = new Object[] {    new Vehicle(),
                                                 new Vehicle(),
                                                 new Car(),
                                                 new Vehicle(),
                                                 new Car(),
                                                 new Vehicle() };
            Car car;
            for (int i = 0; i < aobject.Length ; i++)
            {
                if ((car= aobject[i] as Car) == null)
                {   // car 가 Vehicle 객체인 경우
                    Vehicle vehicle = (Vehicle)aobject[i];
                    Console.WriteLine("vehicle 타입");
                }
                else
                {   // car 가 Car 객체인 경우
                    Console.WriteLine("car타입");
                }
            }
            
            
            

 

 

 

 

 

 

 

 

 

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

namespace ConsoleApp13
{
    public class PropertyTest
    {
        private string _name;
        private int _readonly;
        private int _writeonly;

        public int Writeonly //쓰기만 가능 set으로 구현
        {
            set { _writeonly = value; }
        }


        public int Readonly //일기만 가능 get으로 구현
        {
            get { return _readonly; } // 읽기만 할 수 있는거 할려면 set을 뺀다
        }

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }
    }
    class Program
    {
        public static void Main(string[] args)
        {
            PropertyTest pt = new PropertyTest();
            pt.Name = "헬로 키티";
            Console.WriteLine(pt.Name);
            Console.WriteLine(pt.Readonly);
            //Console.WriteLine(pt.Writeonly);
            pt.Writeonly = 100;

            int inum = 100;
            Object aobject = inum;     // Boxing
            int inum2 = (int)aobject;  // Unboxing
            Console.WriteLine(inum);
            Console.WriteLine(aobject);
            Console.WriteLine(inum2);
        }
    }
}

 

 

 

 

 

 

 

 

프로퍼티 사용 방법!!!!

 

 

 

 

 

 

 

 

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

namespace ConsoleApp14
{
    public class IndexerTest
    {
        private Hashtable myFavorite = new Hashtable();
        public string this[string kind]
        {
            get { return (string)myFavorite[kind]; }
            set { myFavorite[kind] = value; }
        }
    }
    public class Test
    {
        

        public static void Main(string[] args)
        {
            IndexerTest it = new IndexerTest();
            it["fruit"] = "apple";
            it["color"] = "blue";
            Console.WriteLine(it["fruit"]);
            Console.WriteLine(it["color"]);


        }
    }
}

 

이거 다시 공부해보기

 

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

namespace ConsoleApp14
{
    public class IndexerTest
    {
        private Hashtable myFavorite = new Hashtable();
        public string this[string kind]
        {
            get { return (string)myFavorite[kind]; }
            set { myFavorite[kind] = value; }
        }
    }
    public class Test
    {
        private string[] _array;
        public string this[int index] // ?
        {
            get { return _array[index]; }
            set { _array[index] = value; }
        }

        public static void Main(string[] args)
        {
            IndexerTest it = new IndexerTest();
            it["fruit"] = "apple";
            it["color"] = "blue";
            Console.WriteLine(it["fruit"]);
            Console.WriteLine(it["color"]);

            Test atest = new Test();
            atest[0] = " 할룽";
            Console.WriteLine(atest[0]);


        }
    }
}
반응형