当前位置: 首页 > 编程语言 > C#

与return相比,使用out-ref有什么好处?分享

时间:2023-04-10 18:31:53 C#

与return相比,使用out/ref有什么好处?我正在使用XNA框架制作游戏,因此我使用了很多对向量进行操作的函数。(特别是Vector2(64位结构))。令我困扰的是,大多数方法都是用ref和out参数定义的。这是一个例子:voidMin(refVector2value1,refVector2value2,outVector2result)我也觉得有点奇怪。还有一个明显更明显的publicstaticVector2Min(Vector2value1,Vector2value2);基本上,几乎所有函数都有ref和s的重载。同样,其他API。这种设计有什么好处?XNA针对性能进行了优化,这会是结果吗?比如说,四元数需要128b,其中ref更少。编辑:这是一个测试代码:publicclassGame1:Microsoft.Xna.Framework.Game{GraphicsDeviceManagergraphics;精灵批次精灵批次;私人Vector2vec1=新Vector2(1,2);私人Vector2vec2=新Vector2(2,3);私有Vector2分钟;私有字符串timeRefOut1;私有字符串timeRefOut2;私有SpriteFont字体;publicGame1(){graphics=newGraphicsDeviceManager(this);Content.RootDirectory="内容";refOut1();refOut2();}privateVector2refOut1(){Vector2min=Vector2.Min(vec1,vec2);返回分钟;}privateVector2refOut2(){Vector2.Min(refvec1,refvec2,outmin);返回分钟;}protectedoverridevoidInitialize(){constintlen=100000000;秒表stopWatch=newStopwatch();stopWatch.Start();对于(inti=0;i