C#值类型参数传递的性能开销

Performance issues

Let's dig a little deeper. When data is passed into methods as value type parameters, a copy of each parameter is created on the stack. Clearly, if the parameter in question is a large data type, such as a user-defined structure with many elements, or the method is executed many times, this may have an impact on performance.


In these situations it may be preferable to pass a reference to the type, using the ref keyword. This is the C# equivalent of the C++ technique of passing a pointer to a variable into a function. As with the C++ version, the method has the ability to change the contents of the variable, which may not always be safe. The programmer needs to decide on the trade-off between security and performance.

https://msdn.microsoft.com/en-us/library/4d43ts61(v=vs.90).aspx

原文地址:https://www.cnblogs.com/lilei9110/p/5072665.html