본문 바로가기

Study/C#36

[C#][Study][기초다지기] foreach 문 foreach 문 - 배열과 같은 컬렉션의 내부에 값들을 하나씩 가져오면서 반복을 진행 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace foreach_loop { class Program { static void Main(string[] args) { string[] arr = new string[5]; // 배열 선언 // 배열의 값들을 저장 arr[0] = "Steven"; arr[1] = "Clark"; arr[2] = "Mark"; arr[3] = "Thompson"; arr[4] = "John"; // foreach문을 이용해 값을 검색 foreach (string name.. 2023. 9. 23.
[C#][Study][기초다지기] do-while 문 do-while 문 - while문이 적어도 한 번은 실행되는 것이 보장됨 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace do_while { class Program { static void Main(string[] args) { int table, i, res; table = 12; i = 1; do { res = table * i; Console.WriteLine("{0} x {1} = {2}", table, i, res); i++; } while (i == 10); // 세미콜론 써야함에 유의! // i는 10이 아니지만 문장을 먼저 실행하고 조건을 체크하기 때문에 한번은 출.. 2023. 9. 23.
[C#][Study][기초다지기] using 문 using 문 - File이나 Font와 같이 관리되지 않는 클래스, 즉 사용 후 알아서 해제가 되지 않는 자원(리소스)들은 사용자가 직접 해제(Dispose)해 주어야 한다. - 매번 해제해주는 것은 실수가 잦고, 힘들기 때문에 using문을 사용해 자동으로 자원이 해제(Dispose)되게 한다. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Using_Statement { class check_using : IDisposable { public void Dispose() { // 자원이 해제될 때 실행 Console.WriteLine("실행 2"); } } class Progr.. 2023. 9. 23.
[C#][Study][기초다지기] lock 문 lock 문 - 현재 thread에서 프로그램을 실행하는동안 다른 thread의 개체를 잠그고, 진행되던 thread의 실행이 끝난 뒤에 개체의 잠금이 해제되어 다음 thread를 실행할 수 있게 함 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Lock_Statement { class Program { public void printname() { Console.WriteLine("My name is Steven Clark"); } static void Main(string[] args) { Program p = new Program(); lock (p) // lock 구문 생성.. 2023. 9. 23.
[C#][Study][기초다지기] uncheck 문 uncheck 문 - 스택 오버플로우가 발생해도 예외를 무시하고 실행시킴 - 잘못된 출력이 발생할 가능성이 높음 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Unchecked_Statement { class Program { static void Main(string[] args) { int num = int.MaxValue; // int의 최대값 할당 try { unchecked { // 스택 오버플로우 발생 -> 예외를 처리하지 않고 잘못된 값을 출력함 num = num + 1; Console.WriteLine(num); } } catch (Exception e) { Cons.. 2023. 9. 23.
[C#][Study][기초다지기] check 문 check 문 - 오버플로우나 언더플로우시 예외를 발생시킴 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Checked_Statement { class Program { static void Main(string[] args) { int num = int.MaxValue; // int의 최대값 할당 try { checked { // int의 최대값을 넘게하여 스택 오버플로우를 강제로 발생시킴 num = num + 1; Console.WriteLine(num); } } catch (Exception e) { Console.WriteLine(e.ToString()); } Console.. 2023. 9. 23.
[C#][Study][기초다지기] throw 문 throw 문 - 예외를 처리할 때 사용 - 예외는 catch 블록에서 처리됨 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace throw_statement { class Program { static void Main(string[] args) { int num1, num2, result; Console.WriteLine("Enter First Number"); num1 = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("Enter Second Number"); num2 = Convert.ToInt32(Console.ReadLine.. 2023. 9. 23.
[C#][Study][기초다지기] goto 문 goto 문 - label을 선언한 곳으로 돌아감 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace goto_statements { class Program { static void Main(string[] args) { string name; label: // 레이블 생성 -> 콜론 주의! Console.WriteLine("Enter your name: "); name = Console.ReadLine(); Console.WriteLine("Welcome {0}", name); Console.WriteLine("Press Ctrl + C for Exit\n"); goto label;.. 2023. 9. 23.
[C#][Study][기초다지기] XOR XOR(^) - 조건이 모두 참 or 모두 거짓 인 경우에만 true 반환 ex) (1 == 0) ^ (1 == 2) => true ex) (1 == 1) ^ (1 == 1) => true ex) (1 == 0) ^ (1 == 1) => false 2023. 9. 23.
[C#][Study][기초다지기] C# 사용자 입력 - ReadLine(), Read() Console.ReadLine(), Console.Read() - 사용자 입력을 읽어오는 함수 - 기본 반환형이 string이므로 int형을 반환하려면 형 변환을 해주어야 한다. // 사용자에게 받아온 문자열 입력을 Int형으로 바꾸는 방법 age = Int32.Parse(Console.ReadLine()); // Parse 사용 age = Convert.ToInt32(Console.ReadLine()); // Convert 사용 cf) 출력 함수 = Console.WriteLine("출력 값") 2023. 9. 23.
[C#][Study][기초다지기] ref, out의 차이점 1. out - 매개변수를 참조로 전달 - 인수가 초기화 되지 않았어도 전달 가능 - 내부에서 변수에 값을 할당 해주어야 함 2. ref - 매개변수를 참조로 전달 - 인수가 초기화 되지 않은 경우 전달 불가(오류 발생) ex) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace command { class Program { public static void Out(out int x) { x = 1; // 내부에서 해당 변수에 값을 할당 해주어야 함! x++; // 단독으로는 쓰일 수 없음 } public static void Reference(ref int x) { x++; } sta.. 2023. 9. 16.
[C#][Study][기초다지기] Main함수, 명령줄 인수 Main(string[] args) : 명령줄 인수 - Main() 함수는 프로그램 실행을 시작하는 곳이기에 다른 어떤 메소드에서도 매개변수를 허용하지 않는다. - Command line(명령줄)을 통해 매개변수를 허용하는데 런타임 시 n개의 매개변수를 받을 수 있는 배열형 매개변수를 선언 받는다.(Main(string[] args)) - 명령줄 인수를 사용해보는 두가지 방법이 있다. [사용법 1] cmd로 실제 매개변수 선언 [사용법 2] VS 내에서 디버그 매개변수를 미리 선언 아래의 코드를 두 예시에서 동일하게 사용했다. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace comm.. 2023. 9. 16.