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:
using System;
using System.Text;
class GFG {
static void Main( string [] args)
{
byte num = 255;
Console.WriteLine( "num: " + num);
Console.WriteLine( "Size of a byte variable: " + sizeof ( byte ));
}
}
|
Output:
num: 255
Size of a byte variable: 1
Example 2:
using System;
using System.Text;
class GFG {
static void Main( string [] args)
{
byte num1 = 261;
Console.WriteLine( "num1: " + num1);
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:
using System;
using System.Text;
namespace geeks {
class GFG {
static void Main( string [] args)
{
byte num1 = 150;
Console.WriteLine( "Type of num1: " + num1.GetType());
Console.WriteLine( "num1: " + num1);
Console.WriteLine( "Size of a byte variable: " + sizeof ( byte ));
Console.ReadLine();
}
}
}
|
Output:
Type of num1: System.Byte
num1: 150
Size of a byte variable: 1
Last Updated :
22 Jun, 2020
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...