改变结构的方法 [英] change struct in method

查看:157
本文介绍了改变结构的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

这篇关于改变结构的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆