how to have a method to return an uninitilized variable in c#?

L

Leon_Amirreza

Hi,

We have byValue and ByReference in VB and out and ref parameters in C#
1- ref parameter expects an initialized variable
2- out should be initialized before return

First Question: How can I have a parameter remain uninitialized after a
return (that compiler expects to be initialized even after the invokation of
the method)? (compiler designer might need to add a runtime error for
reading from an uninitialized variable)

Second Question: Somtimes compiler naggs about that not all code paths
return a value? how can I assure the compiler it always return a value?
 
P

Peter Duniho

Leon_Amirreza said:
Hi,

We have byValue and ByReference in VB and out and ref parameters in C#
1- ref parameter expects an initialized variable
2- out should be initialized before return

First Question: How can I have a parameter remain uninitialized after a
return (that compiler expects to be initialized even after the
invokation of the method)? (compiler designer might need to add a
runtime error for reading from an uninitialized variable)

Second Question: Somtimes compiler naggs about that not all code paths
return a value? how can I assure the compiler it always return a value?

Both of your questions can probably be best answered by directing you to
the C# specification, in which the rules for "definite assignment" are
found (see section "5.3 Definite Assignment").

The short versions:

1) C# has rules governing how the compiler can statically determine
whether a variable has been definitely assigned or not. If the compiler
cannot determine _statically_ (i.e. at compile time) that a variable has
been definitely assigned, you can't read the variable. Something that
requires a run-time error is simply not going to be part of the language.

Because of the possibility of delegate invocation or calling into a
different assembly, there's no way to reliably statically determine
definite assignment if you allow "out" arguments to be left unassigned
when the method returns.

2) If the compiler is unable to determine that a variable is
definitely assigned, then you need to assign it somewhere. Usually this
means given a reference type variable an initial value of "null" and a
value type variable an initial value of "new YourValueTypeNameHere()"
(with the obvious replacement for the actual type name).

And yes, this means your code may wind up with a bug that involves the
variable _really_ not being assigned a meaningful value before it's
used. But the C# compiler team has determined that it's just not
practical (or perhaps even possible) to include enough analysis to
_always_ know that a variable is definitely assigned.

At least in that case, you're no worse off than if you were using some
other language without definite assignment rules. :)

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top