본문 바로가기

c#109

C# 비트 연산자를 이용한 홀수짝수 판별, 절반으로 나누기 함수 구현 C# 비트 연산자(Bitwise Operator) 활용하기 using System; class Program { static void Main(string[] args) { var input = Console.ReadLine(); var num = Int32.Parse(input); Console.WriteLine($"{num} is {IsOdd(num)}"); Console.WriteLine($"{num} half = {Half(num)}"); } static string IsOdd(int n) { return (n & 1) == 0 ? "even" : "odd"; } static int Half(int n) { return n >> 1; } } 홀수 짝수 판별의 경우, 0001번째 자리와 비교해서 곱.. 2024. 3. 18.
C# LINQ - Enumerable.Zip() Enumerable.Zip(IEnumerable, IEnumerable, Func) 메서드 public static System.Collections.Generic.IEnumerable Zip (this System.Collections.Generic.IEnumerable first, System.Collections.Generic.IEnumerable second, Func resultSelector); 매개변수 first IEnumerable 병합할 첫 번째 시퀀스 second IEnumerable 병합할 두 번째 시퀀스 resultSelector Func 두 시퀀스의 요소를 병합하는 방법을 지정하는 함수 반환 IEnumerable 두 입력 시퀀스의 병합된 요소가 들어있는 IEnumerable 예제 .. 2024. 3. 5.
C# 함수형으로 구현한 팩토리얼 함수 재귀로 구현한 팩토리얼 using System; class Program { static void Main(string[] args) { var input = Console.ReadLine(); var num = Int32.Parse(input); Console.WriteLine($"{num}! result: {Factorial(num)}"); } static long Factorial(int n) { if (n x * y); return result; } } 2024. 3. 4.
C# Linq - Enumerable.Take() Enumerable.Take(IEnumerable, Int32) 메서드 public static System.Collections.Generic.IEnumerable Take (this System.Collections.Generic.IEnumerable source, int count); 매개변수 source IEnumerable 요소가 반환되는 시퀀스 count Int32 반환할 요소의 수 반환 IEnumerable 입력 시퀀스의 시작 위치로부터 지정된 수의 요소가 들어있는 IEnumerable 예제 코드 int[] grades = { 59, 82, 70, 56, 92, 98, 85 }; IEnumerable topThreeGrades = grades.OrderByDescending(grade => .. 2024. 2. 26.
C# 웹 인증(Authentication) & 권한(Authorization) 코드 작성하기 C#으로 웹 인증 코드 작성하기 C# 프로젝트를 진행할 때 인증 관련된 코드를 작성하는 일이 생긴다면 C#이 제공하는 기능을 사용해서 좀 더 쉽게 인증 관련 코드를 작성하고 처리하는 것이 가능하다. 웹 인증을 위해서는 아래의 두 가지를 구현해야 한다. 인증(Authentication) vs 권한(Authorization) 인증(authentication) : 유저가 누구인지에 대해서 검증한다. 보통 토큰이 유효(valid)한 지 검증한다. 권한(authorization) : 유저가 해당 행동을 수행하는 것이 가능한지를 결정하는(determining) 프로세스이다. 사용자 정의 인증 스키마(Custom authentication schemes) 인증 스키마는 반드시 프레임워크 startup 파일에 등록되어.. 2024. 2. 19.
C# Linq - Enumerable.Reverse() Enumerable.Reverse(IEnumerable) 메서드 public static System.Collections.Generic.IEnumerable Reverse (this System.Collections.Generic.IEnumerable source); 매개변수 source IEnumerable 변환할 값의 시퀀스 반환 IEnumerable 특정 시퀀스 요소의 순서를 뒤집을때 사용한다. 예제 코드 static void Main(string[] args) { var s = "abcdef"; var revse_s = new string(s.Reverse().ToArray()); Console.WriteLine(revse_s); // fedcba } char[] apple = { 'a', 'p.. 2024. 2. 19.