可选的非系统类型参数

发布于 2024-08-29 19:11:41 字数 132 浏览 0 评论 0原文

C#4.0 带来了可选参数,我已经等了很长一段时间了。然而,似乎因为只有系统类型可以是 const,所以我无法使用我创建的任何类/结构作为可选参数。

有没有某种方法可以让我使用更复杂的类型作为可选参数。或者这是人们必须忍受的现实之一?

C#4.0 brings optional parameters, which I've been waiting for for quite some time. However it seems that because only System types can be const, I cannot use any class/struct which I have created as an optional parameter.

Is there a some way which allows me to use a more complex type as an optional parameter. Or is this one of the realities that one must just live with?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2024-09-05 19:11:41

对于引用类型,我能想到的最好的办法是:

using System;

public class Gizmo
{
    public int Foo { set; get; }
    public double Bar { set; get; }

    public Gizmo(int f, double b)
    {
        Foo = f;
        Bar = b;
    }
}

class Demo
{
    static void ShowGizmo(Gizmo g = null)
    {
        Gizmo gg = g ?? new Gizmo(12, 34.56);
        Console.WriteLine("Gizmo: Foo = {0}; Bar = {1}", gg.Foo, gg.Bar);
    }

    public static void Main()
    {
        ShowGizmo();
        ShowGizmo(new Gizmo(7, 8.90));
    }
}

您可以通过使参数可为空来对结构使用相同的想法:

public struct Whatsit
{
    public int Foo { set; get; }
    public double Bar { set; get; }

    public Whatsit(int f, double b) : this()
    {
        Foo = f; Bar = b;
    }
}

static void ShowWhatsit(Whatsit? s = null)
{
    Whatsit ss = s ?? new Whatsit(1, 2.3);
    Console.WriteLine("Whatsit: Foo = {0}; Bar = {1}",
        ss.Foo, ss.Bar);
}

The best I could come up with for reference types was:

using System;

public class Gizmo
{
    public int Foo { set; get; }
    public double Bar { set; get; }

    public Gizmo(int f, double b)
    {
        Foo = f;
        Bar = b;
    }
}

class Demo
{
    static void ShowGizmo(Gizmo g = null)
    {
        Gizmo gg = g ?? new Gizmo(12, 34.56);
        Console.WriteLine("Gizmo: Foo = {0}; Bar = {1}", gg.Foo, gg.Bar);
    }

    public static void Main()
    {
        ShowGizmo();
        ShowGizmo(new Gizmo(7, 8.90));
    }
}

You can use the same idea for structs by making the parameter nullable:

public struct Whatsit
{
    public int Foo { set; get; }
    public double Bar { set; get; }

    public Whatsit(int f, double b) : this()
    {
        Foo = f; Bar = b;
    }
}

static void ShowWhatsit(Whatsit? s = null)
{
    Whatsit ss = s ?? new Whatsit(1, 2.3);
    Console.WriteLine("Whatsit: Foo = {0}; Bar = {1}",
        ss.Foo, ss.Bar);
}
-柠檬树下少年和吉他 2024-09-05 19:11:41

您可以使用任何类型作为可选参数:

using System;

class Bar { }

class Program
{
    static void Main()
    {
        foo();
    }
    static void foo(Bar bar = null) { }
}

好的,我重读了您的问题,我想我明白您的意思 - 您希望能够执行以下操作:

static void foo(Bar bar = new Bar()) { }

不幸的是,这是不允许的,因为默认参数的值必须在编译时已知,以便编译器可以将其烘焙到程序集中。

You can use any type as an optional parameter:

using System;

class Bar { }

class Program
{
    static void Main()
    {
        foo();
    }
    static void foo(Bar bar = null) { }
}

Okay, I reread your question and I think I see what you mean - you want to be able to do something like this:

static void foo(Bar bar = new Bar()) { }

Unfortunately this is a not allowed since the value of the default parameter must be known at compile time so that the compiler can bake it into the assembly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文