Unassigned variable value not subject to nullification (C #)

Just curious.

If you go:

string myString; 

Its value is null.

But if you go:

 int myInt; 

What is the value of this variable in C #?

thanks

David

+7
c #
source share
6 answers

First, note that this only applies to fields, not local variables - they cannot be read until they are assigned, at least in C #. In fact, the CLR initializes the stack frames to 0 if you have the appropriate set of flags, which, in my opinion, is the default. This is rarely seen, but you need to go through bulky hacks.

The default value of int is 0 - and for any type, it is essentially a value represented by a bit pattern full of zeros. For a value type, this is the equivalent of calling a constructor without parameters, and for a reference type, null.

Basically the CLR erases memory with zeros.

It is also the value given by default(SomeType) for any type.

+26
source share

The default value for int is 0

+5
source share

The default value for int is 0.

See the full list of default values ​​for each type: http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

+5
source share

Here is the table of default values ​​for value types in C #: http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx The standard values ​​of reference types are usually zero.

+2
source share

A string is a reference type. Int is the type of value. Link types are simply a pointer to a stack directed at the heap, which may or may not contain a value. The type of value is just the value on the stack, but it should always be set to something.

+2
source share

The value for a unified variable of type T always default(T) . For all reference types, this value is null, and for value types, the link that @Blorgbeard posted (or write code to verify).

+1
source share

All Articles