Junior — Middle
What methods of stream data processing do you know?
sobes.tech AI
Answer from AI
In C#, the following methods and approaches are often used for processing streaming data:
- Streams — a fundamental way to work with sequential data, such as FileStream, MemoryStream. They allow reading and writing data in parts.
- Asynchronous read/write using
ReadAsyncandWriteAsyncmethods for non-blocking data processing. - Reactive Extensions (Rx) — a library for reactive programming that enables processing of event and data streams using LINQ-like operators (Select, Where, Buffer, etc.).
- IAsyncEnumerable<T> and the await foreach operator — for asynchronous iteration over data streams.
- Dataflow (TPL Dataflow) — a library for building data processing pipelines with blocks that can process data in parallel and asynchronously.
Example of asynchronous reading from a stream:
using (var stream = new FileStream("file.txt", FileMode.Open))
{
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
// Processing the read data
}
}