1

If we pass an array of structs as method parameter, in the method body do we have a reference to an array of structs, or a new array of structs?

3 Answers 3

8

You'll have a reference to an array of structs.

Array itself is a reference type, so an array of structs will be an object with the values stored inline.

If you pass an array to a method, you pass a reference to the array object. The reference itself is passed by value.

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

Comments

1

When you declare an array of value types, .NET allocates memory on the heap not stack. So it is always referred to be its reference.

The only exception is stackalloc where a memory area is allocated on the stack and can be used unsafely and it is faster that heap access.

Comments

1

Array is a class in the .net framework so if you create a struct arrays so you will have a reference type ,i am not commenting how and where these will be stored whether it is stack or heap because it is pure implementational details but Microsoft implementation of reference type will go on the HEAP.

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.