1 回答

TA貢獻1851條經(jīng)驗 獲得超4個贊
它也有利于緊密耦合。例如,MSDN 中的以下方法 (https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/in-parameter-modifier):
static void Method(in int argument)
{
// implementation removed
}
Method(5); // ok
Method(5L); // CS1503: no implicit conversion from long to int
這是一個有趣的優(yōu)化鏈接(我復制了他們的結(jié)果),它討論了,和:https://faithlife.codes/blog/2017/12/in-will-make-your-code-slower/readonly structreadonly refin
Method Mean
PointByValue 25.09 ns
PointByRef 21.77 ns
PointByIn 34.59 ns // our guy
ReadOnlyPointByValue 25.29 ns
ReadOnlyPointByRef 21.78 ns
ReadOnlyPointByIn 21.79 ns
Image caption 一些進一步的閱讀(圖片來源:Jon Skeet):
微觀優(yōu)化:只讀場的驚人低效率
https://codeblog.jonskeet.uk/2014/07/16/micro-optimization-the-surprising-inefficiency-of-readonly-fields/
- 1 回答
- 0 關(guān)注
- 110 瀏覽
添加回答
舉報