user3402584
user3402584

Reputation: 410

C# declaration of variable

I started my journey with C# but I realised that I have some problems with some basic information about memory when it comes to declaration of variables. See if I am correct.

int x; // I declared variable of type int, which name is x. Compiler will provide memory for it but we dont have known value of it.
x=10; // Now memory location is still the same but value now kept there is 10;

public struct Point {
    public int x, y;
}

Now I define a struct named Point. Beacuse struct is a value type, it again has reserved memory for it on the computer. Howewer x and y have no value.

Now Point p1 = new Point(); // what is happening here? Struct is not a reference type. So is this just initialization of Point variable with the default constructor without assigning values to x and y?

Second short question. When I write a code like:

int x = 10;

Can I say that I created instance of class integer which value is 10 and name x; I would be grateful for help.

Upvotes: 0

Views: 267

Answers (3)

Ameet
Ameet

Reputation: 7

When you write int x;

this is similar to Point p1 = new Point(); (considering Point structure is already defined)

in both the cases all integer variables will have default value of 0 and not null, which is is basically what is used in C# to denote 'nothing' and can be assigned only to reference types.

As well, in c# everything is a class, so when you write int x = 10;

you are creating an instance of class Int32, though the run time will handle this as value type instead of ref type, as special case.

Same is true for other basic types like, Long, DateTime and few others

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1064324

// what is happening here? Struct is not a reference type. So is this just initialization of Point variable with the default constructor without assigning values to x and y?

No; there are 4 possible scenarios here:

  • a class: the memory space is wiped to all 0s, then any custom constructor is invoked, which may also involve field initializers
  • a struct called without a custom constructor: the memory space is wiped to all 0s
  • a struct called with a custom constructor: the custom constructor is required to assign all the fields
  • a struct variable used without ever calling a constructor: this is actually a thing, but the calling code must write to all the fields before they can do anything else with it; since most structs do not expose their fields, this rarely works

Second short question. When i write a code like:

   int x = 10;

Can i say that i created instance of class integer which value is 10 and name x; I would be grateful for help.

Not really, because in C# terms, int is not a class (it might be in IL terms). Simply: you have declared a local variable of type int with name x and assigned it the value 10, if this is in a method. If this is a class field, then: you have declared a private instance field of type int named x with a field-initializer giving it the value of 10.

Incidentally, you should avoid public fields in general, and mutable fields on structs. You might prefer:

public struct Point {
    private readonly int x, y;
    public int X { get { return x; } }
    public int Y { get { return y; } }
    public Point(int x, int y) { this.x = x; this.y = y'; }
}

This will avoid a huge range of problems.

Upvotes: 4

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51430

In C# the default struct constructor sets the struct memory to 0, effectively setting all variables to their default values.

In case of ints, it will be 0. For reference types, it will result in null.
(in other words, for any type T it will be default(T)).

Note that when you write a custom constructor in a struct, you must initialize all member fields.

Upvotes: 1

Related Questions