Re: I think C# is forcing us to write more (redundant) code




"Jon Skeet [C# MVP]" <skeet@xxxxxxxxx> wrote in message
news:MPG.1d7d547cf2bc28e998c623@xxxxxxxxxxxxxxxxxxxxxxx
> Bob Powell [MVP] <bob@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
>> Creation and assignment of variables is essentially the same for VB and
>> C#.
>> Value types are assigned their normal default values. For example you
>> don't
>> have to explicitly write:
>>
>> int x = new int(12);
>>
>> and if you just use "int x;" then x contains zero.
>>
>> Reference types are assigned null by the framework too.
>
> No, that's not true for local variables. Local variables aren't
> assigned to anything by default, and can't be read until they've been
> definitely assigned.
>
> --
> Jon Skeet - <skeet@xxxxxxxxx>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too

Actualy they are, the IL directive .locals init, emitted by C# , VB.NET and
ME C++, is a hint for the JIT to initialize the locals (stack as well as
register allocated) to 0 (zero).


Willy.


.



Relevant Pages

  • Re: I think C# is forcing us to write more (redundant) code
    ... Creation and assignment of variables is essentially the same for VB and C#. ... > If you do not specify an initialization value for a variable, ... > mechanism to have all local variables have default ... moving this to a warning will guarantee that there's ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Readonly locals?
    ... int _p; ... Find great Windows Forms articles in Windows Forms Tips and Tricks ... it would be an error to place it on the left of an assignment or to ... I searched this newsgroup for any mention of readonly local variables, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How can I find the remainder when dividing 2 integers
    ... UnboundLocalError: ... pretty much of what it says: You tried to access a variable without prior assignment to it. ... Python's scoping rules treat x and z as local variables, ... the scoping rules treat y as a local variable, ...
    (comp.lang.python)
  • Re: automatic variables are not initialized correctly?
    ... and assigned a value by an assignment statement ... standard, different compilers had produced different results, so the ... standard stated that neither interpretation could be trusted. ... sub-programs assign a value to unSAVEd local variables. ...
    (comp.lang.fortran)
  • Re: unclear new Object declaration
    ... l5: Manager manager =null; ... is all that line 5 is declaring is a new instance of "Manager" and ... Local variables must be initialized or assigned to before being used. ... Variables may also be declared before a meaningful assignment is possible, ...
    (comp.lang.java.programmer)

Loading