Search code examples
c#filestream

Is merely creating a FileStream object expensive?


Suppose I just have this single line

var fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read);

Is this an expensive operation? Could this already be an I/O operation or not? Because I am nog reading from it by simply calling the constructor, or do I indirectly?

What I mean by expensive is just I/O wise. Does it do anything more than just being a POCO in memory?


Solution

  • It isn't just a POCO, since it involves an OS file handle (for here). As such, it should be avoided if you aren't actually planning to access data.