GateKiller
GateKiller

Reputation: 75959

Why do I have to specify the "out" keyword in C#?

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.

Upvotes: 4

Views: 636

Answers (6)

David Thornley
David Thornley

Reputation: 57066

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.

Upvotes: 0

nabrond
nabrond

Reputation: 1388

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.

Upvotes: 1

JaredPar
JaredPar

Reputation: 755327

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.

Upvotes: 6

Andrew Hare
Andrew Hare

Reputation: 351616

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.

Upvotes: 18

kemiller2002
kemiller2002

Reputation: 115538

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.

Upvotes: 2

Malfist
Malfist

Reputation: 31815

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.

Upvotes: 10

Related Questions