1

It is simple C# struct example below.

public struct MyStruct
{
    public int a;

    public void Foo()
    {
         //Do something
    }
}

public class Test
{
    Mystruct st;
    void DoSomething()
    {
        st.Foo();
    }    
}

I had known If we use struct instance without new, member field must be initialized before use. but example above don't have any error. why?

1
  • When declared as a field (Mystruct st;) the structure will be initialized with zeros; when declared as a local variable the structure will not be initialized (will contain trash) Commented Apr 7, 2017 at 7:56

2 Answers 2

3

This has nothing to do with struct.

In C#, every (local) variable has to be initialized (assigned a value) before you can use it.

Fields of classes are initialized to their default value when an instance of the class is created, so you don't have to assign a value explicitly in your constructor.

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

1 Comment

It would be slightly more correct to say that "Fields on classes" are initialized to their default value ... (blah)"; because that isn't the case for structs with bespoke constructors - all fields on a struct must be explicitly assigned a value in the constructor
0

It has everything to do with the difference between classes and structs. A struct is a value type. It means, that as soon as you declare it, memory is allocated for that struct. So in your case, the memory required for the MyStruct.a is allocated within the memory of the instance of Test, when the instance of Test is created.

The memory for class instances (reference type) is only allocated when you create the instance (new). So you can't access any member (field or method or ...) before it's created. Theoretically, the code for both a struct and a class should be available, but for classes that code needs information from the instance to call the method, e.g. to find out which (virtual) method actually to call.

It also has to do with the difference between allocating and initializing a variable. When you've allocated memory for a variable, you can use that variable, even though you might not know what the contents are. When you've initialized a variable, you actually know what the contents are, so you can use it AND get predictable results. Initializing a variable is done automatically for some variables and you need to do it yourself for others (e.g. local variables). I don't know the reason for this, but just accept it ;) If you don't initialize a variable that should be initialized, the compiler helps you with a build error.

I hope this helps for you.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.