C# program to count number of bytes in an array


Set a byte array −

byte[] b = { 5, 9, 19, 23, 29, 35, 55, 78 };

To count number of bytes −

Buffer.ByteLength(b)

The following is the code −

Example

 Live Demo

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);
   }
}

Output

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

Updated on: 22-Jun-2020

945 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements