Differences between byte and sbyte in C#

C# byte and sbyte: Here, we will find out about the contrasts among byte and sbyte in C#.

C# byte and C# sbyte:

A solitary byte can store 8-bits esteem. Both are utilized for byte kind of information, for example, the information that contains a lone 1-byte esteem.

a byte is utilized to work with unsigned byte information, it works with a lone positive incentive between in the scope of 0 to 255.

sbyte is utilized to work with the marked byte information, it works with the two sorts of information (Negative and Positive), it can store the between in the scope of – 128 to 127.

The difference among ‘byte’ and ‘sbyte:

bytesbyte
byte stands for an unsigned byte.sbyte stands for signed byte.
It can store positive bytes only. It can store negative and positive bytes.
It takes 8-bits space in the memory. It also takes the 8-bits in memory.
Its data range is 0 to 255 that means it can store a minimum value 0 and maximum upto 255. Its data range is -128 to 127 that means it can store a minimum value -128 and maximum upto 127.
Declaration syntax:
byte variable;
Declaration syntax:
sbyte variable;

Example:

Announcing marked and unsigned byte variable, doling out them with the qualities and printing the qualities.

using System;
using System.Text;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            byte a;
            sbyte b;

            //printing minimum & maximum values
            Console.WriteLine("Minimum value of byte: " + byte.MinValue);
            Console.WriteLine("Maximum value of byte: " + byte.MaxValue);

            Console.WriteLine("Minimum value of sbyte: " + sbyte.MinValue);
            Console.WriteLine("Maximum value of sbyte: " + sbyte.MaxValue);
            a = 0;
            Console.WriteLine("a = " + a);
            a = 255;
            Console.WriteLine("a = " + a);

            b = -100;
            Console.WriteLine("b = " + b);
            
            b = 123;
            Console.WriteLine("b = " + b);
            
            b = 127;
            Console.WriteLine("b = " + b);


            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output:

Minimum value of byte: 0
Maximum value of byte: 255
Minimum value of sbyte: -128
Maximum value of sbyte: 127
a = 0
a = 255
b = -100
b = 123
b = 127

Leave a Comment

error: Alert: Content is protected!!