C# Signed Byte Array: Here, we will find out about the signed byte array in C# – how to announce, how to instate and how to get to components?
Signed Byte Array in C#
In C#.Net, we can make a signed byte array by utilizing sbyte, sbyte is utilized to store both of the qualities (negative and positive) between the scope of – 128 to 127 (Signed 8 bits integer).
It involves 1-byte memory for every component, if the array size is 10, it will take 10 bytes memory.
Statement of a signed byte[]
1) Array statement with instatement
Syntax: sbyte[] array_name = { byte1, byte2, byte2, ...};
Example: sbyte[] arr1 = { -128, -100, 0, 100, 127};
2) Array statement with fixed number
Syntax: sbyte[] array_name = new sbyte[value];
Example: sbyte[] arr2 = new sbyte[5];
3) Array declaration with user input
Syntax: sbyte[] array_name = new sbyte[variable];
Example: sbyte[] arr3 = new sbyte[n];
Getting to signed byte array’s components
Like different sorts of arrays – we can get to the array components with its file, the list begins with 0 and closures with n-1. Here, n is the all outnumber of array components.
Example:
Think about the given example – Here, we are proclaiming 3 arrays with 3 unique methodologies, instating the arrays either with default esteems or client input. To print the array components, we are utilizing foreach loop, we can likewise use for or while loop with loop counter to get to the array components.
using System;
using System.Text;
namespace Test
{
class Program
{
static void Main(string[] args)
{
//declaring signed byte[] & initializing it with 5 elements
sbyte[] arr1 = { -128, -100, 0, 100, 127 };
//printing all bytes of arr1
Console.WriteLine("arr1 items...");
foreach (sbyte item in arr1)
{
Console.WriteLine(item);
}
Console.WriteLine(); //to print a line
//declaring array for 5 elements
//reading values and assigning to array
sbyte[] arr2 = new sbyte[5];
//reading values from the user
for (int loop = 0; loop < 5; loop++)
{
Console.Write("Enter a byte (b/w -128 to 127): ");
arr2[loop] = sbyte.Parse(Console.ReadLine());
}
//printing all bytes of arr2
Console.WriteLine("arr2 items...");
foreach (sbyte item in arr2)
{
Console.WriteLine(item);
}
Console.WriteLine(); //to print a line
//read value of "n" and declare array for "n" elements
//reading values and assigning to array
Console.Write("Enter length of the array: ");
int n = int.Parse(Console.ReadLine());
//declaring array for n elements
sbyte[] arr3 = new sbyte[n];
//reading values from the user
for (int loop = 0; loop < n; loop++)
{
Console.Write("Enter a byte (b/w -128 to 127): ");
arr3[loop] = sbyte.Parse(Console.ReadLine());
}
//printing all bytes of arr3
Console.WriteLine("arr3 items...");
foreach (sbyte item in arr3)
{
Console.WriteLine(item);
}
//hit ENTER to exit
Console.ReadLine();
}
}
}
Output:
arr1 items...
-128
-100
0
100
127
Enter a byte (b/w -128 to 127): 127
Enter a byte (b/w -128 to 127): 100
Enter a byte (b/w -128 to 127): -100
Enter a byte (b/w -128 to 127): 0
Enter a byte (b/w -128 to 127): 20
arr2 items...
127
100
-100
0
20
Enter length of the array: 3
Enter a byte (b/w -128 to 127): -128
Enter a byte (b/w -128 to 127): 0
Enter a byte (b/w -128 to 127): 127
arr3 items...
-128
0
127