5-10장

2022. 4. 18. 14:59C#/C#에 대한 다양한 공부

??연산자

using System;

namespace study
{
    class Program
    {
        static void Main(string[] args)
        {
           string test = "hello";
            if (test == null)
                Console.WriteLine("null");
            else
            {
                Console.WriteLine(test);
            }

            string test2 = "world";
            Console.WriteLine(test2 ?? "null");

        }
    }
}

자동 구현 속성(프로퍼티)

  class Test
    {
        public string Name { get; set; }
    }
    
  class Test2 //C# 6.0버전 이상
   {
        public string Name { get; set; } = "청호";
    }

객체 초기화

using System;

namespace study
{    class Person
    {
        string name;
        int age;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        public int Age
        {
            get { return age; }
            set { age = value; }
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            Person person1 = new Person();
            Person person2 = new Person { Name = "청호" };
            Person person3 = new Person { Age = 26 };
            Person person4 = new Person { Name = "청호", Age = 26 };
        }
    }
}

 

'C# > C#에 대한 다양한 공부' 카테고리의 다른 글

델리게이트, 이벤트  (0) 2022.06.01
추상클래스, 인터페이스 연습용  (0) 2022.05.24
델리게이트와 이벤트를 활용한 콜벡메서드 예제  (0) 2022.05.24
11-15장  (0) 2022.04.18
1-4장  (0) 2022.04.14