1

I am wondering about a sentence in a book about C# I'm reading currently. This sentence is:

'value type that is initialized to all 0's'.

Probably I don't understand it because I'm not a native speaker.

In my understanding of the language would that mean that a variable has multiple values when it gets initialized? This doesn't really makes sense to me. Could you help me to understand what this means?

Niall C.
  • 10,878
  • 7
  • 69
  • 61

6 Answers6

4

Consider this value type:

public struct Point
{
   public int X;
   public int Y;
}

"All 0's" here means X = 0 and Y = 0.

Update: I just discovered that this example is used in the MSDN documentation for struct. They throw in a constructor.

Daniel Daranas
  • 22,454
  • 9
  • 63
  • 116
  • 1
    I think this response is somewhat imprecise. Zero in memory does not necessarily *mean* "zero", as it happens to do for `int` variables. The sentence in the book, however, probably refers to zeroes in memory. – O. R. Mapper Jul 17 '13 at 14:52
1

I think the sentence is not correctly formulated. I guess that the author means, or should have written, that a value type is initialized to its default value when it is declared. In case of an integer (which is a value type), the default value is 0.

For instance

public struct Test
{
   public int a;
   public Decimal b;
   public DateTime c;

   public void Output(){

      Console.WriteLine ("a: " + a);
      Console.WriteLine ("b: " + b);
      Console.WriteLine ("c: " + c);

   }
}

var t = new Test();
t.Output();

will display:

a: 0
b: 0
c: 01/01/0001 0:00:00
Frederik Gheysels
  • 56,135
  • 11
  • 101
  • 154
  • This only applies to fields, not locals. Also, reference types are default-initialized as well. – Cory Nelson Jul 17 '13 at 14:50
  • The sentence is correct, as the underlying value in memory (in the case of `DateTime`, ticks since the date shown as value `c`) would be zero. +1 for showing `DateTime` as an example where the memory value is indeed 0, even though it does not *represent* a value that we would interpret as "zero". – O. R. Mapper Jul 17 '13 at 15:04
0

Probably it is about default values of value types like byte, double, int, sbyte, short, uint, ushort, ulong.

For example when you say this;

int i = new int();

i initialized to 0.

Check Default Values Table (C# Reference)

Also it could be using value types in a struct type like;

public struct MyStruct
{
   public int X;
   public short Y;
   public double Z;
}

The values of X, Y and Z are 0.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
0

Most probably author means default value for every type present inside the value type instance (which is naturally might be different for every type).

To achieve this you can do like:

MyStruct struc = default(MyStruct);
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

A variable of a value type occupies a certain number of bytes in memory. The statement that this type is initialized to zeroes means that all of these bytes are set to the value 0 upon initialization of the variable.

This seems to be true for many, if not all, value types (i.e. primitive number types, structs, ...), as outlined in this question.

Community
  • 1
  • 1
O. R. Mapper
  • 20,083
  • 9
  • 69
  • 114
0

A value type is a non-reference type, such as a struct or integer. By saying it "initializes to all zeroes" it means that if you don't set it to something it will be auto-initialized to the zero value.

public class Foo
{
    public int  _foo;
    public int? _bar;

    public void Output()
    {
        Console.WriteLine(this._foo); // Outputs: 0
        Console.WriteLine(this._bar.HasValue ? "not null" : "null"); // Outputs: null
    }
}
Nick Gotch
  • 9,167
  • 14
  • 70
  • 97
  • Dont forget this is false for `int?(Nullable)`. I mean which will have null as default – Sriram Sakthivel Jul 17 '13 at 14:54
  • Yes. Nullables are reference types since they box value types. – Nick Gotch Jul 17 '13 at 14:56
  • @NickGotch: [The answers to this question](http://stackoverflow.com/questions/3149173/are-nullable-types-reference-types) come to the opposite conclusion. – O. R. Mapper Jul 17 '13 at 14:57
  • 1
    Ah, interesting. Makes sense. – Nick Gotch Jul 17 '13 at 14:59
  • 1
    @NickGotch your assumption is wrong. it is actually a struct. which has a Boolean saying `HasValue`, setting it to null just makes this Boolean to false. It is actually a `Struct` compiler pretends to show as null. – Sriram Sakthivel Jul 17 '13 at 15:00
  • 1
    So the reason a Nullable would default to zero would be because if everything is initialized to zero, the HasValue would default to false and make it act as a null. Good to know. – Nick Gotch Jul 17 '13 at 15:01