본문 바로가기

기초다지기32

[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.