Optionally passing a struct to a custom function in C#

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

Optionally passing a struct to a custom function in C#

问题

以下是翻译好的部分:

有关此问题还有其他类似的Stack Overflow帖子,但我很难找到任何有意义的解决方案。在C#方面我还相对新手...

所以,我想做的只是向自定义函数添加一个可选参数。这个参数需要包含两个值,所以我想使用一个自定义结构体,像这样:

public struct FooBah {
    public string foo;
    public string bah;

    public FooBah(string foo, string bah) {
        this.foo  = foo;
        this.bah = bah;
    }
}

然后在函数调用中使用它:

FooBah foobah = new FooBah("foo", "bah");
CustomFunction(foobah);

至于函数本身:

private void CustomFunction(FooBah foobah = null) {
    if(foobah != null) {
        // 使用foobah做一些操作
    }
}

但是出现了以下错误:

不能将类型 '' 的值用作默认参数,因为没有到类型 'FooBah' 的标准转换

请问有什么建议吗?

英文:

There are other similar SO posts on this, but I'm struggling to find anything that makes much sense. Still fairly new to C# here...

So, all I want to do is add an optional parameter to a custom function. This param needs to contain two values so I figured a custom struct would work, like this:

public struct FooBah {
    public string foo;
    public string bah;

    public FooBah(string foo, string bah) {
        this.foo  = foo;
        this.bah = bah;
    }
}

Then to use it in a function call:

FooBah foobah = new FooBah("foo", "bah");
CustomFunction(foobah);

And for the function itself:

private void CustomFunction(FooBah foobah = null) {
    if(foobah != null) {
        // Do something with foobah
    }
}

But this errors because:

> A value of type '<null>' cannot be used as a default parameter because there are no standard conversions to type 'FooBah'

Any advice please?

答案1

得分: 1

如果结构体有可检测到的默认状态(如 foo 为 null),您可以编写如下代码:

static void CustomFunction(FooBah foobah = default)
{
    if (foobah.foo != null)
    {
        // 使用 foobah 进行操作
    }
}

当然,这取决于您对 foo 的使用情况。

或者,您可以使用 FooBah? 参数:

static void CustomFunction(FooBah? foobah = null)
{
    if (foobah == null)
    {
        // 使用 foobah 进行操作
    }
}

以下调用都有效:

CustomFunction();
CustomFunction(new FooBah());
CustomFunction(null);

var fooBah = new FooBah();
CustomFunction(fooBah);

请注意,如果您不想在方法内部修改结构体,可以使用 in 修饰符:

static void CustomFunction(in FooBah? foobah = null)
{
    if (foobah == null)
    {
        Console.WriteLine("默认");
    }
    else
    {
        Console.WriteLine("非默认");
    }
}

请注意,在使用可空值类型时,必须通过 .Value 属性访问值本身,即 foobah.Value.foo - 这是因为类型是 Nullable<FooBah> 而不是 FooBah

查看此处的 Nullable 值类型文档

查看此处以获取有关何时使用 in 的更多信息

最后,最明显的解决方案是重载方法,有参数和无参数两种情况。

英文:

If there is a default state for the struct that you can detect (such as foo being null) you can write code like this:

static void CustomFunction(FooBah foobah = default)
{
    if (foobah.foo != null)
    {
        // Do something with foobah
    }
}

Of course this depends on your usage of foo.

Alternatively you can use a FooBah? parameter:

static void CustomFunction(FooBah? foobah = null)
{
    if (foobah == null)
    {
        // Do something with foobah
    }
}

The following calls all work:

CustomFunction();
CustomFunction(new FooBah());
CustomFunction(null);

var fooBah = new FooBah();
CustomFunction(fooBah);

Note that if you don't want to modify the struct inside the method you can use the in modifier:

static void CustomFunction(in FooBah? foobah = null)
{
    if (foobah == null)
    {
        Console.WriteLine(&quot;Default&quot;);
    }
    else
    {
        Console.WriteLine(&quot;Not default&quot;);
    }
}

Note that when using a nullable value type, you must access the value itself via a .Value property, i.e. foobah.Value.foo - this is because the type is Nullable&lt;FooBah&gt; rather than FooBah.

See here for the Nullable value type documentation

See here for more information on when you should use in.

Finally the most obvious solution is to overload the method with and without a parameter.

答案2

得分: 1

This could work, but your structure is not nullable, as in it can't be converted to null, hence, you could do it like this to define an optional argument -

这个方法可能可行,但是你的结构体是不可为空的,也就是不能转换为null,因此,你可以像这样定义一个可选参数 -

public struct FooBah {
    public string foo;
    public string bah;

    public FooBah(string foo, string bah) {
        this.foo  = foo;
        this.bah = bah;
    }
}

FooBah foobah = new FooBah("foo", "bah");
CustomFunction(foobah);

private void CustomFunction(FooBah? structOptional = null){
//What you want to do with the struct
}
英文:

This could work, but your structure is not nullable, as in it can't be converted to null, hence, you could do it like this to define an optional argument -

public struct FooBah {
    public string foo;
    public string bah;

    public FooBah(string foo, string bah) {
        this.foo  = foo;
        this.bah = bah;
    }
}

FooBah foobah = new FooBah(&quot;foo&quot;, &quot;bah&quot;);
CustomFunction(foobah);

private void CustomFunction(CustomFunction? structOptional = null){
//What you want to do with the struct
}

huangapple
  • 本文由 发表于 2023年5月10日 18:48:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217494.html
匿名

发表评论

匿名网友

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

确定