C# Byte (byte) and sbyte Variable Types

Byte and sbyte

Both Byte (byte) and sbyte are both number variables and they hold 8 bits of data, these are very compact variables which also makes them quite efficient too. The main difference between them is that on of them has a signed bit, while the other doesn't. What this means one can only hold positive numbers only while the other can hold negative and positive numbers.

Data Type Range
Byte 0 to 255
sbyte -128 to 127

As you can see from above table sbyte is the signed variable. As Bytes are not that useful on their own as single bytes, you will generally find that Byte Arrays are usually more useful.

You may of noticed that I have used 'Byte' and 'byte', there is not really any difference in the data type itself, 'byte' is just simply an alias for struct 'Byte' data type.

By default the data type byte cannot be null. You can force this to be null, if you wish.

Create byte or sbyte

It is recommended to use 'byte' rather than 'Byte' keyword, but either will be fine. Notice the code below that assigns byte variables to 0. To use the "Byte" struct, you must use "System.Byte".

byte a = 0;
System.Byte b = 0;

You can just use "Byte" but you will need to insert "using System;" above namespace.

using System;

namespace GetCoding
   class Program
      static void Main()
         byte a = 0;
         Byte b = 0;

By using "byte" you make your code easier and simpler.