Home
C#
byte and sbyte Types
Updated Mar 8, 2025
Dot Net Perls
Byte. The C# byte type (which is 8 bits) is a compact and efficient type. Byte arrays are useful more often than single bytes—they can store file data.
To make matters more complex, in the C# language byte has no sign bit, but an sbyte does. We can use an sbyte when negative numbers are needed.
byte Array
First example. Byte is a value type. When used as a local variable, its storage location is directly contained in the evaluation stack. It can hold any value between 0 and 255.
Part 1 We declare a byte variable with the name "value." The variable is assigned the integer value 5.
Part 2 We display the minimum and maximum values of the byte type. These are 0 and 255.
Part 3 The typeof operator returns a managed pointer (System.Byte). The default value of byte is equal to zero.
typeof
default
using System; // Part 1: assign an integer to a byte variable. byte value = 5; Console.WriteLine(value); // Part 2: display minimum value and maximum value. Console.WriteLine(byte.MinValue); Console.WriteLine(byte.MaxValue); // Part 3: use Type pointer and default value. Console.WriteLine(typeof(byte)); Console.WriteLine(typeof(Byte)); // Uppercase Byte Console.WriteLine(default(byte));
5 0 255 System.Byte System.Byte 0
MinValue. The minimum value (byte.MinValue) is equal to zero. And the maximum value (byte.MaxValue) is equal to 255. A value greater than 255 will cause an overflow error.
OverflowException
using System; Console.WriteLine("Byte: {0}, {1}", byte.MinValue, byte.MaxValue); Console.WriteLine("Sbyte: {0}, {1}", sbyte.MinValue, sbyte.MaxValue);
Byte: 0, 255 Sbyte: -128, 127
Convert int to byte. Suppose we want to copy some ints to a byte array, and we want to be sure the ints can all be represented as bytes. We can use a checked block for the cast.
Step 1 We create a byte array. We will be storing ints in the byte array, but do not want any issues with over flow.
Step 2 We loop over a range of numbers. We will be storing these ints inside the byte array.
Step 3 We cast the int to a byte inside a checked statement. If it does not fit, an exception will occur, and we can fix the bug.
checked
using System; // Step 1: create empty byte array. byte[] array = new byte[100]; int write = 0; // Step 2: loop over some numbers. for (int i = 20; i < 40; i++) { // Step 3: safely cast int to byte. // ... Store byte in array. checked { array[write++] = (byte)i; } } Console.WriteLine("DONE");
DONE
Arguments. The C# compiler will treat some numbers as bytes in a program. So it passes the value 10 as a byte value. But it cannot treat 1000 as a byte, so it causes an error.
Tip The compiler uses a special type of error, a compile-time error, to prevent incorrect programs from ever running.
using System; class Program { static void Main() { // The 10 is automatically treated as a byte. Test(10); // This does not compile: // Test(1000); } static void Test(byte value) { Console.WriteLine(value); } }
10
Sbyte. This signed byte type represents a small integer that can be negative or positive. It is 8 bits or 1 byte and it stores integers between -128 and 127. It is less commonly used.
Info This program tests and increments as byte. It prints the size, and shows the minimum and maximum values.
And The program uses logic to return the Type and TypeCode associated with the sbyte type.
Tip You can assign and increment sbytes just like any other value type. An unassigned sbyte on the heap will be initialized to 0.
using System; sbyte val = 1; // Local variable. Console.WriteLine("val: {0}", val); Console.WriteLine("sizeof: {0}", sizeof(sbyte)); Console.WriteLine("default: {0}", default(sbyte)); Console.WriteLine("min: {0}", sbyte.MinValue); Console.WriteLine("max: {0}", sbyte.MaxValue); Console.WriteLine("type: {0}", val.GetType()); Console.WriteLine("code: {0}", val.GetTypeCode()); if (val == 1) // Test. { Console.WriteLine("1: {0}", true); } val++; // Increment. Console.WriteLine("val: {0}", val);
val: 1 sizeof: 1 default: 0 min: -128 max: 127 type: System.SByte code: SByte 1: True val: 2
Char. There are some types that are related to, or similar to, the byte type. In programming languages such as C or C++, the char type is only one byte.
However In .NET, the System.Char type is two bytes and requires twice as much storage.
char
sizeof
using System; Console.WriteLine("Byte = {0}", sizeof(byte)); Console.WriteLine("Char = {0}", sizeof(char));
Byte = 1 Char = 2
Byte variables can contain the values between 0 and 255. The type is a value type. A related type, sbyte can accommodate negative numbers as it has a sign bit.
Dot Net Perls is a collection of pages with code examples, which are updated to stay current. Programming is an art, and it can be learned from examples.
Donate to this site to help offset the costs of running the server. Sites like this will cease to exist if there is no financial support for them.
Sam Allen is passionate about computer languages, and he maintains 100% of the material available on this website. He hopes it makes the world a nicer place.
This page was last updated on Mar 8, 2025 (new example).
Home
Changes
© 2007-2025 Sam Allen