2

I have a structure as below. I have few problems

Problem1:

struct MyStruct 
{
    public MyStruct(int a)
    {
        this.a = a;
        this.b = 10;
    }
    public int a;
    public int b;
}

When I remove this.b from MyStruct constuctor it will give me an error "Field must be fully assigned before control is returned to the caller". but in case of class it doesn't occur

Problem2:

struct MyStruct 
{
    //public MyStruct(int a)
    //{
    //      this.a = a;
    //      this.b = 10;
    //}
    //int asd;
    //public int MyProperty { get; set; }
    public void getImplemen()
    {
        Console.WriteLine("azsdfa");
    }
    public int a;
    public int b;
}

static void Main(string[] args)
{
    MyStruct myStruct ;
    myStruct.a = 15;//when I comment this it will give an error
    myStruct.b = 15; //when I comment this it will give an error
    myStruct.getImplemen();
}

When I change MyStruct myStruct to MyStruct myStruct = new MyStruct (); it works fine.

why so?

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
Ankur Bhutani
  • 3,079
  • 4
  • 29
  • 26

5 Answers5

4

That's just how it goes.

Default constructor initializes every field to a default value, while a constructor with parameters forces you to initialize every field in the struct.

What if you have a default constructor AND one with parameters, you ask? Well, I don't remember. Easy enough to check on your own.

SimpleVar
  • 14,044
  • 4
  • 38
  • 60
  • I know that thing but why it doesn't give me an error in case of class. This is my question. – Ankur Bhutani Sep 04 '15 at 04:55
  • 2
    See: http://stackoverflow.com/questions/721246/why-must-i-initialize-all-fields-in-my-c-sharp-struct-with-a-non-default-constru – Blorgbeard Sep 04 '15 at 04:58
  • You shouldn't ask why the error doesn't occur in class. The behavior where it initializes defaults for you is reasonably easy to understand. It is the case of structs which is strange and should be questioned, and for that you can look at the link @Blorgbeard shared. – SimpleVar Sep 04 '15 at 05:17
  • @Yorye, yeah you are right my point of focus should be structs. BTW which link you are referring? can you share the link from Blorgbeard? – Ankur Bhutani Sep 04 '15 at 05:25
  • 1
    It is the second comment on this very answer. It is even upvoted. – SimpleVar Sep 04 '15 at 05:26
2

It does not allocate memory for fields:

MyStruct myStruct;

Allocates memory and initialize fields in constructor:

MyStruct myStruct = new MyStruct();

If you does not allocate memory for a variable then you can not assign a value to the fields. Сonstructor allocate memory and initializes fields (you need initialize fields in constructor before control is returned to the caller).

Denis Bubnov
  • 2,619
  • 5
  • 30
  • 54
  • This one answers his question by explaining why (though you didn't touch on his parameters). +1 – Kaitlyn Sep 04 '15 at 05:25
0

you should refer to https://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx

you need to create the instance of your struct before using it.

amit dayama
  • 3,246
  • 2
  • 17
  • 28
  • I understand that's why I said when I use MyStruct myStruct it will give an error but when use new it works fine. But why this behaviour what is cooked with new for struct case – Ankur Bhutani Sep 04 '15 at 05:08
0

The difference is that structs are value types while classes are reference types. When a value type object is created, memory space will be allocated to store the value, thus its member variable cannot be null, whilst class member variables can be null. Hence, the compiler only complains when struct member variables are not assigned to.

Viet Nguyen
  • 406
  • 3
  • 12
0

Remember the thumb rule for Structs: All fields must be initialized. Values can be provided by you or default ones.

For Question 1:

When you initialize struct with 'new' (without parameters), all fields in it are initialized to default type values (0 for int, null for string etc). Since you are using parameterized constructor compiler does not use default one and hence you get error if you don't initialize field 'b'. You can still make this work as below:

public MyStruct(int a) : this()
{
   this.a = a;
}

For Question 2:

Recall the thumb rule I mentioned in the beginning. So you either use default constructor with 'new' initialization or set field values in calling code.

Quick suggestion: Please do not use public fields in class/struct. Use properties to encapsulate them.

Nikhil Vartak
  • 5,002
  • 3
  • 26
  • 32