c#中的long类型示例_C#中带示例的无符号字节数组
c#中的long類型示例
C#中的無符號字節數組 (Unsigned Byte Array in C#)
In C#.Net, we can create an unsigned byte array by using byte, byte is used to store only positive values between the range of 0 to 255 (Unsigned 8 bits integer).
在C#.Net中,我們可以使用byte創建一個無符號的字節數組, byte用于僅存儲0到255 (無符號的8位整數)范圍內的正值。
It occupies 1-byte memory for each element, if array size is 10, it will take 10 bytes memory.
每個元素占用1字節的內存 ,如果數組大小為10,則將占用10字節的內存。
聲明無符號字節[] (Declaration of a unsigned byte[])
1) Array declaration with initialization
1)初始化數組聲明
Syntax: byte[] array_name = { byte1, byte2, byte2, ...};Example: byte[] arr1 = { 0, 100, 120, 210, 255};Array decoration with fixed number of elements
具有固定數量元素的陣列裝飾
Syntax: byte[] array_name = new byte[value];Example: byte[] arr2 = new byte[5];3) Array declaration with user input
3)帶有用戶輸入的數組聲明
Syntax: byte[] array_name = new byte[variable];Example: byte[] arr2 = new byte[n];訪問無符號字節數組的元素 (Accessing unsigned byte array's elements)
Like other types of arrays – we can access the array elements with its index, index starts with 0 and ends with n-1. Here, n is the total number of array elements.
像其他類型的數組一樣,我們可以使用其索引訪問數組元素,索引以0開頭,以n-1結尾。 此處, n是數組元素的總數。
Example:
例:
Consider the given example – Here, we are declaring 3 arrays with 3 different approaches, initializing the arrays either with default values or user input. To print the array elements, we are using foreach loop, we can also use for or while loop with loop counter to access the array elements.
考慮給定的示例–在這里,我們使用3種不同的方法聲明3個數組,并使用默認值或用戶輸入來初始化數組。 為了打印數組元素,我們使用了foreach loop ,我們也可以使用帶有循環計數器的for或while循環來訪問數組元素。
using System; using System.Text;namespace Test {class Program{static void Main(string[] args){//declaring unsigned byte[] & initializing it with 5 elementsbyte[] arr1 = { 0, 100, 120, 210, 255};//printing all bytes of arr1Console.WriteLine("arr1 items...");foreach (byte item in arr1){Console.WriteLine(item);}Console.WriteLine(); //to print a line //declaring array for 5 elements //reading values and assigning to array byte[] arr2 = new byte[5];//reading values from the userfor (int loop = 0; loop < 5; loop++){Console.Write("Enter a byte (b/w -128 to 127): ");arr2[loop] = byte.Parse(Console.ReadLine());}//printing all bytes of arr2Console.WriteLine("arr2 items...");foreach (byte 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 elementsbyte[] arr3 = new byte[n];//reading values from the userfor (int loop = 0; loop < n; loop++){Console.Write("Enter a byte (b/w -128 to 127): ");arr3[loop] = byte.Parse(Console.ReadLine());}//printing all bytes of arr3Console.WriteLine("arr3 items...");foreach (byte item in arr3){Console.WriteLine(item);}//hit ENTER to exitConsole.ReadLine();}} }Output
輸出量
arr1 items... 0 100 120 210 255Enter a byte (b/w -128 to 127): 0 Enter a byte (b/w -128 to 127): 100 Enter a byte (b/w -128 to 127): 150 Enter a byte (b/w -128 to 127): 200 Enter a byte (b/w -128 to 127): 255 arr2 items... 0 100 150 200 255Enter length of the array: 3 Enter a byte (b/w -128 to 127): 0 Enter a byte (b/w -128 to 127): 225 Enter a byte (b/w -128 to 127): 255 arr3 items... 0 225 255翻譯自: https://www.includehelp.com/dot-net/unsigned-byte-array-with-example-in-c-sharp.aspx
c#中的long類型示例
總結
以上是生活随笔為你收集整理的c#中的long类型示例_C#中带示例的无符号字节数组的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: setpriority_Java Thr
- 下一篇: 最新大厂面试真题集锦