Skip to content
Related Articles

Related Articles

Improve Article

sbyte Keyword in C#

  • Last Updated : 22 Jun, 2020

Keywords are the words in a language that are used for some internal process or represent some predefined actions. SByte is a keyword that is used to declare a variable that can store a signed value between the range of -128 to +127. It is an alias of System.SByte.

SByte keyword occupies 1 byte (8 bits) in the memory.

Syntax:

sbyte variable_name = value;

SByte keyword can store the value from the range of -128 to +127.

Example:



Input: -109

Output: num: -109
        Size of a sbyte variable: 1

Input: 110

Output: Type of num: System.SByte
        num: 110
        Size of a sbyte variable: 1

Example 1:




// C# program for sbyte keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        sbyte num = -109;
  
        // to print value
        Console.WriteLine("num: " + num);
  
        // to print size of a sbyte
        Console.WriteLine("Size of a sbyte variable: " + sizeof(sbyte));
    }
}

Output:

num: -109
Size of a sbyte variable: 1

Example 2:




// C# program for sbyte keyword
using System;
using System.Text;
  
namespace geeks {
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        sbyte num = 110;
  
        // to print type of variable
        Console.WriteLine("Type of num: " + num.GetType());
  
        // to print value
        Console.WriteLine("num: " + num);
  
        // to print size of sbyte 
        Console.WriteLine("Size of a sbyte variable: " + sizeof(sbyte));
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

Output:

Type of num: System.SByte
num: 110
Size of a sbyte variable: 1

Example 3:




// C# program for sbyte keyword
using System;
using System.Text;
  
class GFG {
  
    static void Main(string[] args)
    {
        // char variable declaration
        sbyte num = -189;
  
        // to print value
        Console.WriteLine("num: " + num);
  
        // to print size of a sbyte
        Console.WriteLine("Size of a sbyte variable: " + sizeof(sbyte));
    }
}

Error: When we enter number beyond the range from (-128-127).

Constant value `-189′ cannot be converted to a `sbyte’




My Personal Notes arrow_drop_up
Recommended Articles
Page :