본문 바로가기

Study64

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