tags:

views:

274

answers:

4

This is in relation to this other SO question which asks how to overwrite an existing file.

The top answer is this:

FileStream file = File.Open("text.txt", FileMode.Create);

My answer was this:

FileStream fs = System.IO.File.Create(fileName);

As of when I wrote this question, the tally was 14-0 in favor of Open.

If votes are an indication of good versus bad solutions, this makes me wonder a bit:

Is there something I'm missing in these methods that would make it clearly that much better to choose Open over Create?

A: 

Well, I too answered with Create but Open really is the better solution as you are indicating that you wish to open a file and with the FileMode.Create you are indicating that you wish to create the file if it does not already exist. I think that is pretty clear.

Andrew Hare
I guess. But isn't the Create more succinct? Also, the Open has the flag FileMode.Create, which I think makes it a little more confusing: "I want to Open, but Create it." versus "I want to Create, I don't care what was there before." Is there any case where (other than these semantics) that Open would actually be better? I.e. Would Create have some other side-effects that Open does not?
Erich Mirabal
A: 

First, I think you might be reading too much into it. I, for instance, I tend to up-vote the first correct answer, and neglect any further answers... so getting there first helps a bit.

Second, File.Open reads better than System.IO.File.Open, even though they are the same.

Third, Create is not as semantically relevant as Open, from a readability standpoint. If you want to create AND open a file, the first is more explicit.

Brian Genisio
Maybe I am reading too much into it. I knew that by the time I finished typing the answer, somebody probably would've "beaten" me to the first response. What makes me wonder is that other "harder" or "interesting" questions may not get as much on the up-vote as that question. It was an easy question, so I typically wouldn't see voting that high for something that takes 2 seconds on google or msdn to answer. I guess the explicit wording of "Open" gives a little better semantics than "Create."
Erich Mirabal
@Brian: Simply voting up the first answer because it is the first answer is total nonsense. Good answers should be voted up, not first answers.
0xA3
@divo: I couldn't agree more. It is called laziness. Only being honest. I think it happens all the time, though :)
Brian Genisio
@divo: I upvoted your comment... I should turn a new leaf and be less lazy.
Brian Genisio
@divo: Just to be clear: I vote up the first CORRECT answer, not just the first answer. Not justifying the behavior... just clarifying.
Brian Genisio
A: 

To me, I know exactly what File.Open("...", FileMode.Create) does because I can hover over FileMode.Create and it tells me that is will create a new file each time. File.Create("...") has no such tool tip that indicates that it will do this.

Samuel
That actually made me laugh a little. Coding decision over a tooltip. Funny, but I guess it's as good of a discrimator as anything since it does provide more information, which is crucial for development. Seems random, but actually makes sense. Thanks :)
Erich Mirabal
+1  A: 

There only one place I know you could look for an answer to this one: Reflector

And it turns out both call new FileStream(... with a full set of arguments!

Stijn Sanders
Well, that's exactly what I am saying. They both end up calling the same code. I guess it is a matter of people liking 'Open' over 'Create' since it makes it a little more obvious that you are getting back a file that is open and ready to go.
Erich Mirabal