Keywords are the words in a language that are used for some internal process or represent some predefined actions. byte is a keyword that is used to declare a variable which can store an unsigned value range from 0 to 255. It is an alias of System.Byte.
byte keyword occupies 1 byte (8 bits) in the memory.
Syntax:
byte variable_name = value;
Example:
Input: 250
Output: number: 250
Size of a byte variable: 1
Input: 150
Output: Type of num1: System.Byte
num1: 150
Size of a byte variable: 1
Example 1:
// C# program to show the usage of byte keyword using System; using System.Text; class GFG { static void Main(string[] args) { // byte variable declaration byte num = 255; // to print value Console.WriteLine("num: " + num); // to print size of a byte Console.WriteLine("Size of a byte variable: " + sizeof(byte)); } } |
Output:
num: 255 Size of a byte variable: 1
Example 2:
// C# program to show the usage of byte keyword using System; using System.Text; class GFG { static void Main(string[] args) { // byte variable declaration byte num1 = 261; // to print value Console.WriteLine("num1: " + num1); // to print size of a byte Console.WriteLine("Size of a byte variable: " + sizeof(byte)); } } |
Error: When we enter number beyond the range from (0-255).
Constant value `261' cannot be converted to a `byte'
Example 3:
// C# program to show the usage of byte keyword using System; using System.Text; namespace geeks { class GFG { static void Main(string[] args) { // byte variable declaration byte num1 = 150; // to print type of variable Console.WriteLine("Type of num1: " + num1.GetType()); // to print value Console.WriteLine("num1: " + num1); // to print size of a byte Console.WriteLine("Size of a byte variable: " + sizeof(byte)); // hit ENTER to exit Console.ReadLine(); } } } |
Output:
Type of num1: System.Byte num1: 150 Size of a byte variable: 1
Recommended Posts:
- C# | Byte.CompareTo(Byte) Method
- C# | Byte.Equals(Byte) Method
- How to implement is functionality without using is keyword in C#
- C# | this Keyword
- C# | finally keyword
- C# | as Operator Keyword
- C# | is Operator Keyword
- Is vs As operator keyword in C#
- Static keyword in C#
- typeof Operator Keyword in C#
- Invoking an overloaded constructor using this keyword in C#
- Difference between readonly and const keyword in C#
- LINQ | Let Keyword
- uint keyword in C#
- decimal keyword in C#
- char keyword in C#
- bool Keyword in C#
- sbyte Keyword in C#
- float keyword in C#
- double Keyword in C#
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

