Cast expression
Peponi │ 11/11/2024 │ 2m
C#
SyntaxExpression
Cast expression
11/11/2024
2m
Peponi
C#
SyntaxExpression
1. Introduction
캐스트 식은 (T)expression
으로 나타내며, 명시적으로 expression
을 T
로 형식 변환한다. 명시적 변환이 불가한 경우 컴파일 타임 오류가 발생하며 런타임에 명시적 변환에 실패하는 경우 System.InvalidCastException, System.OverflowException 등이 발생할 수 있다.
2. Example
다음은 캐스트 식을 사용하는 사례를 보여준다.
long foo = 123;
Console.WriteLine((ushort)foo);
List<int> bar = [1, 2, 3];
IEnumerable<int> baz = (IEnumerable<int>)bar;
Console.WriteLine(string.Join(", ", baz));
/* output:
123
1, 2, 3
*/
사용자 정의 변환을 수행하는 예시는 아래와 같다.
public readonly struct Byte
{
private readonly byte _value;
public Byte(byte value) => _value = value;
public static explicit operator byte(Byte value) => value._value;
public static implicit operator Byte(byte value) => new(value);
}
var foo = new Byte(5);
byte bar = (byte)foo;
Console.WriteLine(bar);
/* output:
5
*/
다음은 명시적 변환에 실패하는 경우를 보여준다.
int? foo = null;
int bar = (int)foo; // System.InvalidOperationException
int foo = -1;
checked
{
byte bar = (byte)foo; // System.OverflowException
}