HomeSearch

C# Byte and sbyte Types

Use the byte and sbyte number types. Byte requires 8 bits and represents the numbers 0 to 255.

Byte.

A byte is 8 bits. The byte type is a compact and efficient type. Byte arrays are useful more often than single bytes. They can store file data. Sometimes bits are even more efficient.Byte Array

Byte versus sbyte.

To make matters more complex, a byte has no sign bit, but an sbyte does. We can use an sbyte when negative numbers are needed.
System.Byte information: byte.MinValue = 0 byte.MaxValue = 255 System.SByte information: sbyte.MinValue = -128 sbyte.MaxValue = 127

First example.

Byte is a value type, which means its storage location is directly contained in the evaluation stack when used as a local variable.

Tip: You can assign the byte variable any value between 0 and 255 without an error.

Info: The program proves this by demonstrating the result of several expressions related to the byte type.

Here: The program performs a series of accesses to a byte variable on the evaluation stack. The variable is assigned the integer value 5.

C# program that uses byte type using System; class Program { static void Main() { // You can assign an integer to a byte variable. byte value = 5; Console.WriteLine(value); // The byte type includes a minimum value and maximum value. Console.WriteLine(byte.MinValue); Console.WriteLine(byte.MaxValue); // The byte type has a Type pointer and default value. Console.WriteLine(typeof(byte)); Console.WriteLine(typeof(Byte)); // Uppercase Byte Console.WriteLine(default(byte)); } } Output 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

Checked: The behavior during an overflow error is determined by the overflow context, such as a checked or unchecked context.

Checked

Typeof.

The typeof operator returns a managed pointer. This pointer describes the type of the argument. The expression typeof(byte) is equal to System.Byte.Typeof, nameof

Default.

The default value of any value type such as byte is equal to zero. If a byte is a class field, we do not need to initialize it to zero.Default

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.

Compile-Time Error
C# program that uses byte arguments 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); } } Output 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.

Sbyte example.

This program first tests and increments a sbyte local variable. It next prints the size in bytes of the sbyte. It shows the minimum and maximum values.

And: The program uses logic to return the Type and TypeCode associated with the sbyte type.

C# program that uses sbyte type using System; class Program { static void Main() { 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); } } Output val: 1 sizeof: 1 default: 0 min: -128 max: 127 type: System.SByte code: SByte 1: True val: 2

Sbyte notes.

You can assign and increment sbytes just like any other value type. An unassigned sbyte on the heap will be initialized to 0.

Sbyte discussion.

The sbyte type doesn't provide any low-level functionality that the byte type doesn't. But it can improve interoperability with other libraries.

Tip: Because both sbyte and byte represent 8 bits, you can store the bits in either type with no data loss.

Tip 2: If a program uses signed bytes, though, the data will make more sense if you treat one of the bits as a sign bit by using sbyte.

Note: Thanks to Akilram Krishnan for noticing an error in the SByte data at the top of the page.

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 the .NET Framework, the System.Char type is two bytes and requires twice as much storage.

Char

Tip: The ushort type is another small integer type. It is best to use the smallest type that fits your range of data.

short, ushort

BitArray.

Bytes have eight bits. But if we only need a true or false value, we can use a single bit. A BitArray can greatly reduce memory usage over a byte array.BitArray

A review.

Byte variables can contain the values between 0 and 255. The type is a value type. Sbyte meanwhile can accommodate negative numbers.
Home
Dot Net Perls
© 2007-2019 Sam Allen. All rights reserved. Written by Sam Allen, info@dotnetperls.com.