본문 바로가기
728x90
반응형

C#72

C#] 식별자/변수명 네이밍 규칙, convention identifier(식별자)란? type(class, interface, struct, delegate, enum) type명, 변수명, member명, namespace명이다. 네이밍 규칙 identifier는 반드시 아래 규칙을 따라야 한다. 아래 규칙을 따르지 않으면 C# compiler가 error를 발생한다. Identifier는 반드시 문자 또는 밑줄선(_)으로 시작한다. if라는 이름의 식별자를 선언하려면 @prefix를 붙여서 @if 로 선언하면 된다. 네이밍 conventions 규칙에 더해, identifier conventions이 .NET API 전체에 사용된다. 이 conventions은 compiler가 강제하지 않더라도, name의 일관성을 제공한다. 다르게 사용해도 무방하다... 2024. 1. 12.
C#] Deprecated Method 표시, 미사용 함수 표시 C#에서 어떻게 deprecated method를 표시하는지 알아보자. Preparing the Environment public static class DateUtils { public static int GetCurrentYearV1() { return 2022; } public static int GetCurrentYearV2() { return DateTime.UtcNow.Year; } } 현재 연도를 가져오는 함수가 2개 있다. GetCurrentYearV1() 함수는 과거 연도를 반환한다. GetCurrentYearV2() 함수가 현재 연도를 가져오기 더 적합하다. ObsoleteAttribute GetCurrentYearV1() 함수에 ObsoleteAttribute를 사용하므로 deprec.. 2024. 1. 9.
C#] PeriodicTimer class 몇 시스템 개발 시, task 실행을 일정한 시간을 두고 해야할 때가 있다. C#은 .NET 6에서 소개된 PeriodicTimer class를 제공한다. 기능과 사용법을 살펴보고 간단히 구현해보자. C#에서 PeriodicTimer Class 작동법 1. 필요한 변수를 정의하자. private readonly PeriodicTimer _periodicTimer; private readonly CancellationTokenSource _cancellationToken = new(); private Task? _task; private int _size; 차후 초기화할 PeriodicTimer 변수가 있다. 반복 Task를 종료시킬때 사용할 CancellationTokenSource 변수를 정의하고 초기.. 2024. 1. 9.
C#] Path의 File, Directory 여부 확인 path가 file인지 directory인지 결정하는 방법을 살펴보자. 2가지 방법이 있다. 첫번째는 path가 file 또는 directory로 존재하는지 확인한다. 두번째는 path의 attribute를 확인한다. Directory, File 존재여부 확인 .NET에는 2쌍의 클래스가 있다. File과 Directory가 있고, FileInfo와 DirectoryInfo가 있다. File 함수는 FileInfo 함수와 비슷하고, Directory함수는 DirectoryInfo 함수와 비슷하다. Directory에서 file을 구분하는데 이 클래스들이 어떻게 사용되는지 보자. Directory와 File 클래스의 사용 File와 Directory는 static class이다. 둘다 Exists() 함수.. 2024. 1. 5.
C#] System.Linq.Enumerable.Aggregate 함수 MS 문서를 기반으로 단계별로 살펴보자. Aggregate() 함수는 source 순서로 모든 elements에 function을 적용하고, 매 함수 호출의 결과값으로 누적된 값을 계산한다. 3개의 오버로드를 갖는다. 1. Aggregate(IEnumerable, Func) public static TSource Aggregate (this System.Collections.Generic.IEnumerable source, Func func); Code Example string sentence = "the quick brown fox"; // 위 문자열을 개별 단어로 나눈다. string[] words = sentence.Split(' '); string reversed = words.Aggregate.. 2023. 11. 15.
C#] LINQ - Any(), Exists() 함수 차이 출처 : Differences Between Any and Exists Methods in C# - Code Maze (code-maze.com) 두 함수는 비슷해보이지만, 각자 고유의 특성이 있다. Any Method LINQ Any() 함수는 collection의 어떤 element가 주어진 조건을 만족하거나 존재하는지 결정하도록 한다. collection의 elements를 하나하나씩 확인하며, result가 결정될 때마다 boolean value를 반환한다. IEnumerable interface를 구현하는 모든 collections(Arrays, Lists, Dictionaries)와 함께 사용할 수 있다. collections이 null이면 ArgumentNullException 에러를 던진다.. 2023. 11. 15.
728x90
반응형