Home > Article > Backend Development > Byte structures in C#
Byte Struct represents an 8-bit unsigned integer in C#. The following are the fields:
Serial number | Fields and descriptions |
---|---|
1 |
MaxValue represents the maximum possible value of Byte. This field is a constant. |
2 |
MinValue represents the smallest possible value of Byte field is constant. |
Following are some of the methods −
Sr.no | Field & Description |
---|---|
1 |
CompareTo(Byte) Compares this instance to a specified 8-bit unsigned integer and returns an indication of their relative values. |
2 |
CompareTo(Object) Compare this instance with the specified object compare and returns an indication of their relative values. |
Equals(Byte)Returns a value indicating whether this instance and a The specified Byte objects represent the same value. | |
Equals(Object)Returns a value indicating whether this instance Equal to the specified object. | |
GetHashCode() Returns the hash code for this instance. | |
GetTypeCode().Returns the TypeCode of the value type Byte. |
using System; public class Demo { public static void Main() { string str = "186"; try { byte val = Byte.Parse(str); Console.WriteLine(val); } catch (OverflowException) { Console.WriteLine("Out of range of a byte.", str); } catch (FormatException) { Console.WriteLine("Out of range of a byte.", str); } } }OutputThis will produce the following output−
186ExampleLet’s look at another example − Live Demonstration
using System; public class Demo { public static void Main() { byte[] arr = { 0, 10, 50, 90, 100, 150 }; foreach (byte b in arr) { Console.Write(" ", b.ToString()); Console.Write(b.ToString("D4") + " "); Console.WriteLine(b.ToString("X4")); } } }OutputThis will produce the following output−
0000 0000 0010 000A 0050 0032 0090 005A 0100 0064 0150 0096
The above is the detailed content of Byte structures in C#. For more information, please follow other related articles on the PHP Chinese website!