Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

โปรแกรม C# นับจำนวนไบต์ในอาร์เรย์


ภาพรวม

ใช้ Buffer.ByteLength() ดึง ขนาดรวมของอาร์เรย์เป็นไบต์ (จำนวนองค์ประกอบ × ขนาดต่อองค์ประกอบ)

รูปแบบการใช้งาน

byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };
int len = Buffer.ByteLength(b); // 8 (8 elements × 1 byte)

ตัวอย่างโค้ดเต็ม

using System;

class Program
{
    static void Main()
    {
        byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };
        int len = Buffer.ByteLength(b);
        for (int i = 0; i < len; i++) Console.WriteLine(b[i]);
        Console.WriteLine("Length of byte array = " + len);
    }
}

ผลลัพธ์

5
9
19
23
29
35
55
78
Length of byte array = 8

ข้อมูลเพิ่มเติม

ชนิดขนาด (ไบต์)Buffer.ByteLength(array)
byte[]1Length × 1
short[] / ushort[]2Length × 2
int[] / uint[] / float[]4Length × 4
long[] / ulong[] / double[]8Length × 8