[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

microsoft.public.dotnet.framework.sdk

C# Initializations of Variables

Uwe Kuehnlein

10/16/2003 10:15:00 AM

Hi,
in the past I used Visual Basic 6.0 and Visual C++. In my
opinion one of the advantages of VB is the initialization
of variables with 0. To decide between the languages
VB.NET and C# I want to know how C# initializes. I read in
MSDN, C# Specification, Default Values, that static
variables, instance variables and array elements are
initialized. So is it right that the others, like a local
variable of a class method, are not initialized?
1 Answer

(Mattias Sjögren)

10/16/2003 12:17:00 PM

0

Uwe,

>So is it right that the others, like a local
>variable of a class method, are not initialized?

It depends. :-)

From the C# language point of view, most local variables are not
automatically initialized, you have to do that explicitly. You can
read more about the definite assignment rules in the language spec

http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpsp...

However, methods in C# binaries compiled without the /unsafe option
will all have the "init" flag set, which you can see if you look at
the method with ILDASM

..locals init (...)

The init flag is required for code to be verifiable, and it causes the
runtime to initialize the locals. If you compile with /unsafe and
render the code unverifiable anyway, the init flag will not be set.



Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.n...
Please reply only to the newsgroup.