Machineboy空
C# Type-testing operator : is, as, typeof 연산자 본문
is 연산자
지정된 형식과 호환되는지 확인
int i = 27;
object iBoxed = i;
Console.WriteLine(isBoxed is int); // True
Console.WriteLine(isBoxed is long); // False
as 연산자
지정된 형식으로 변환
IEnumerable<int> numbers = new List<int>() {10,20,30};
IList<int> indexable = numbers as IList<int>;
if(indexable != null)
{
Console.WriteLine(indexable[0] + indexable[indexable.Count -1]) // 10 + 30 = 40
}
예제
using System;
using System.Collections;
using System.Collections.Generic;
public static class FlattenArray
{
public static List<object> list = new List<object>();
public static IEnumerable Flatten(IEnumerable input)
{
list.Clear(); // 여러 번 호출될 때 중복 방지
dig(input);
return list;
}
public static void dig(IEnumerable input)
{
foreach (var a in input)
{
if (a == null) continue;
if (a is IEnumerable) // is 사용 안 함
{
IEnumerable nested = a as IEnumerable;
dig(nested);
}
else
{
list.Add(a);
}
}
}
}
https://exercism.org/tracks/csharp/exercises/flatten-array
Flatten Array in C# on Exercism
Can you solve Flatten Array in C#? Improve your C# skills with support from our world-class team of mentors.
exercism.org
'언어 > C#' 카테고리의 다른 글
C# Property 프로퍼티 get, set (0) | 2025.02.20 |
---|---|
C# Exceptions 예외 처리하기 : Try-Catch 문 (0) | 2025.02.19 |
Excercism - Yacht : LINQ 연습 (0) | 2025.02.18 |
C# Attribute, Flag Enum (0) | 2025.02.13 |
C# 열거형 Enum (0) | 2025.02.10 |