.NET Parameter passing - by reference v/s by value

后端 未结 6 1067
抹茶落季
抹茶落季 2020-12-15 06:05

I\'m trying to validate my understanding of how C#/.NET/CLR treats value types and reference types. I\'ve read so many contradicting explanations I stil

This is what

6条回答
  •  太阳男子
    2020-12-15 06:43

    The specifications says nothing about where to allocate value types and objects. It would be a correct C# implementation to say allocate everything on the heap and there Atr situations where values are allocated on the heap other than those you write.

    int i = 4; Func dele = ()=> (object)i;

    Will result in (a copy of) i being allocated on the heap because the compiler will make it into a member of a class eventhough it's not declared as such. Other than that you're pretty much spot on. And no everything is not passed as reference. It would be closer to the thruth to state that every parameter was passed by value but still not entirely correct (e.g. ref or out).

提交回复
热议问题