tags:

views:

324

answers:

2

Studying C#, my books are showing me classes for readin files. I've found 2 that are very similar and the Visual Studio debugger doesn't show an obvious difference between the two.

code:

FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);


FileStream fs = File.Open(path, FileMode.Open, FileAccess.Read);

Now I wonder, what's the difference between these 2 ways of reading a file. Is there any internal difference you know of?

+11  A: 

The latter is just a factory which returns an instance of FileStream. I.e. they do the same.

Here's the implementation for Open():

public static FileStream Open(string path, FileMode mode, FileAccess access, FileShare share) {

   return new FileStream(path, mode, access, share);

}
Brian Rasmussen
+3  A: 

If you read the documentation, you'll find they are the same.

Robert C. Barth