본문 바로가기

전체 글

(44)
[프로그래밍 C#] 조건문 쓰지 않기 (if, switch case) 서문 프로그래머로 살면서 가장 짜증나는 상황은 고객 업체의 담당자가 일의 이해도가 떨어지거나 갑자기 현재의 일의 흐름과 다른 방향의 요구사항을 할때이다. .. 담당자가 일의 이해도가 떨어진다는 뜻은 .. 가솔린으로 움직이는 자동차를 만들되 자동차에 주유구는 만들지 마세요.. 같은 이상한 요구를 할 수 도 있다는 뜻이다. 보통 이런 경우 미리 문제점을 파악하면 다행이지만 대게 프로그램의 구조가 어느정도 잡혀 고치기도 힘든시점에 문제가 터지기 때문에 참 많이 힘들다. .. 그리고 일을 진행하다 보면 본래의 의도와 조금 또는 많이 다른 요구 사항을 할때에도 있다. 이런 요구 사항은 무조건 있는것 같다. 그래도 이건 좀 덜 힘들다. 여튼, 이런 저런 경험을 하다 보니 여러가지 꼼수 같은 스킬들이 생겼고, 이 경..
[C#] 문자열 배열 분할 Split (char 분할, string 분할, StringSplitOptions) 단일 char 구분자로 문자열 분할 string words = "123,456,789"; string[] split = words.Split(new char[] {','}); foreach(string word in split) System.Console.WriteLine(word); , 를 기준으로 문자열 분할됨. 복수 char 구분자로 문자열 분할 string words = "123,456,789.abc.def.ghi"; string[] split = words.Split(new char[] { ',', '.' }); foreach (string word in split) System.Console.WriteLine(word); , 과 . 를 기준으로 문자열 분할됨. 분할 배열 개수 지정 string..
[C#] 재귀 (Recursion) 의 단점을 보완한 꼬리 재귀 (Tail Recursion) 재귀 함수 (Recursion Function) 본인을 참조하는 함수 class Program { static void Main(string[] args) { Recur(); } static void Recur() { // 함수 내부에서 본인을 다수 호출함 Recur(); } } 재귀 함수는 아주 간단하게 설명하자면 함수안에서 다시 함수를 다시 호출 하는 것을 말함. 무한루프에 빠지지 않기 위한 종료 트리거 class Program { static void Main(string[] args) { Recur(10); } static void Recur(int end) { System.Console.WriteLine($"IDX : {end}"); // 종료 트리거 if (end == 0) { System.C..
[C#] 매개변수 배열 (params) class Program { static void Main(string[] args) { Combine("one"); Combine("one", "two"); Combine("one", "two", "three"); Combine("one", "two", "three", "four"); Combine("one", "two", "three", "four", "five"); Combine("one", "two", "three", "four", "five", "six"); } static void Combine(string arg1, params string[] args) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append($..
[C#] in 매개변수 class Program { static void Main(string[] args) { new Lesson(); } } class Lesson { public Lesson() { int num1 = 100; int num2 = 200; print(num1, num2); } // in 매개변수 int public static void print(in int num1, in int num2) { System.Console.WriteLine($"num1 : {num1}, num2 : {num2}"); } } in 1. 매개변수 앞에 in 을 붙여 주면 매개변수 전달 시 값 형식이 아닌 참조 형식으로 전달됨. 2. 매개변수 앞에 in 을 붙여 주면 해당 매개변수는 메소드 내에서 읽기만 가능. 3. 참조에 의한..
[C#] out 매개변수 class Program { static void Main(string[] args) { new Lesson(); } } class Lesson { public Lesson() { string a, b; Init(out a, out b); System.Console.WriteLine($"{a} {b}"); } // out 매개변수 string public static void Init(out string a, out string b) { a = "Good"; b = "Bad"; } } out 키워드 1. 매개변수 앞에 out 를 붙여 주면 매개변수 전달 시 값 형식이 아닌 참조 형식이으로 전달됨 2. 매개변수 앞에 out 를 붙여 주면 전달 된 매개변수는 메소드 내에서 값을 필수적으로 할당해야 함. ex..
[C#] 참조 매개변수 ref class Program { static void Main(string[] args) { new Lesson(); } } class Lesson { public Lesson() { string a = "Good", b = "Bad"; System.Console.WriteLine($"{a} {b}"); Swap(ref a, ref b); System.Console.WriteLine($"{a} {b}"); int c = 0, d = 1; System.Console.WriteLine($"{c} {d}"); Swap(ref c, ref d); System.Console.WriteLine($"{c} {d}"); } // 참조 매개변수 string public static void Swap(ref string a,..
[C#] 파일 삭제 class Lesson { public Lesson() { // 디렉토리 생성 CreateDir(@"C:\TestDir"); // 파일 생성 CreateFile(@"C:\TestDir\File.txt"); // 파일 삭제 DeleteFile(@"C:\TestDir\File.txt"); } // 디렉토리 생성 private void CreateDir(string dirPath) { // 해당 경로에 존재하지 않는 디렉토리들 생성 System.IO.Directory.CreateDirectory(dirPath); } // 파일 생성 private void CreateFile(string filePath) { System.IO.File.Create(filePath).Close(); } // 파일 삭제 priva..