一个(无效)扩展方法中更改数值 [英] Change value inside an (void) extension method

查看:157
本文介绍了一个(无效)扩展方法中更改数值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这个模拟的扩展方法,它的值更改为另一个值:

So I have this mock extension method which change a value to another value:

public static void ChangeValue(this int value, int valueToChange)
{
    value = valueToChange;
}

当我尝试使用它:

int asd = 8;
asd.ChangeValue(10);
Debug.Log(asd);



它返回的8而不是10
在值没有给ChangeValue方法中改变,它并没有改变ASD的值。我需要什么补充的方法,使?

It returns 8 instead of 10. While the value did change inside the ChangeValue method, it didn't change the value of "asd". What do I need to add to the method, to make it update "asd"?

推荐答案

您不能这样做,没有它更新ASD使用一个收益值或 REF 参数。后者不工作一起这个(扩展方法),所以最好的办法是收益价值(而不是无效)。

You can't do that without using either a return value, or a ref parameter. The latter doesn't work alongside this (extension methods), so your best bet is a return value (rather than void).

这篇关于一个(无效)扩展方法中更改数值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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