tags:

views:

497

answers:

6

Before I ask my question, please take a look at this example function:

DateTime.TryParse("01/01/2000", out oDate)

Why do I need to specify the out keyword? Shouldn't the compiler know this from the function's definition?

I'm asking this out of pure curiosity in the hope that I will learn something new about the compiler.

I should also clarify that I'm asking about the C# .NET 3.5 compiler in particular.

+9  A: 

The compiler does know, you may not. It's a way of letting you know that the parameter you are passing can change in this function you are passing it to.

Malfist
+2  A: 

Yeah the compiler could figure it out, but this way you know that it is going to modify the variable you are passing in.

the C# language has a lot of what I would call safety nets that explicitely tell the programmer what is going on. A couple of examples are:

  1. No fall through in switch statements.
  2. You can't assign a value in an if statement: if(x = 5) throws an error instead of evaluating to true.
Kevin
Technically #2 is incorrect. The technical definition is that the thing inside an if statement must resolve to a boolean value. x = 5 returns an int, which isn't a bool, so the compiler complains. However, the following is valid: bool foo = false, bar = false; if (foo = bar) { }
FryGuy
+16  A: 

The out keyword could be implied by the compiler but my understanding is that the C# team decided to make the out keyword explicitly required by the caller of the function to increase visibility as to the nature of the parameter.

Andrew Hare
They probably also decided on it because they wanted to make it as painful as possible to use it in order to discourage its use.
Michael Meadows
This was the best answer. Thanks :)
GateKiller
"because Anders Hejlsberg said so"
BCS
+6  A: 

It's not about what the compiler knows, it's all about making sure the developer realizes this call can and will change the value of variable X.

A lot of this has it's roots in C++ where a reference value needs no call site monitor. It's impossible to look at a C++ call and know exactly what it will do. Parameters passed by reference and value in C++ have huge differences in semantics.

JaredPar
+1  A: 

http://msdn.microsoft.com/en-us/library/t3c3bfhx(VS.80).aspx

"The out keyword causes arguments to be passed by reference. This is similar to the ref keyword, except that ref requires that the variable be initialized before being passed. To use an out parameter, both the method definition and the calling method must explicitly use the out keyword."

Since the DateTime.TryParse does not require oDate to be initialized, you must pass the out keyword.

Brandon
A: 

OK, I'm not a C# expert, so if I mess up will somebody please correct me?

There's two ways to pass a parameter to a C# function: by value and by reference. The big difference here is whether modifying the parameter inside the function modifies the variable used to call it. This is not something I'd trust the compiler to decide for itself.

Since you want oDate to be a variable passed in from the caller, and changed, you want it passed by reference.

The other question is whether it should be initialized or not. C# likes to catch when variables are used while uninitialized, since that's almost always an error. In this case, you might well just declare what you're passing in, and use TryParse() to give it its first value. This is a perfectly legitimate technique, so the compiler should allow it. This is another thing I wouldn't trust the compiler to get right. (I assume the compiler also checks to make sure an out parameter is initialized before use in TryParse().)

So, "out" serves two purposes. It establishes that the parameter is passed in by reference, and that it is expected to be initialized inside the function. Neither of these can be determined by the compiler.

David Thornley