0

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.

3
  • 2
    A struct is a value type which always has a value (the default value of each field's type, so 0 for int). An explicit parameterless constructor is not allowed for this reason. Commented Aug 8, 2014 at 14:21
  • 1
    See the spec.. Commented Aug 8, 2014 at 14:22
  • When you say int x = 10; You are assigning value to integer type x, no instance. Commented Aug 8, 2014 at 14:24

3 Answers 3

4

// 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.

Sign up to request clarification or add additional context in comments.

6 Comments

great but i dont get what ,,wiped to all 0s,, means :)
@user3402584 your computer has memory; every value, whether a value-type or reference-type: takes some of that memory. Imagine a value takes 20 bytes to store, for some reason (5 integers, as an example). Now set the 20 bytes that represent that value all to zero. That is what it means. In the case of integers, the 4 bytes [0 0 0 0] represent 0. In the case of references the 4-bytes (32-bit) or 8 bytes (64-bit) of zero are also known as: null
hmmm,,,Now set the 20 bytes that represent that value all to zero'' U mean i set 5 integers to 0 ? I dont get it, srry
Your fourth bullet can be a little weaker - if you write to one field, you can at least read that field.
@Rawling indeed, yes; although since it is very rare to expose fields, this is largely moot anyway ;p
|
1

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.

2 Comments

what does memory to 0 mean?
If you know C, imagine calling memset(&theStruct, 0, sizeof(typeOfTheStruct))
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.