728x90
반응형
§ C# 기본 형태 §
namespace Basic
{
class Program
{
static void Main(string[] args)
{
}
}
}
§ 연산자 §
using System;
// 산술 연산자
class ClassBasic
{
static void Main(string[] args)
{
// 섭씨 화씨 변환기 만들기
int 섭씨 = 12; // 한글변수 사용 가능하다
// 공식 : (섭씨 x 9 / 5 ) + 32 = 화씨
double 화씨 = (섭씨 * (double)9 / 5) + 32;
Console.WriteLine("{0}, {1}",화씨, 화씨.GetType());
// 첫 출력 : 53 : 결과값은 더블로 설정했으나, 시작값과 계산값이 모두 int여서 값도 int 출력
// 수정후 : 계산식의 int형 중에서 한 가지(어떤것이라도 가능)를 더블로 변경해주면 결과값이 더블로 연산되어 출력
int a8 = 100 % 3;
Console.WriteLine("{0},{1}", a8, a8.GetType());
}
}
// 할당 연산자
class ClassBasic
{
static void Main(string[] args)
{
int b8 = 100;
b8 = b8 + 10;
Console.WriteLine(b8);
b8 += 10;
Console.WriteLine(b8);
}
}
// 증감 연산자
class ClassBasic
{
static void Main(string[] args)
{
int a9 = 0;
a9++;
Console.WriteLine("a++ : {0}", a9);
++a9;
Console.WriteLine("++a : {0}", a9);
a9--;
Console.WriteLine(a9);
--a9;
Console.WriteLine(a9);
}
}
// 비교 연산자
class ClassBasic
{
static void Main(string[] args)
{
// 비교 연산자 > < >= <= ==
if( a9 >= 10) { }
}
}
// 논리 연산자
class ClassBasic
{
static void Main(string[] args)
{
// 논리 연산자 && || !
if (a9 > 1 && a9 < 100) { }
// 예제
int a10 = 0;
int b10 = 1;
++b10;
Console.WriteLine(b10);
if (++a10 > 0 || ++b10 > 2) // 논리 합의 경우, 둘 중 하나가 참인 경우, 다른 조건은 무시
{ }
Console.WriteLine(a10); // 1 + a10 = 1 + 0 = 1
Console.WriteLine(b10); // 1 + b10 = 1 + 1 = 2 가 되어야 하지만
Console.WriteLine(a10 + b10);
}
}
// Bit 연산자
class ClassBasic
{
static void Main(string[] args)
{
int x1 = 0x07; // 0000 0111 // 7
int y1 = 0x0E; // 0000 1110 // 14
int z = x1 & y1; // 0000 0110 // 6
Console.WriteLine(z);
z = x1 | y1; // 0000 1111 // 15
Console.WriteLine(z);
z = x1 ^ y1; // xor 연산 // 같으면 false, 다르면 true // 0000 1001 // 9
Console.WriteLine(z);
}
}
// shift 연산자 << >> : 숫자를 왼쪽 or 오른쪽으로 얼마만큼 이동시키고 싶을 때 사용함 (콘텐츠 개발 시 사용 빈도 낮음)
class Program
{
static void Main(string[] args)
{
int a = 4 << 1;
// ㄴ[해석: 4를 왼쪽으로 1(bit)만큼 이동시키고 싶다] → 그러기 위해서는 해당 숫자(4)를 2진수로 바꿔야 한다(0000 0100).
// ㄴ 진수 변환을 했다면 8자리의 숫자 중 가장 왼쪽 숫자를 1개 지우고 그만큼 오른쪽에 0을 채워넣는다. (왼쪽으로 1칸 이동시키는 게 목적이니까)
// 시작값 4 ( 0000 0100) -> ( << 1 ) -> 0000 1000 -> 결과값 8
Console.WriteLine(a);
a = 8 >> 1; //0000 1000 (8) -> ( >> 1 ) -> 0000 0100 (4)
Console.WriteLine(a);
// ㄴ (-) 값을 이진수 변환할 경우는 가장 숫자를 1로 채워줌 (0으로 채우면 양수가 되어버리기 때문)
// ㄴ ex. a= -8 >> 1; 일때 : 11111000
}
}
// 조건 연산자 (C 언어의 if문과 유사함)
class Program
{
static void Main(string[] args)
{
int a = 5;
int b = 1;
int result = (a > b) ? 1 : 0;
// ? : 조건문의 경계 위치
// ? 앞의 내용 : 조건문
// ? 뒤의 내용 중 첫번째 : True
// ? 뒤의 내용 중 두번째 : False
// 타입 변수 = ( '조건 1' '비교 연산자' '조건 2' ) ? 참 : 거짓 ;
if(a>b)
{result = 1;}
else
{result = 0;}
// 이런 의미
}
}
// null 병합 연산자 ?? (변수 내 null 값의 유무 확인 목적으로 사용)
class Program
{
static void Main(string[] args)
{
// null 병합 연산자 ??
// 왼쪽의 피연산자 값이 null 이면 : 오른쪽 피연산자 리턴
// 왼쪽의 피연산자 값이 null 아니면 : 왼쪽 피연산자 리턴
string c = null;
string re = c ?? "****";
// null 아니면 : re = c;
// null 이면 : re = "****";
}
}
// 문자열 결합 연산자
class Program
{
static void Main(string[] args)
{
string ss = "1234" + "5678"; // 중간에 공백을 주고 싶을 땐 "1234" + " " + "5678";로 사용하기
System.Console.WriteLine(ss);
}
}
728x90
'C# > C# 강의 정리' 카테고리의 다른 글
C# 배열 (0) | 2020.12.27 |
---|---|
C# 함수 (0) | 2020.12.27 |
C# mutable type, immutable type (0) | 2020.12.27 |
C# 문자열 (0) | 2020.12.27 |
C# 기초 정리 (정의, 변수) (0) | 2020.12.25 |