Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Byte Struct in C#
The byte struct in C# represents an 8-bit unsigned integer that can store values from 0 to 255. It is one of the fundamental value types in .NET and provides various methods for comparison, conversion, and formatting operations.
Syntax
Following is the syntax for declaring and initializing a byte variable −
byte variableName = value; // value must be between 0 and 255
Fields
The byte struct provides two important constant fields −
| Field | Description | Value |
|---|---|---|
| MaxValue | Represents the largest possible value of a byte | 255 |
| MinValue | Represents the smallest possible value of a byte | 0 |
Example
using System;
public class Demo {
public static void Main() {
Console.WriteLine("Byte MinValue: " + byte.MinValue);
Console.WriteLine("Byte MaxValue: " + byte.MaxValue);
Console.WriteLine("Size in bits: 8");
}
}
The output of the above code is −
Byte MinValue: 0 Byte MaxValue: 255 Size in bits: 8
Common Methods
| Method | Description |
|---|---|
| CompareTo(byte) | Compares this instance to another byte and returns an integer indicating their relative values |
| Equals(byte) | Returns true if this instance equals the specified byte value |
| ToString() | Converts the byte value to its string representation |
| Parse(string) | Converts a string representation to a byte value |
| TryParse(string, out byte) | Attempts to convert a string to a byte without throwing exceptions |
Using Byte.Parse() with Exception Handling
Example
using System;
public class Demo {
public static void Main() {
string str = "186";
try {
byte val = byte.Parse(str);
Console.WriteLine("Parsed value: " + val);
}
catch (OverflowException) {
Console.WriteLine("Value is out of range for a byte: " + str);
}
catch (FormatException) {
Console.WriteLine("Invalid format for a byte: " + str);
}
// Testing with invalid value
string invalidStr = "300";
try {
byte invalidVal = byte.Parse(invalidStr);
Console.WriteLine("Parsed value: " + invalidVal);
}
catch (OverflowException) {
Console.WriteLine("Value is out of range for a byte: " + invalidStr);
}
}
}
The output of the above code is −
Parsed value: 186 Value is out of range for a byte: 300
Formatting Byte Values
Example
using System;
public class Demo {
public static void Main() {
byte[] arr = { 0, 10, 50, 90, 100, 150 };
Console.WriteLine("Decimal\tPadded\tHexadecimal");
Console.WriteLine("-------\t------\t-----------");
foreach (byte b in arr) {
Console.WriteLine($"{b}\t{b:D4}\t{b:X4}");
}
}
}
The output of the above code is −
Decimal Padded Hexadecimal ------- ------ ----------- 0 0000 0000 10 0010 000A 50 0050 0032 90 0090 005A 100 0100 0064 150 0150 0096
Byte Comparison and Operations
Example
using System;
public class Demo {
public static void Main() {
byte a = 100;
byte b = 150;
byte c = 100;
Console.WriteLine($"a = {a}, b = {b}, c = {c}");
Console.WriteLine($"a.CompareTo(b): {a.CompareTo(b)}");
Console.WriteLine($"a.CompareTo(c): {a.CompareTo(c)}");
Console.WriteLine($"b.CompareTo(a): {b.CompareTo(a)}");
Console.WriteLine($"a.Equals(c): {a.Equals(c)}");
Console.WriteLine($"a.Equals(b): {a.Equals(b)}");
}
}
The output of the above code is −
a = 100, b = 150, c = 100 a.CompareTo(b): -1 a.CompareTo(c): 0 b.CompareTo(a): 1 a.Equals(c): True a.Equals(b): False
Conclusion
The byte struct in C# is essential for working with 8-bit unsigned integers ranging from 0 to 255. It provides methods for parsing, formatting, comparison, and conversion operations, making it useful for low-level data manipulation, file operations, and network programming where memory efficiency is important.
