What are streams in C#?

๐Ÿ’ก Concept: Streams in C#

Streams are abstract representations of sequences of bytes used for reading and writing data sequentially.

๐Ÿ“˜ Quick Intro

Streams provide a uniform way to handle data input and output from different sources like files, memory, and network connections.

๐Ÿง  Analogy

Think of streams as a conveyor belt moving data one piece at a time between producer and consumer.

๐Ÿ”ง Technical Explanation

  • ๐Ÿ”„ Streams support sequential read/write operations.
  • ๐Ÿ“‚ Types include FileStream, MemoryStream, NetworkStream, etc.
  • โš™๏ธ Streams can be synchronous or asynchronous.
  • ๐Ÿ› ๏ธ Properly closing streams is essential to release resources.
  • ๐Ÿ“š Streams implement IDisposable for cleanup.

๐ŸŽฏ Use Cases

  • โœ… Reading and writing files.
  • โœ… Handling network data.
  • โœ… Memory manipulation.
  • โœ… Data compression and encryption streams.

๐Ÿ’ป Code Example


using System;
using System.IO;

class Program {
    static void Main() {
        using (FileStream fs = new FileStream(""example.txt"", FileMode.OpenOrCreate)) {
            byte[] info = new byte[] { 1, 2, 3, 4, 5 };
            fs.Write(info, 0, info.Length);
            fs.Seek(0, SeekOrigin.Begin);
            byte[] buffer = new byte[info.Length];
            fs.Read(buffer, 0, buffer.Length);
            Console.WriteLine(string.Join("","", buffer));
        }
    }
}

โ“ Interview Q&A

Q1: What is a stream?
A: An abstraction for reading/writing sequential data.

Q2: Name some stream types.
A: FileStream, MemoryStream, NetworkStream.

Q3: Why close streams?
A: To release resources and avoid leaks.

Q4: Can streams be asynchronous?
A: Yes.

Q5: What interface do streams implement?
A: IDisposable.

Q6: How to read from a stream?
A: Using Read or ReadAsync methods.

Q7: How to write to a stream?
A: Using Write or WriteAsync methods.

Q8: Can streams be used for networking?
A: Yes, NetworkStream is designed for this.

Q9: Are streams buffered?
A: Often, to improve performance.

Q10: What is FileMode?
A: Enum for file open modes.

๐Ÿ“ MCQs

Q1. What is a stream?

  • Thread
  • Stream
  • Task
  • Process

Q2. Name some stream types.

  • Thread, Task
  • FileStream, MemoryStream
  • Socket, Process
  • File, Directory

Q3. Why close streams?

  • No reason
  • Release resources
  • Avoid bugs
  • Increase speed

Q4. Can streams be asynchronous?

  • No
  • Yes
  • Sometimes
  • Never

Q5. What interface do streams implement?

  • IEnumerable
  • IDisposable
  • IComparable
  • ICloneable

Q6. How to read from a stream?

  • Write
  • Close
  • Read or ReadAsync
  • Flush

Q7. How to write to a stream?

  • Read
  • Write or WriteAsync
  • Close
  • Flush

Q8. Can streams be used for networking?

  • No
  • Yes
  • Sometimes
  • Never

Q9. Are streams buffered?

  • Never
  • Often
  • Sometimes
  • Always

Q10. What is FileMode?

  • FileMode
  • FileAccess
  • FileShare
  • FileOptions

๐Ÿ’ก Bonus Insight

Streams provide a flexible and efficient way to handle I/O across various sources in C#.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Learn More About C# ๐Ÿ“š

1. What is C#? ๐Ÿ‘‰ Explained
2. Main Features of C# ๐Ÿ‘‰ Explained
3. Difference Between C# and Java ๐Ÿ‘‰ Explained
4. Common Language Runtime (CLR) in C# ๐Ÿ‘‰ Explained
5. Common Type System (CTS) in C# ๐Ÿ‘‰ Explained
6. Common Language Specification (CLS) in C# ๐Ÿ‘‰ Explained
7. Value Types vs Reference Types in C# ๐Ÿ‘‰ Explained
8. What is a Namespace in C#? ๐Ÿ‘‰ Explained
9. Purpose of the 'using' Keyword in C# ๐Ÿ‘‰ Explained
10. Different Data Types in C# ๐Ÿ‘‰ Explained
11. Difference Between int and Int32 in C# ๐Ÿ‘‰ Explained
12. Difference Between float, double, and decimal in C# ๐Ÿ‘‰ Explained
13. What is the Default Value of a Boolean in C#? ๐Ÿ‘‰ Explained
14. What is Boxing and Unboxing in C# ๐Ÿ‘‰ Explained
15. What are the Different Types of Operators in C# ๐Ÿ‘‰ Explained
16. Difference Between Equals and == in C# ๐Ÿ‘‰ Explained
17. What is the Null-Coalescing Operator ?? in C# ๐Ÿ‘‰ Explained
18. What is the Ternary Operator in C# ๐Ÿ‘‰ Explained
19. How Does the Switch Statement Work in C# ๐Ÿ‘‰ Explained
20. What is Object-Oriented Programming in C# ๐Ÿ‘‰ Explained
21. What are the Four Pillars of OOP in C# ๐Ÿ‘‰ Explained
22. What is Encapsulation in C# ๐Ÿ‘‰ Explained
23. What is Inheritance in C# ๐Ÿ‘‰ Explained
24. What is Polymorphism in C# ๐Ÿ‘‰ Explained
25. What is Abstraction in C# ๐Ÿ‘‰ Explained
26. What is an Abstract Class in C# ๐Ÿ‘‰ Explained
27. What is an Interface in C# ๐Ÿ‘‰ Explained
28. Can a Class Implement Multiple Interfaces in C#? ๐Ÿ‘‰ Explained
29. Difference Between Abstract Class and Interface in C# ๐Ÿ‘‰ Explained
30. How Do You Create a Class in C#? ๐Ÿ‘‰ Explained
31. What is a Constructor in C# ๐Ÿ‘‰ Explained
32. What Are the Types of Constructors in C# ๐Ÿ‘‰ Explained
33. What is a Static Constructor in C# ๐Ÿ‘‰ Explained
34. Difference Between Static and Non-Static Members in C# ๐Ÿ‘‰ Explained
35. What is the Use of 'this' Keyword in C# ๐Ÿ‘‰ Explained
36. What is a Destructor in C# ๐Ÿ‘‰ Explained
37. What is Object Initializer Syntax in C# ๐Ÿ‘‰ Explained
38. What is the Difference Between Field and Property in C# ๐Ÿ‘‰ Explained
Share:

Tags:


Feedback Modal Popup