如何在函数内部更改和返回值

huangapple go评论101阅读模式
英文:

How to change and return value from within a function

问题

I am given a code block (C#) and want to change and return a new value (e.g., 9999) in the variable gid. However, the new value is not being returned. The original value used in the function call has not changed. How do I force a change in the value of the variable?

This is the code block:

public uint? GeneratePrimaryGid(DirectoryEntry de, uint? gid, int option)
{
    gid = 9999;
    return gid;
}

Unfortunately, passing it by reference, using the ref keyword, did not work.
As a comparison, here is the code segment for changing the Unix shell:

public string GenerateShell(DirectoryEntry de, string shell, int option)
{
    return shell;
}

When I add this line before the return:
shell = "/bin/test";

I can see that the shell is set to /bin/test, even though the value that was passed to the function was '/bin/bash'. In fact, when the parameter is of type 'string', the changes are reflected. Just not when the type is 'uint?'.

英文:

I am given a code block (C#) and want to change and return a new value (e.g., 9999) in the variable gid. However, the new value is not being returned. The original value used in the function call has not changed. How do I force a change in the value of the variable?

This is the code block:

public uint? GeneratePrimaryGid(DirectoryEntry de, uint? gid, int option)
{
    gid = 9999;
    return gid;
}

Unfortunately, passing it by reference, using the ref keyword, did not work.
As a comparison, here is the code segment for changing the Unix shell:

    public string GenerateShell(DirectoryEntry de, string shell, int option)
    {
        return shell;
    }

When I add this line before the return:
shell = "/bin/test";

I can see that the shell is set to /bin/test, even tho the value that was passed to the function was '/bin/bash'. In fact, when the parameter is of type 'string', the changes are reflected. Just not when the type is 'unit?'.

答案1

得分: 1

以下是翻译好的部分:

对于这个答案,我简化了方法签名,只包含正在更改的参数:

public uint? GeneratePrimaryGid(uint? gid)

您提到:
> 我有一个代码块,想要更改并返回变量gid中的新值(例如9999)

返回一个值和更改一个值是两种不同的事情。您的方法成功地在这里返回了您想要的值:

return gid;

可能让您感到困惑的部分是,因为uint?是一个值类型,所以您只得到了参数中值的副本,所以当您修改它时,调用方的变量值不会发生变化。为了更改调用方的值,您需要通过引用传递它,使用ref关键字:

public uint? GeneratePrimaryGid(ref uint? gid)
{
    gid = 9999;
    return gid;
}

现在,您既返回值,又更改了调用方的变量值。

英文:

For this answer I'm simplifying the method signature to only contain the argument that's being changed:

public uint? GeneratePrimaryGid(uint? gid)

You stated:
> I am given a code block and want to change and return a new value (e.g., 9999) in the variable gid

Returning a value and changing a value are two different things. Your method is successfully returning the value you want here:

return gid;

The part that is possibly confusing you is that, because uint? is a value type, you're only getting a copy of the value in the argument, so when you modify it, there is no change to the value of the variable on the caller's side. In order to change the value for the caller, you need to pass it by reference, using the ref keyword:

public uint? GeneratePrimaryGid(ref uint? gid)
{
    gid = 9999;
    return gid;
}

Now you are both returning the value and changing the value of the variable on the caller's side.

huangapple
  • 本文由 发表于 2023年6月29日 22:47:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76582192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定