본문 바로가기

기초다지기32

[C#][Study][기초다지기] 예외처리 예외처리 - try : 예외를 일으킬 수 있는 코드 식별 - catch : 예외처리 - throw : 사용자 정의 예외 메시지 생성 - finally : 예외 발생 여부에 관계없이 실행 [ex] using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Exception_Handling { class Program { static void Main(string[] args) { label: // Try block: 런타임에 발생하는 예외를 확인한다. try { int num1, num2; decimal result; Console.Write.. 2023. 9. 23.
[C#][Study][기초다지기] 배열 배열 [다차원 배열 선언] string[,] arr = new arr[3, 3]; // 자주 헷갈리는 것들 // 1. string[][] arr = new string[][]; // 괄호 2개 x // 2. string arr[,] = new string[3, 2]; // 선언할 때는 괄호가 먼저 와야함 [매개변수 배열] - 매개변수의 개수가 확실하지 않거나, n개의 매개변수를 허용하는 메서드를 생성할 때 사용 - params 키워드 + 1차원 배열 static int add(params int[] allnumber) (ex) 예제 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace p.. 2023. 9. 23.
[C#][Study][기초다지기] out 매개변수 out 매개변수 - 여러 값을 반환해주기 위해 사용 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace out_parameter { class Program { // 네 개의 매개변수 중 두 개는 출력값으로 반환된다. public static void rect(int len, int width, out int area, out int perimeter) { area = len * width; perimeter = 2 * (len + width); } static void Main(string[] args) { int area, perimeter; // 두 개의 반환 값을 가져옴 Prog.. 2023. 9. 23.
[C#][Study][기초다지기] Main 메서드 Main 메서드 - 프로그램 실행이 시작하고 끝나는 위치 - 단 하나만 있을 수 있고, 클래스나 구조체 내부에 있어야 하며, static을 사용해 선언해야 한다. - Main 메서드는 다른 함수 메서드에서 호출할 수 없어야 하기 때문에 항상 static으로 선언된다. 2023. 9. 23.
[C#][Study][기초다지기] static static - 클래스 내의 함수나 변수는 클래스가 객체를 생성할 때까지 메모리에 인스턴스가 생성되지 않는다. - 그러나 static을 사용해 함수나 변수를 선언하면, 메모리에 인스턴스가 직접 생성되고, 전역적으로 작동한다. - 또한 어떠한 객체도 참조하지 않는다. using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Static_var_and_fun { class number { public static int num; // 정적 변수 선언 public static void power() // 정적 함수 선언 { Console.WriteLine("Power of {0} = {1}", n.. 2023. 9. 23.
[C#][Study][기초다지기] get, set 프로퍼티 get set - get : 개인 필드에서 값을 '검색'하는 데 사용됨 - set : 개인 변수에 값을 '저장'하는 데 사용됨(value라는 암시적 매개변수 사용) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Get_Set { class access { private static string name; // private로 선언 public void print() { Console.WriteLine("\nMy name is " + name); } public string Name // Name 프로퍼티 생성 { get { // 값을 리턴 return name; } set { //.. 2023. 9. 23.
[C#][Study][기초다지기] 접근제한자 internal 접근제한자 internal - 같은 어셈블리 내에서만 사용 가능하다. 2023. 9. 23.
[C#][Study][기초다지기] 캡슐화와 추상화 캡슐화와 추상화 - 클래스나 인터페이스 외부에서 멤버를 숨기는 목적 - 사용자에게 관련 없는 데이터를 숨기는 프로세스 - 필수 기능만 표시하는 목적 - 사용자에게 관련 데이터만 표시하는 메커니즘 ex) 휴대폰 - 캡슐화 : 휴대폰 내부의 동작이 어떻게 진행되는지 사용자는 모름 - 추상화 : 사용자는 휴대폰 외부에서 다양한 유형들의 필수 기능을 사용 가능 2023. 9. 23.
[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.