[C#] What is the difference between is and as keyword in c# > C#/.Net/Blazor/IIS

본문 바로가기
사이트 내 전체검색

C#/.Net/Blazor/IIS

[C#] What is the difference between is and as keyword in c#

페이지 정보

작성자 sbLAB 댓글 0건 조회 6,647회 작성일 18-07-25 17:37

본문


What is the difference between is and as keyword in c#


1. What is the difference between is and as ?

   IS  - return true (if an object be cast to a specific type, otherwise false)

   AS -  return null (AS operator attemps to cast an object a specific type), and if it fails returns null



2. What is the different Cast operrator and AS operator ?

   Cast - Cast opertator throws an exception if the conversion cannot be done

   AS   - AS operator returns NULL if the conversion cannot be done


- (형변환 cast)는 불가하면 예외발생시킴

- AS는 예외발생시키지 않고. NULL  반환



msn012.gif https://youtu.be/PAv_TI26xO8?list=PL6n9fhu94yhWlAv3hnHzOaMSeggILsZFs



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace why_abstract
{
    class Program
    {
        static void Main(string[] args)
        {
 
            //Error	1	Cannot create an instance of the abstract class or interface
            //BaseEmployee bte = new BaseEmployee();
 
            FullTimeEmployee fte = new FullTimeEmployee()
            {
                ID = 101,
                FirstName = "Mark",
                LastName = "May",
                AnnualSalary = 60000
            };
 
            ContractEmployee cte = new ContractEmployee()
            {
                ID = 102,
                FirstName = "Brus",
                LastName = "Oh",
                HourlySalary = 100,
                TotalHoursWorked = 500
            };
 
            BaseEmployee asbte_baseEmployee = new FullTimeEmployee()
            {
                ID = 102,
                FirstName = "Brus",
                LastName = "Oh"
            };
 
            //-------------------------IS
 
            //fte is BaseEmployee - TRUE
            if (fte is BaseEmployee)
            {
                Console.WriteLine(fte.FirstName + " is BaseEmployee");
            }
            else
            {
                Console.WriteLine(fte.FirstName + " is Not BaseEmployee");
            }
 
            //fte is FullTimeEmployee - TRUE
            if (fte is FullTimeEmployee)
            {
                Console.WriteLine(fte.FirstName + " is FullTimeEmployee");
            }
            else
            {
                Console.WriteLine(fte.FirstName + " is Not FullTimeEmployee");
            }
 
 
            //cte is ContractEmployee - TRUE
            if (cte is BaseEmployee)
            {
                Console.WriteLine(cte.FirstName + " is BaseEmployee");
            }
            else
            {
                Console.WriteLine(cte.FirstName + " is Not BaseEmployee");
            }
 
            //fte is ContractEmployee - FALSE
            if (fte is ContractEmployee)
            {
                Console.WriteLine(fte.FirstName + " is FullTimeEmployee");
            }
            else
            {
                Console.WriteLine(fte.FirstName + " is Not FullTimeEmployee");
            }
 
 
            //-----------------------AS
 
            //NOT NULL
            FullTimeEmployee as_fullTimeEmployee = fte as FullTimeEmployee;
            if (as_fullTimeEmployee == null)
            {
                Console.WriteLine(fte.FirstName + " as_fullTimeEmployee is NULL");
            }
            else
            {
                Console.WriteLine(fte.FirstName + " as_fullTimeEmployee is NOT NULL");
            }
 
            //NOT NULL
            BaseEmployee as_baseEmployee = fte as FullTimeEmployee;
            if (as_baseEmployee == null)
            {
                Console.WriteLine(fte.FirstName + " as_baseEmployee is NULL");
            }
            else
            {
                Console.WriteLine(fte.FirstName + " as_baseEmployee is NOT NULL");
            }
 
            //NOT NULL
            FullTimeEmployee fullTimeEmployee = asbte_baseEmployee as FullTimeEmployee;
            if (fullTimeEmployee == null)
            {
                Console.WriteLine("fullTimeEmployee is NULL");
            }
            else
            {
                Console.WriteLine("fullTimeEmployee is NOT NULL");
            }
 
            //NULL
            ContractEmployee contractEmployee = asbte_baseEmployee as ContractEmployee;
            if (contractEmployee == null)
            {
                Console.WriteLine("contractEmployee is NULL");
            }
            else
            {
                Console.WriteLine("contractEmployee is NOT NULL");
            }
 
 
            //FullTimeEmployee
            Console.WriteLine(fte.GetFullName());
            Console.WriteLine(fte.GetMonthSalary());
 
 
            //ContractEmployee
            Console.WriteLine(cte.GetFullName());
            Console.WriteLine(cte.GetMonthSalary());
 
 
            //BaseEmployee <- 베이스클래스 참조
            Console.WriteLine("BaseEmployee <- 베이스클래스 참조");
            BaseEmployee bte = fte;
            Console.WriteLine(bte.ID + "-" + bte.GetMonthSalary());
            bte = cte;
            Console.WriteLine(bte.ID + "-" + bte.GetMonthSalary());
 
            Console.ReadKey();
 
        }
    }
 
 
    //BaseEmployee  BASE CLASS
    public abstract class BaseEmployee
    {
        public int ID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
 
        public string GetFullName()
        {
            return this.FirstName + " " + this.LastName;
        }
 
        public abstract int GetMonthSalary();
    }
 
 
    //FullTimeEmployee EACH CLASS
    public class FullTimeEmployee : BaseEmployee
    {
        public int AnnualSalary { get; set; }
 
        public override int GetMonthSalary()
        {
            return this.AnnualSalary / 12;
        }
    }
 
    //ContractEmployee EACH CLASS
    public class ContractEmployee : BaseEmployee
    {
        public int HourlySalary { get; set; }
        public int TotalHoursWorked { get; set; }
 
        public override int GetMonthSalary()
        {
            return this.HourlySalary * this.TotalHoursWorked;
        }
    }
 
 
}

4cb3f0ba517befeb9b243ed5915443ee_1532507872_8526.jpg
 


댓글목록

등록된 댓글이 없습니다.

회원로그인

접속자집계

오늘
78
어제
512
최대
1,279
전체
224,252

그누보드5
Copyright © sebom.com All rights reserved.