C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Here: The program performs a series of accesses to a byte variable on the evaluation stack. The variable is assigned the integer value 5.
Typeof: The typeof operator returns a managed pointer. The expression typeof(byte) is equal to System.Byte.
Typeof, nameofDefault: The default value of byte is equal to zero. If a byte is a class field, we do not need to initialize it to zero.
DefaultC# program that uses byte type
using System;
class Program
{
static void Main()
{
// Assign an integer to a byte variable.
byte value = 5;
Console.WriteLine(value);
// The byte type includes a minimum value and maximum value.
Console.WriteLine(byte.MinValue);
Console.WriteLine(byte.MaxValue);
// The byte type has a Type pointer and default value.
Console.WriteLine(typeof(byte));
Console.WriteLine(typeof(Byte)); // Uppercase Byte
Console.WriteLine(default(byte));
}
}
Output
5
0
255
System.Byte
System.Byte
0
System.Byte information:
byte.MinValue = 0
byte.MaxValue = 255
System.SByte information:
sbyte.MinValue = -128
sbyte.MaxValue = 127
Step 1: We create a byte array. We will be storing ints in the byte array, but do not want any issues with over flow.
Step 2: We loop over a range of numbers. We will be storing these ints inside the byte array.
Step 3: We cast the int to a byte inside a checked statement. If it does not fit, an exception will occur, and we can fix the bug.
CheckedC# program that converts int to byte safely
using System;
class Program
{
static void Main()
{
// Step 1: create empty byte array.
byte[] array = new byte[100];
int write = 0;
// Step 2: loop over some numbers.
for (int i = 20; i < 40; i++)
{
// Step 3: safely cast int to byte.
// ... Store byte in array.
checked
{
array[write++] = (byte)i;
}
}
Console.WriteLine("DONE");
}
}
Output
DONE
Tip: The compiler uses a special type of error, a compile-time error, to prevent incorrect programs from ever running.
C# program that uses byte arguments
using System;
class Program
{
static void Main()
{
// The 10 is automatically treated as a byte.
Test(10);
// This does not compile:
// Test(1000);
}
static void Test(byte value)
{
Console.WriteLine(value);
}
}
Output
10
Info: This program tests and increments as byte. It prints the size, and shows the minimum and maximum values.
And: The program uses logic to return the Type and TypeCode associated with the sbyte type.
Tip: You can assign and increment sbytes just like any other value type. An unassigned sbyte on the heap will be initialized to 0.
C# program that uses sbyte type
using System;
class Program
{
static void Main()
{
sbyte val = 1; // Local variable.
Console.WriteLine("val: {0}", val);
Console.WriteLine("sizeof: {0}", sizeof(sbyte));
Console.WriteLine("default: {0}", default(sbyte));
Console.WriteLine("min: {0}", sbyte.MinValue);
Console.WriteLine("max: {0}", sbyte.MaxValue);
Console.WriteLine("type: {0}", val.GetType());
Console.WriteLine("code: {0}", val.GetTypeCode());
if (val == 1) // Test.
{
Console.WriteLine("1: {0}", true);
}
val++; // Increment.
Console.WriteLine("val: {0}", val);
}
}
Output
val: 1
sizeof: 1
default: 0
min: -128
max: 127
type: System.SByte
code: SByte
1: True
val: 2
Tip: Because both sbyte and byte represent 8 bits, you can store the bits in either type with no data loss.
Tip 2: If a program uses signed bytes, though, the data will make more sense if you treat one of the bits as a sign bit by using sbyte.
Note: Thanks to Akilram Krishnan for noticing an error in the SByte data at the top of the page.
However: In the .NET Framework, the System.Char type is two bytes and requires twice as much storage.
CharTip: The ushort type is another small integer type. It is best to use the smallest type that fits your range of data.
short, ushort