PROGRAMMING LANGUAGE/C# (9) 썸네일형 리스트형 델리게이트와 람다식 using System; class Program { public delegate int Del(int a, int b); static void Main(string[] args) { Del del = (int a, int b) => { return a + b; }; int num = del(1, 1); Console.WriteLine( num ); } } using System; class Program { delegate int Prn1(string s); delegate int Prn2(); delegate void Prn3(string s); delegate void Prn4(); static void Main(string[] args) { Prn1 prn1 = (string s) => { ret.. 이벤트 using System; class Button { public delegate void Del(); // 델리게이트 선언 public event Del click; // 이벤트 선언 public void OnClick() // 이벤트 발생 { click(); // 이벤트 통지 } } class MainApp { static void Print() { Console.WriteLine("Click"); } static void Main(string[] args) { Button button = new Button(); button.click += Print; } } 예외처리 try catch when using System; class Program { class FilterableException : Exception { public int ErrorNo { get; set; } } static void Main(string[] args) { Console.WriteLine("Enter Number Between 0~10"); string input = Console.ReadLine(); try { int num = Int32.Parse(input); if (num 10) throw new FilterableException() { ErrorNo = num }; else Console.WriteLine($"Output : {num}"); } catch (FilterableEx.. StringBuilder using System; using System.Text; namespace MyCshap1 { class Program { static void Main(string[] args) { string s = string.Empty; for(int i = 1; i 인덱서 namespace MyCshap1 { class MyArray { private int[] array; public MyArray() { array = new int[5]; } public int this[int index] { get { return array[index]; } set { array[index] = value; } } } class Program { static void Main(string[] args) { MyArray myArray = new MyArray(); myArray[0] = 10; myArray[1] = 20; myArray[2] = 30; myArray[3] = 40; myArray[4] = 50; } } } string using System; namespace myCSharp1 { class Program { static void Main(string[] args) { // string은 immutable 타입으로 string에 추가 문자열을 추가하면 배열을 재할당한다. string s2 = "Hello"; char c = s2[0]; Console.WriteLine(c); for (int i = 0; i < s2.Length; i++) { Console.WriteLine(s2[i]); } s2 += " World"; for(int i = 0; i < s2.Length; i++) { Console.WriteLine(s2[i]); } } } } 10진수, 2진수, 16진수로 변수에 값 할당하기 using System; namespace myCSharp1 { class Program { static void Main(string[] args) { byte a = 240; Console.WriteLine($"a={a}"); // 10 진수 byte b = 0b1111_0000; Console.WriteLine($"b={b}"); // 2 진수 byte c = 0xF0; Console.WriteLine($"c={c}"); // 16 진수 } } } Console 기능 using System; namespace myCSharp1 { class Program { static void Main(string[] args) { Console.SetWindowSize(80, 20); Console.SetCursorPosition(40,10); Console.WriteLine("Hello"); Console.ForegroundColor = ConsoleColor.Yellow; // ConsoleColor의 변수를 받아서 글자색을 변경 Console.BackgroundColor = ConsoleColor.Green; // 글자의 배경. Console.Clear(); // Clear()을 해주면, 전체 콘솔창의 내용 삭제 Console.Beep(); // 삑소리가 난다 } } } 이전 1 2 다음