2017년 3월 27일 월요일

[탑크리에듀,국비지원과정,C#강좌] C#연산자(Operators)

이번 강좌에서는 C#에서 사용 되어지는 연산자에 대해 알아 보도록 하겠습니다. 아래는 C#의 연산자 목록 입니다. 구분 연산자 기본 연산자 (x), ., f(x), a[x], x++, x--, new, typeof, sizeof, checked, unchecked 단항 연산자 +, -, !, ~, ++x, --x 산술 연산자 +, -, *, /, % 쉬프트 연산자 <<, >> 비교 연산자 <, >, <=, >=, is 비트 연산자 &, ^, | 논리 연산자 &&, || 조건 연산자 ?: 할당 연산자 =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= 연산자 이름 의미 (x) 괄호 연산의 우선순위 명시 x.y 점 클래스의 메소드나 속성 f() 메소드괄호 메소드의 인자를 괄호안에 넣는다 a[x] 대괄호 배열이나 인덱서의 인덱스 checked 형변환시 오버플로우를 체크 unchecked 형변환시 오버플로우 체크 않음 typeof() 객체에 대한 정보를 얻음 ( 리플렉션참고 ) sizeof() 객체의 크기를 얻음 산술연산자( Mathematical operators) Operator Description = Assignment operator += a+=b is equivalent to a = a + b -= a-=b is equivalent to a = a – b *= a*=b is equivalent to a = a * b /= a/=b is equivalent to a = a / b %= a%=b is equivalent to a = a % b <<= a<<=b is equivalent to a = a << b >>= a>>=b is equivalent to a = a >> b &= a&=b is equivalent to a = a & b ^= a^=b is equivalent to a = a ^ b |= a|=b is equivalent to a = a | b 관계 연산자(Relational operators) Operator Description == Equals != Not equal to < Less than <= Less than or equal to > Greater than >= Greater than or equal to 예)if (x == 2.0 && y != 4.0) { // do some stuff } 논리연산자(Logical Operators) Operator Description & Bitwise AND | Bitwise OR ^ Bitwise exclusive OR && Logical AND || Logical OR ! Not ?: Ternary && 는 AND, || 는 OR l ^ 는 XOR, ! 는 NOT 의 의미 하나짜리는 비트연산자로서 l & 는 AND, | 는 OR l ^ 는 XOR, ~ 는 NOT 의 의미 입니다. 예) a = b if (b [참고하세요] X &&Y 의 경우 X 가 false 이면 더 이상 Y 를 계산하지 않아도 false 임을 아니까 false 로 결정된다 . 결국 X가 true 일때만 Y 를 계산 하는 겁니다. X || Y 의 경우 X가 true 이면 더 이상 Y 를 계산하지 않고 true로 결정 합니다. 또한 X 가 false 인 경우에만 Y 를 계산 합니다. X|Y 나 X &Y 의 경우 X 값에 관계없이 B와 같이 계산 한 후에 true, false를 결정 합니다. 할당연산자( Assignment operators ) Operator Description = Assignment operator += a+=b is equivalent to a = a + b -= a-=b is equivalent to a = a – b *= a*=b is equivalent to a = a * b /= a/=b is equivalent to a = a / b %= a%=b is equivalent to a = a % b <<= a<<=b is equivalent to a = a << b >>= a>>=b is equivalent to a = a >> b &= a&=b is equivalent to a = a & b ^= a^=b is equivalent to a = a ^ b |= a|=b is equivalent to a = a | b x << 4 --> 왼쪽으로 4 bit shift 1bit shift 마다 값이 2 배씩 증가 x >> 4 --> 오른쪽으로 4 bit shift 1bit shift 마다 값이 1/2 배씩 감소 [아래의 예제를 따라 해 보세요] using System; public class Bitoper { public static void Main(string[] args) { int i = 16; int j = 64; int k; k = i & j; //i와 j를 비트 연산 i = i >>3; //i를 3비트 우쉬프트 j = i<<3; //i를 3비트 좌쉬프트 Console.WriteLine("k={0}, i={1}, j={2}", k,i,j); Console.ReadLine(); } } [결과] k=0, i=2, j=16 객체 연산자( Object operators ) Operator Description () Type cast operator. [] Accesses an element of an array or collection, or is the indexer operator. typeof Returns type of object or if the argument is a primitive the primitive type of the argument is returned. sizeof Struct size retrieval. . Member access. is Object comparison as Performs object downcast. new Calls a constructor. For a reference type, a new object is allocated on the heap. For a value type, the fields of the value type are initialized to their default values. is operator - determine if an object derives from another class (다른 클래스로 부터 파생 되었는지 확인하는 연산자, 파생되었다는 이야기는 하위클래스라는 이야기임.. 이부분은 객체 지향을 공부 하신후 다시 확인 바랍니다.) if ( a is bird ) b = (bird) a; //safe else Console.WriteLine("Not a Bird"); as operator - object “downcasts” (두개의 Reference-Type을 casting 하는 것처럼 형병환을 합니다. 변환이 되면 변환 된것을 넘겨주고 변환이 안되면 null을 돌려 줍니다. 이 부분 역시 객체 지향을 공부 하신후 다시 확인 바랍니다.) Bird b = a as Bird; //a를 Bird로 변환을 시도하여 변환이 되면 변환된것을 b에 돌려주고 아니면 null을 돌린다. if ( b == null ) Console.WriteLine("Not a Bird"); sizeof 연산자 주어진 데이터 형식의 크기를 byte 단위로 돌려 줍니다. 제약사항 --> sizeof 연산이 가능한 형식은 값 형식인 경우int, float, enum, struct등이 있습니다. 일반연산자( Miscellaneous operators ) Operator Description checked Arithmetic checking turned on unchecked Arithmetic checking turned off 어렵지 않습니다. 다음 예를 이해하도록 하죠~ checked { int number = int.MaxValue; Console.WriteLine(++number); } 위예제에서는 Overflow 체크를 한다는 이야기 입니다. 즉 OverfloException이 발생 합니다. 물론 컴파일은 됩니다. 런타임중 예외가 발생한다는 이야기 입니다. unchecked { int number = int.MaxValue; Console.WriteLine(++number); } 위의 예제는 오버플로우 체크를 하지 않겠다는 의미 이므로 정수형의 최대갑에다 1을 더하니까 2의 보수 표현에 의해 -값으로 바뀌게 됩니다.

댓글 없음:

댓글 쓰기