2014년 7월 11일 금요일

[강좌#10]C#연산자(Operators)[닷넷C#교육/ADO.NET강좌/ASP.NET교육잘하는곳/C#,ASP.NET교육추천/닷넷실무교육/.NET,C#/ADO.NET교육/닷넷학원/ASP.NET실무교육]

이번 강좌에서는 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)
OperatorDescription
=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)
OperatorDescription
==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)
OperatorDescription
&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 )
OperatorDescription
=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 )
OperatorDescription
()Type cast operator.
[]Accesses an element of an array or collection, or is the indexer operator.
typeofReturns type of object or if the argument is a primitive the primitive type of the argument is returned.
sizeofStruct size retrieval.
.Member access.
isObject comparison
asPerforms object downcast.
newCalls 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 )
OperatorDescription
checkedArithmetic checking turned on
uncheckedArithmetic checking turned off
어렵지 않습니다. 다음 예를 이해하도록 하죠~
checked {
int number = int.MaxValue;
Console.WriteLine(++number);
}
위예제에서는 Overflow 체크를 한다는 이야기 입니다. 즉 OverfloException이 발생 합니다. 물론 컴파일은 됩니다. 런타임중 예외가 발생한다는 이야기 입니다.
unchecked {
int number = int.MaxValue;
Console.WriteLine(++number);
}
위의 예제는 오버플로우 체크를 하지 않겠다는 의미 이므로 정수형의 최대갑에다 1을 더하니까 2의 보수 표현에 의해 -값으로 바뀌게 됩니다.

오라클자바커뮤니티교육센터, 개발자전문교육, 개인80%환급 
www.oraclejavacommunity.com


평일주간(9:30~18:10) 개강
(7/14)[기업100%환급]C#4.0,WinForm,ADO.NET프로그래밍
(7/14)[기업100%환급]SQL기초에서 Schema Object까지
(7/14)[기업100%환급]안드로이드개발자과정
(7/21)[기업100%환급]자바기초에서 JDBC, Servlet/JSP까지 
(7/21)[기업100%환급]Spring ,MyBatis,Hibernate실무과정
(7/21)[기업100%환급]PL/SQL,ORACLE HINT,TUNING
(7/21)[채용예정교육]오라클자바개발잘하는신입뽑기프로젝트,교육전취업확정

평일야간(19:00~21:50) 개강
(7/15)SQL기초에서실무까지
(7/15)안드로이드개발자과정
(7/16)Spring3.X, MyBatis, Hibernate실무과정
(7/17)닷넷(C#,Network,ADO.NET,ASP.NET)마스터과정
(7/21)웹퍼블리싱 마스터
(7/22)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지
(8/05)MyBatis3.X, Hibernate4.X ORM실무과정

주말(10:00~17:50) 개강
(7/12)SQL초보에서 Schema Object까지
(7/12)개발자를위한PLSQL,SQL튜닝,힌트
(7/19)C#,ASP.NET마스터
(7/19)JAVA,Network&WEB&Framework(자바기초에서웹스프링까지)
(7/19)Spring3.X, MyBatis, Hibernate실무과정
(7/19)웹퍼블리싱 마스터
(7/19)안드로이드개발자과정
(8/02)MyBatis3.X, Hibernate4.X ORM실무과정
(8/09)자바기초에서JSP,Ajax,jQuery,Spring3.2,MyBatis까지

주말저녁(18:30~22:20) 개강
(8/02)JAVA,Network&WEB&Framework
(8/09)SQL기초에서실무까지

댓글 없음:

댓글 쓰기