using System; using System.IO; class Program { static void Main(string[] args) { try { FileStream myFile = new FileStream("data.bin", FileMode.Open); int byteRead = myFile.ReadByte(); Console.WriteLine("The first byte read from the file is: " + byteRead); myFile.Close(); } catch (Exception e) { Console.WriteLine("Exception: " + e.Message); } } }
using System; using System.IO; class Program { static void Main(string[] args) { byte[] byteArray = new byte[]{1, 2, 3, 4, 5}; MemoryStream memoryStream = new MemoryStream(byteArray); int byteRead = memoryStream.ReadByte(); Console.WriteLine("The first byte read from the memory stream is: " + byteRead); memoryStream.Close(); } }This code creates a byte array with 5 elements, creates a memory stream from it, reads the first byte using ReadByte(), and displays the value on the console. The memory stream is closed after reading. The System.IO namespace is a part of the .NET Framework library.