C#/복습

2020-04-30 삼항연산자, Null병합연산자, 연산자 우선순위

J월드 2020. 4. 30. 22:59

책에 있는 걸 복습해 보았다.

 

 

삼항연산자 

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_018
{
    class App
    {
        public App()
        {
            //삼항 연산자
 
            int num1 = 3;
            int num2 = 5;
 
            bool result = false;
 
            result = (num1 + num2 < 10) ? true : false;
            Console.WriteLine("{0},  {1}", num1 + num2,result);
 
            if (result == true)
                this.PrintTrue();
            else
                this.PrintFalse(); 
        }
 
        public void PrintTrue()
        {
            Console.WriteLine("PrintTrue를 출력합니다. ");
        }
 
        public void PrintFalse()
        {
            Console.WriteLine("PrintFalse를 출력합니다.");
        }
    }
}
 
 
 

 

 

Null 병합 연산자

null 값을 체크하는 연산자

더보기
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Repeat_018
{
    class App
    {
        public App()
        {
            // null 병합 연산자
 
            int? a = null;
            int b = 10;
            int? c = null;
            int d = 100;
            int result = 0;
 
            result = a ?? b;
            Console.WriteLine("result : " + result);
 
            result = a ?? c ?? d;
            Console.WriteLine("result : " + result);
 
        }
    }
}
 
 
 

 

 

연산자 우선 순위