Computer >> Máy Tính >  >> Lập trình >> C#

Phương thức UInt16.ToString trong C # với các ví dụ

Phương thức UInt16.ToString () trong C # được sử dụng để chuyển đổi giá trị số của cá thể UInt16 hiện tại thành biểu diễn chuỗi tương đương của nó.

Cú pháp

Sau đây là cú pháp -

public override string ToString();

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ để triển khai phương thức 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");
   }
}

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Value1 (String representation) = 100
Value2 (String representation) = 80
Return value (comparison) = False
val1 != val2

Ví dụ

Bây giờ chúng ta hãy xem một ví dụ khác để triển khai phương thức UInt16.ToString () -

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");
   }
}

Đầu ra

Điều này sẽ tạo ra kết quả sau -

Value1 (String representation) = 0
Value2 (String representation) = 65535
Return value (comparison) = False
val1 != val2