C#에서 UInt16.ToString() 메서드는 현재 UInt16 인스턴스가 가진 숫자 값을 이에 해당하는 문자열 표현으로 변환하는 데 사용됩니다.
구문
이 메서드의 기본 구문은 다음과 같습니다.
public override string ToString();
매개변수를 받지 않으며, 호출된 인스턴스의 값을 나타내는 문자열을 반환합니다.
예제 1
다음은 UInt16.ToString() 메서드를 구현한 첫 번째 예제입니다.
using System;
public class Demo {
public static void Main(){
ushort val1 = 100;
ushort val2 = 80;
Console.WriteLine("Value1 (String representation) = "+val1.ToString());
Console.WriteLine("Value2 (String representation) = "+val2.ToString());
bool res = val1.Equals(val2);
Console.WriteLine("Return value (comparison) = "+res);
if (res)
Console.WriteLine("val1 = val2");
else
Console.WriteLine("val1 != val2");
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Value1 (String representation) = 100 Value2 (String representation) = 80 Return value (comparison) = False val1 != val2
예제 2
이번에는 UInt16의 최솟값(0)과 최댓값(UInt16.MaxValue)을 활용한 예제를 살펴보겠습니다.
using System;
public class Demo {
public static void Main(){
ushort val1 = 0;
ushort val2 = UInt16.MaxValue;
Console.WriteLine("Value1 (String representation) = "+val1.ToString());
Console.WriteLine("Value2 (String representation) = "+val2.ToString());
bool res = val1.Equals(val2);
Console.WriteLine("Return value (comparison) = "+res);
if (res)
Console.WriteLine("val1 = val2");
else
Console.WriteLine("val1 != val2");
}
}출력 결과
위 코드를 실행하면 다음과 같은 결과가 출력됩니다.
Value1 (String representation) = 0 Value2 (String representation) = 65535 Return value (comparison) = False val1 != val2
정리
UInt16.ToString() 메서드는 매개변수 없이 간단히 호출할 수 있으며, 부호 없는 16비트 정수 값(0~65535 범위)을 문자열로 손쉽게 변환할 수 있습니다. 콘솔 출력, 로깅, UI 표시 등 다양한 상황에서 유용하게 활용됩니다.