Is there a default implementation for a readonly stream in .NET?

You can use the constructor new MemorySream(byte[] buffer, bool writeable) (documentation).

Setting the writeable parameter to false will make the stream readonly.


Such a stream does not exist in the BCL. You have to write it. In my life I have implemented about a dozen such streams and it is not too bad. The 2nd one is much easier because you can use the first one as a template.

I recommend that you inherit from Stream and not from some other stream. If you were inheriting from MemoryStream you'd abuse inheritance to save code which is not its primary purpose. Your derived stream would not work like a MemoryStream and it is-not a MemoryStream.

Prefer composition over inheritance.

Tags:

C#

.Net

Stream