수업 015 (04.24 2020) 조건연산자(?:) F9,F5,F11 swap ref
2020. 4. 24. 18:20
728x90
1. 조건연산자
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._016_4
{
class App
{
public App()
{
// ** 조건 연산자
// 조건식 ? 참일때 값: 거짓일때 값
string result = (10 % 2) == 0 ? "짝수" : "홀수;";
Console.WriteLine(result);
}
}
}
|
2.F9, F5. F11
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._016_4
{
class App
{
// F9, F5 값 확인 가능 F11 한줄씩 실행
public int Fibonachci(int n)
{
if (n < 2)
{
return n;
}
else
{
return Fibonachci(n - 1) + Fibonachci(n - 2);
}
}
}
}
|
3. ref
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._016_4
{
class App
{
public App()
{
int x = 3;
int y = 4;
this.Swap(x, y);
Console.WriteLine("x: {0} y: {1}", x, y);
//output: 3, 4
// 참조형식이 아니라 값형식
}
public void Swap(int a, int b)
{
int temp = b;
b = a;
a = temp;
}
}
}
|
>> 값이 변하지 않음
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Study._016_4
{
class App
{
public App()
{
int x = 3;
int y = 4;
this.Swap(ref x, ref y);
Console.WriteLine("x: {0} y: {1}", x, y);
//output: 4, 3
}
public void Swap(ref int a, ref int b)
{
int temp = b;
b = a;
a = temp;
}
}
}
|
>> ref 추가 후 둘의 값이 바뀜
728x90
'C# > Study' 카테고리의 다른 글
수업 016 (04.27 2020) 상속 (:) (0) | 2020.04.28 |
---|---|
수업 016 (04.27 2020) Maple Story (0) | 2020.04.28 |
수업 015 (04.24 2020) 복습 Inventory + percentage (0) | 2020.04.24 |
수업 014 (04.23 2020) - DateTime (0) | 2020.04.24 |
수업 014 (04.23 2020) 복습 - json, dictionary..etc (0) | 2020.04.23 |