C#/Language

C# - Language - while, do statement (while, do 문)

1. Introductionwhile, do 문은 이하 블록을 반복 실행한다.while, do의 실행 차이점은 아래와 같다.while : 주어진 조건식이 true일 때 실행do : 주어진 조건식이 false일지라도 반드시 한 번 이상 실행반복문에서는 break 문, continue 문을 통해 반복 중단 또는 다음 반복 실행을 할 수 있다.2. whilewhile 문은 조건식이 true일 때 이하 블록을 반복 실행한다.while 문의 구성은 아래와 같다. while (condition) { }condition루프 실행을 위한 조건을 지정한다.조건식은 boolean 식으로 설정하며, true일 때 실행된다.while 문은 다음과 같이 사용할 수 있다. var index = 0; while (inde..

C#/Language

C# - Language - for, foreach statement (for, foreach 문)

1. Introductionfor, foreach 문은 이하 블록을 반복 실행한다.for, foreach의 실행 차이점은 아래와 같다.for : 주어진 조건식이 true일 때 실행foreach : 주어진 컬렉션 전체 실행반복문에서는 break 문, continue 문을 통해 반복 중단 또는 다음 반복 실행을 할 수 있다.2. for 문for 문은 주어진 조건식이 true일 때 블록을 실행한다.for 문의 구성은 아래와 같다. for ( initializer; condition; iterator) { }initializerfor 문의 최초 진입 시 1회 실행된다.일반적으로 루프 초기화 요소를 설정한다.initializer에 선언된 변수는 루프 외부에서 접근할 수 없다.condition루프 실행을 위한..

C#/Language

C# - Language - switch statement (switch 문)

1. Introductionswitch문은 제공된 식과 패턴을 이용해 실행할 문을 선택한다.switch식은 Switch expression을 참조한다.제공된 식과 패턴이 일치할 때만 실행한다.switch문에서 지원되는 패턴은 다음 목록을 참조한다.Type, declaration patternsConstant patternRelational patternsLogical patternsProperty, positional patternsDiscard, var patternList patterns2. Example기본적으로 switch문은 다음과 같이 사용한다. switch(expression) { case pattern: break; }아래는 위 표현을 이용한 산술 연산 예시다. C..

C#/Language

C# - Language - if statement (if 문)

1. Introductionif문은 제공된 식의 결과에 따라 실행하는 기능을 제공한다.제공된 식의 결과가 true일 때만 실행한다.코드의 분기는 if, else 키워드를 통해 진행한다.2. Example기본적으로 if문은 다음과 같이 사용한다. // expression이 true를 나타낼 때 실행 if (expression) { }아래는 위 표현을 이용한 산술 연산 예시다. Console.WriteLine(Calculate(10)); private int Calculate(int value) { if (value 3. elseelse 키워드를 통해 if문이 false인 경우에 확인할 추가 조건을 지정할 수 있다. Console.WriteLine(Calculate(11)); pri..

C#/Language

C# - Language - Lock statement (Lock 문)

1. Introductionlock 문은 하나의 스레드만 블록에 접근 가능하도록 하며, 다음과 같이 동작한다.잠금을 획득lock 블록 실행잠금 해제실행되는 동안 다른 스레드는 해제가 될 때까지 대기lock 블록에는 await 식을 사용할 수 없다.lock의 잠금 객체로 다음 유형은 사용하지 않도록 한다.thisTypestring2. Lock statementlock 문은 다음과 같이 사용한다. object _key = new(); lock (_key) { }만약 하나의 변수에 멀티스레드 접근을 하는 경우, 아래와 유사한 결과를 얻을 수 있다. public class Program { private static object _key = new(); private static b..

C#/Language

C# - Language - Declaration statements (선언문)

1. Introduction선언문은 지역 변수, 상수 또는 참조 변수를 선언한다. // local variable string foo = string.Empty; // local constant const int bar = 0; // local ref variable ref string baz = ref foo;선언문은 한번에 여러 변수를 선언할 수도 있다. int foo, bar, baz;선언문은 block, switch block에서는 허용되지만 embedded statement에는 허용되지 않는다. { int foo; // allowed } if(true) bool bar; // CS10232. Local variable지역 변수 선언은 다음과 같이 한다..

Peponi_
'문' 태그의 글 목록