改变结构的方法

编程入门 行业动态 更新时间:2024-10-27 04:32:31
本文介绍了改变结构的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我怎样才能改变结构的外部方法?

How can I change struct in external method ?

public void ChangeStruct (MyStruct myStruct) { myStruct.field1 = 10; return; }

当我通过结构该方法后ChangeStruct方法我想MYSTRUCT改变。

When I pass struct to ChangeStruct method after that method I would like myStruct to be changed.

推荐答案

您需要传递一个参考结构,而不是使用的 REF 关键字:

You need to pass a reference to the struct instead of a copy using the ref keyword :

public void ChangeStruct (ref MyStruct myStruct) { myStruct.field1 = 10; } ChangeStruct(ref someStruct);

您当前的code在进入方法之前创建的结构的完整位对位的拷贝,它的这个副本要修改,裁判关键字力传递引用(托管指针)主叫方的结构,而不是拷贝。

Your current code create a full bit-for-bit copy of the struct before entering the method and it's this copy that you are modifying, the ref keyword force the caller to pass a reference (managed pointer) to the structure instead of the copy.

更多推荐

改变结构的方法

本文发布于:2023-11-05 18:54:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561592.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:结构   方法

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!