Byte Struct in C#
In C#, Byte Struct is used to represent 8-bit unsigned integers. The Byte is an immutable value type and the range of Byte is from 0 to 255. This class allows you to create Byte data types and you can perform mathematical and bitwise operations on them like addition, subtraction, multiplication, division, XOR, AND etc.
Fields
| Field | Description |
|---|---|
| MaxValue | It is the largest possible value of a Byte. This field is constant. |
| MinValue | It is the smallest possible value of a Byte. This field is constant. |
Example:
// C# program to illustrate the concept // of MaxValue and MinValue fields in Byte using System; public class GFG { // Main method static public void Main() { // Display the minimum and // the maximum value of Byte Console.WriteLine("The minimum value "+ "of Byte: {0}", Byte.MinValue); Console.WriteLine("The maximum value "+ "of Byte: {0}", Byte.MaxValue); } } |
chevron_right
filter_none
Output:
The minimum value of Byte: 0 The maximum value of Byte: 255
Methods
| Method | Description |
|---|---|
| CompareTo(Object) | Compares the current instance to a specified object and returns a sign of their relative values. |
| CompareTo(Byte) | Compares this instance to a specified 8-bit unsigned integer and returns an indication of their relative values. |
| Equals(Object) | To get a value which indicates whether the current instance is equal to a specified object or not. |
| Equals(Byte) | Returns a value indicating whether this instance and a specified Byte object represent the same value. |
| GetHashCode() | Returns the hash code for this instance. |
| GetTypeCode() | Returns the TypeCode for value type Byte. |
| Parse() | Converts the string representation of a number to its Byte equivalent. |
| ToString() | Converts the value of the current Byte object to its equivalent string representation. |
| TryParse() | Tries to convert the string representation of a number to its Byte equivalent, and returns a value that indicates whether the conversion succeeded. |
Example:
// C# program to illustrate the concept // of CompareTo(Byte) method in Byte using System; public class GFG { // Main method static public void Main() { // val1, val2, and val3 are of byte type byte val1 = 32; byte val2 = 40; byte val3 = 10; // Display the comparision // Using CompareTo(Byte) method Console.WriteLine("Comparision 1: {0}", val1.CompareTo(val2)); Console.WriteLine("Comparision 2: {0}", val2.CompareTo(val3)); Console.WriteLine("Comparision 3: {0}", val3.CompareTo(val3)); Console.WriteLine("Comparision 4: {0}", val1.CompareTo(val3)); } } |
chevron_right
filter_none
Output:
Comparision 1: -8 Comparision 2: 30 Comparision 3: 0 Comparision 4: 22
Reference:
Recommended Posts:
- C# | Byte.CompareTo(Byte) Method
- C# | Byte.Equals(Byte) Method
- C# | Int 64 Struct
- C# | UInt32 Struct
- C# | UInt64 Struct
- C# | ValueTuple <T1,T2,T3,T4,T5,T6,T7> Struct
- C# | UInt16 Struct
- C# | Char Struct
- Decimal Struct in C#
- C# | Int16 Struct
- C# | Int32 Struct
- C# | ValueTuple <T1,T2,T3,T4> Struct
- C# | ValueTuple <T1,T2,T3,T4,T5> Struct
- C# | ValueTuple <T1,T2,T3,T4,T5,T6> Struct
- C# | ValueTuple <T1,T2,T3> Struct
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



