C#

C# Operator_1 : 산술 연산자

_dev_mu 2023. 1. 4. 23:42

Arithmetic Operator : 산술 연산자

다음 연산자는 숫자 형식 피연산자를 포함한 산술 작업을 수행합니다.

- 단항 : ++ : 증분 / -- : 감소 / + : 더하기 / - : 빼기
- 이진 : * : 곱하기 / / : 나누기 / % : 나머지 / + : 더하기 / - : 빼기

산술 연산자는 모든 정수 및 부동 소수점 숫자 형식을 지원합니다. 
정수 형식의 경우 산술 연산자(++, -- 연산자 제외)는 int, uint, long 및 ulong 형식에 대해 정의 됩니다.
피현산자가 다른 정수 형식(sbyte, byte, short, ushort 또는 char)인 경우, 해당 값은 연산의 결과 형식이기도 한 int 형식으로 반환됩니다.
피연산자가 정수 형식 또는 부동 소수점 형식인 경우 해당 형식이 있으면 값은 가장 근사치를 포함하는 형식으로 반환됩니다.

 

Increment Operator : 증가 연산자

단항 증가 연산자 ++ 는 피연산자를 1씩 증가시킵니다.
피연산자는 변수, 속성 액세스 또는 인덱서 엑세스여야 합니다.

증가 연산자는 후위 증가 연산자 x++ 및 전위 증가 연산자 ++x 라는 두 가지 양식으로 지원됩니다.
전위 증가 연산자
double a = 1.5;
Console.WriteLine(a); // output: 1.5
Console.WriteLine(++a); // output: 2.5
Console.WriteLine(a); // output: 2.5
후위 증가 연산자
int a = 3;
Console.WriteLine(a); // output: 3
Console.WriteLine(a++); // output: 3
Console.WriteLine(a); // output: 4

 

Decrease Operation : 감소 연산자

단항 감소 연산자 -- 는 피연산자를 1씩 감소시킵니다.
피연산자는 변수, 속성 액세스 또는 인덱서 엑세스여야 합니다.

감소 연산자는 후위 감소 연산자 x-- 및 전위 감소 연산자 --x 라는 두 가지 양식으로 지원됩니다.
전위 감소 연산자
double a = 1.5;
Console.WriteLine(a); // output: 1.5
Console.WriteLine(--a); // output: 0.5
Console.WriteLine(a); // output: 0.5
후위 감소 연산자
int a = 3;
Console.WriteLine(a); // output: 3
Console.WriteLine(a--); // output: 3
Console.WriteLine(a); // output: 2

 

Plus & Minus Operator : 단항 더하기 및 빼기 연산자

단항 + 연산자는 피연산자의 값을 반환합니다.
단항 - 연산자는 피연산자의 숫자 부정을 반환합니다
Console.WriteLine(+4);     // output: 4

Console.WriteLine(-4);     // output: -4
Console.WriteLine(-(-4));  // output: 4

uint a = 5;
var b = -a;
Console.WriteLine(b);            // output: -5
Console.WriteLine(b.GetType());  // output: System.Int64

Console.WriteLine(-double.NaN);  // output: NaN
단항 - 연산자는 ulong 형식을 지원하지 않습니다.

 

Multiply Operator : 곱하기 연산자

곱하기 연산자 * 는 피연산자의 곱을 계산합니다.
Console.WriteLine(5 * 2);         // output: 10
Console.WriteLine(0.5 * 2.5);     // output: 1.25
Console.WriteLine(0.1m * 23.4m);  // output: 2.34

 

Division Operator : 나누기 연산자

나누기 연산자 / 는 오른쪽 피연산자로 왼쪽 피연산자를 나눕니다.
두 피연산즤 몫을 부동 소수점 숫자로 가져오려면 float, double, 또는 decimal 형식을 사용합니다.
Console.WriteLine(13 / 5);    // output: 2
Console.WriteLine(-13 / 5);   // output: -2
Console.WriteLine(13 / -5);   // output: -2
Console.WriteLine(-13 / -5);  // output: 2

// 부동 소수점
Console.WriteLine(13 / 5.0);       // output: 2.6

int a = 13;
int b = 5;
Console.WriteLine((double)a / b);  // output: 2.6
부동 소수점 나누기
float, double 및 decimal 형식의 경우 / 연산자의 결과는 두 피연산자의 몫입니다.
Console.WriteLine(16.8f / 4.1f);   // output: 4.097561
Console.WriteLine(16.8d / 4.1d);   // output: 4.09756097560976
Console.WriteLine(16.8m / 4.1m);   // output: 4.0975609756097560975609756098
피연산자 중 하나가 decimal이면 float 또는 double 모두 decimal로 암시적으로 변환할 수 없기 때문에 나머지 피연산자는 float 또는 double일 수 없습니다.
즉, float 또는 double 피연산자를 decimal 형식으로 암시적으로 변환해야 합니다.

Plus Operator : 더하기 연산자

더하기 연산자 + 는 피연산자의 합계를 계산합니다.
Console.WriteLine(5 + 4);       // output: 9
Console.WriteLine(5 + 4.3);     // output: 9.3
Console.WriteLine(5.1m + 4.2m); // output: 9.3
 문자열 연결 및 대리자 조합의 경우 + 연산자를 사용할 수 있습니다.

 

Minus Operator : 빼기 연산자

빼기 연산자 - 는 왼쪽 피연산자에서 오른쪽 피연산자를 뺍니다.
Console.WriteLine(47 - 3);      // output: 44
Console.WriteLine(5 - 4.3);     // output: 0.7
Console.WriteLine(7.5m - 2.3m); // output: 5.2
대리자 제거를 위해 - 연산자를 사용할 수 있습니다.

 

Compound Assignment Operator : 복합 할당 연산자

복합 할당식
x op= y
// 위의 식은 아래의 식과 동일합니다.
x = x op y

int a = 5;
a += 9;
Console.WriteLine(a);  // output: 14

a -= 4;
Console.WriteLine(a);  // output: 10

a *= 2;
Console.WriteLine(a);  // output: 20

a /= 4;
Console.WriteLine(a);  // output: 5

a %= 3;
Console.WriteLine(a);  // output: 2
각각의 +=, -= 연산자를 사용하여 이벤트에서 구독하거나 구독을 취소 할 수 도 있습니다.

 

연산자 우선 순위 및 결합성

다음 목록에서는 산술 연산자를 가장 높은 우선 순위부터 가장 낮은 것으로 정렬합니다.
  • x++, x-- 
  • ++x,  --x 
  • *, /, %
  • +, -
이진 산술 연산자는 왼쪽 결합형입니다. 즉, 우선 순위 수준이 같은 연산자는 왼쪽에서 오른쪽으로 계산됩니다.
괄호() 를 사용하여 연산자 우선 순위와 연결에 따라 주어진 계산 순서를 변경할 수 있습니다.
Console.WriteLine(2 + 2 * 2);   // output: 6
Console.WriteLine((2 + 2) * 2); // output: 8

Console.WriteLine(9 / 5 / 2);   // output: 0
Console.WriteLine(9 / (5 / 2)); // output: 4