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 elements
            byte[] arr1 = { 0, 100, 120, 210, 255};
            
            //printing all bytes of arr1
            Console.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 user
            for (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 arr2
            Console.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 elements
            byte[] arr3 = new byte[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] = byte.Parse(Console.ReadLine());
            }
            //printing all bytes of arr3
            Console.WriteLine("arr3 items...");
            foreach (byte item in arr3)
            {
                Console.WriteLine(item);
            }

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}

Output

输出量

arr1 items...
0
100
120
210
255

Enter 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
255

Enter 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类型示例

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值