What is a Byte and SByte

Both Byte (byte) and sbyte (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.

Usage of the variable type

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”, this is the same for ‘SByte’ and other variable types.

byte a = 0; 
System.Byte b = 0; 
sbyte a = 0; 
System.SByte 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; sbyte c = 0; 
         SByte d = 0; 
      }
   } 
}

By using “byte” you make your code easier, simpler and can be classed as the standard. However you could also use ‘var’ variable keyword but you would need to cast as byte, due to the standard number being an integer type.

{ 
   byte a = 0; 
   var b = 0; // This defaults to int. 
   var c = (byte)0; // 'var' is of type 'byte' due to casting 
   var d = new byte(); // This is byte with default value of 0 
}

The above is the same for ‘sbtye’ type.

Properties of Byte and SByte

There are two properties that you can use, these are MaxValue and MinValue, These can also be used with other primitive types like Int:

Type MinValue MaxValue
byte 0 255
sbyte -128 127

Code example:

using System; 

namespace GetCoding 
{ 
   class Program 
   { 
      static void Main() 
      { 
         byte byteMinValue = byte.MinValue; 
         byte byteMaxValue = byte.MaxValue; 
         sbyte sbyteMinValue = sbyte.MinValue; 
         sbyte sbyteMaxValue = sbyte.MaxValue; 
         Console.WriteLine("byte MinValue:" + byteMinValue.ToString()); 
         Console.WriteLine("byte MaxValue:" + byteMaxValue.ToString()); 
         Console.WriteLine("sbyte MinValue:" + sbyteMinValue.ToString()); 
         Console.WriteLine("sbyte MaxValue:" + sbyteMaxValue.ToString()); 
         Console.ReadKey(); 
      } 
   } 
}

Output produced from above code sample:

byte Min and Max ValueAs you can see MaxValue assigns the max that the variable type can hold, and MinValue outputs the smallest value.

Overflow

By default, overflow for byte and sbyte is disabled. See Overflow Exception.