C# and .NET Basics

Streams

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "Streams" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer introduces the concept of streams as an abstraction of a sequence of bytes. He explains that streams are commonly used for handling input and output in .NET and provides examples of different types of streams, such as file streams and memory streams.

Preview
Close

Transcript from the "Streams" Lesson

[00:00:00]
>> vSpencer Schneidenbach: Streams, and I just love this phrase from the Microsoft docs, an abstraction of a sequence of bytes. So, streams are everywhere in our world, and lots of times those streams are abstracted away from us in the form of a byte array, or in the form of an object that we can just read from.

[00:00:17]
So, sometimes a stream gets open, we don't know how many bytes are coming through, right? We just don't know, necessarily, how many bites are coming from Google. Their content length header inside their HTTP response could be lying to us, so, we treat that as a stream of bytes coming all the way down.

[00:00:36]
And they are fundamental concepts for handling input and output inside of .NET. You don't have to understand them super, super deeply for the purposes of this course, or the ASP.NET course, but it's important to understand that streams exist. It's just, basically, an array of bytes that's moving along a pipe, so it's water moving along a pipeline.

[00:00:56]
You don't wait till the water gets to the end sometimes, sometimes you wanna process that water, drain that water off, or do something with that water as the water is flowing through the pipe. And that's what essentially a stream is, so there's a few streams that are common.

[00:01:09]
We're gonna be focusing mainly on FileStreams, and an object called MemoryStream, those are probably the more important streams to talk about. So, when you use a FileStream, you're essentially telling the file system, cuz you're on a Windows machine, you're on a Mac, and you wanna open a file and do something with it, right?

[00:01:26]
You wanna do something, you wanna write to it, maybe you're flushing to a log. Maybe you're processing an incoming document and you need to write a report, and you need to drop that report into a PDF. So, a FileStream is a way for you to open up a connection to that file, take an arbitrary number of bytes, and that's important.

[00:01:44]
Streams, essentially an arbitrary number of bytes, and then write it to that file, and then be done. So imagine, instead, if you took a 50 megabyte file and you read it all into memory first. You just downloaded it, very much like we did here, just downloaded the whole thing in the memory.

[00:02:06]
For downloading, google.com probably doesn't matter that much, right? It's probably not gonna be more than a few megabytes, I don't know how much it's gonna be, the page itself, probably not even that. But when we're talking about large files, maybe it's a movie, maybe it's several photos. Maybe it's a ZIP file of several photos, and you have an application that's processing those.

[00:02:25]
Do you really want to take all of those bytes, like this, and put them into memory right away? And then just have 50 megabytes, or 500 megabytes, or an arbitrary number of megabytes hanging out of memory? Probably not, so it's best to stream those things to their final destination.

[00:02:41]
So, what you can do, for instance, is open up an HTTP request, take that stream, and then open up a file on the file system. And take those bytes as they're being downloaded, and put them into the file system, so that way you skip putting that into memory.

[00:02:56]
I see this mistake, I bring this up because it sounds obvious, but I see this mistake all the time in .NET developers. .NET has lots of constructs for dealing with streams, and most of the time they convert and use those streams as byte arrays. So, FileStreams, either reading from or writing to files, are very easy.

[00:03:16]
Notice that our streams are all using using statements, and that is because a stream is an unmanaged resource, at least it can be a representation of an unmanaged resource. So, we need to make sure that we dispose of said resource at the end of the time that we're using it.

[00:03:36]
Because otherwise, if we didn't, we'll keep this file open, we'll keep our hand at what I`d call a handle on a file, we'll keep that locked, and maybe other people can't access it, or other programs. This is probably the most common stream that I see, is MemoryStream. Which is to say that it is, literally as it sounds, a stream of data, but it's fully in-memory, so you get an arbitrary number of bytes, it's essentially a byte array.

[00:04:02]
And MemoryStreams, like I mentioned, are super useful, but if not a little overused. It's oftentimes, I see a lot of developers put things into memory when they don't need to be. Though it's useful for in-memory storage, for small amounts of data, or if you don't wanna use a file for whatever reason, but I tend to find that MemoryStream gets overused.

[00:04:25]
However, you need to know that it exists as a way to represent a stream of bytes, you can represent it in memory as a MemoryStream. I wanna call out one last thing, kinda going back to that example, of what I'll see people do. They'll take a file, they'll download it, they might do something with it, some processing, they might do that in-memory, but that's usually not the best way.

[00:04:51]
Oftentimes, when I am doing applications, where I'm writing a program that needs to read some data on some remote company's machine. It's a company we've contracted with, and we wanna be good stewards of their data, right, and their resources. So, we'll open up a file handle and write to a temporary file to write some data, and then delete it without using MemoryStream.

[00:05:13]
So, as I mentioned, there's things you need to know about streams, mainly that they exist. We'll talk about them a little more in the ASP.NET Core course, but they're just something that you kinda need to be aware of as you're going down your programming journey.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now