如何在 C# 中将参数默认为 DateTime.MaxValue?

发布于 2024-10-29 22:00:59 字数 380 浏览 1 评论 0原文

我想说:

public void Problem(DateTime optional = DateTime.MaxValue)
{
}

但是编译器抱怨 DateTime.MaxValue 不是编译时间常量。

DateTime.MinValue 很简单,只需使用默认值(DateTime)

另请参阅“如何在 C# 中将参数默认为 Guid.Empty?"

我不想使用方法重载,因为我试图驯服的方法有 101 个参数!

I wish to say:

public void Problem(DateTime optional = DateTime.MaxValue)
{
}

But the compiler complains that DateTime.MaxValue is not a compile time constant.

DateTime.MinValue is easy, just use default(DateTime)

see also "How do I default a parameter to Guid.Empty in C#?"

I do not wish to use method overloading, as the method I am trying to tame has 101 parameters!

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

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

发布评论

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

评论(8

终止放荡 2024-11-05 22:00:59

我会用这样的东西代替:

public void Problem(DateTime? optional = null)
{
   DateTime dateTime = optional ?? DateTime.MaxValue
   // Now use dateTime
}

I would substitute this for something like:

public void Problem(DateTime? optional = null)
{
   DateTime dateTime = optional ?? DateTime.MaxValue
   // Now use dateTime
}
染柒℉ 2024-11-05 22:00:59

根据您的评论之一,您正在尝试使具有 101 参数的方法对调用者更有用。
我强烈建议您创建一个参数类并使用默认值初始化该类的属性。为您的方法提供一个仅接受一个参数的重载:参数类。
这将真正提高方法的使用率,因为如果用户只需要更改一个参数,他甚至可以重用其参数类实例。

According to one of your comments, you are trying to make a method with 101 parameters more usable for the callers.
I strongly suggest, that you create a parameter class and initialize the properties of that class with the default values. Provide an overload for your method that accepts only one parameter: The parameter class.
This will really improve the usage of your method, because the user can even reuse its parameter class instance if he needs to change only one parameter.

仙气飘飘 2024-11-05 22:00:59

您可以定义多个函数:

public void Problem()
{
     Problem(DateTime.MaxValue);
}
public void Problem(DateTime optional)
{
     // do your stuff here.
}

如果您调用 Problem() (不带参数),该函数将调用另一个带参数的函数。

You can define multiple functions:

public void Problem()
{
     Problem(DateTime.MaxValue);
}
public void Problem(DateTime optional)
{
     // do your stuff here.
}

If you call Problem() (without parameter) that function calls the other function with a parameter.

糖果控 2024-11-05 22:00:59

你要求做的事情根本不可能。 DateTime.MaxValue 不是编译时常量;它实际上是一个只读字段,在运行时由静态构造函数初始化。这种差异在这里变得非常关键。可选参数需要编译时常量,因为它们将值直接烘焙到代码中。

然而,真正的问题是您的方法需要 101 个参数。我从来没有见过任何事情如此大​​声地呼吁重构。我的建议是更改您的方法以接受类的实例。这也将使您能够更加灵活地为类的各个属性指定默认值。特别是,您将能够指定非编译时常量的值。

What you're asking to do is simply not possible. DateTime.MaxValue is not a compile-time constant; it's actually a read-only field that is initialized at runtime by a static constructor. That difference becomes quite critical here. Optional parameters require compile-time constants, as they bake the value directly into the code.

However, the real problem is that your method takes 101 parameters. I've never seen anything crying out so loudly for refactoring. My recommendation would be change your method to accept an instance of a class, instead. That will also give you more flexibility in specifying default values for individual properties of the class. In particular, you'll be able to specify values that are not compile-time constants.

心如荒岛 2024-11-05 22:00:59

我不熟悉 C#4.0,但在 c#3.5 中我会使用重载;

public void Problem()
{
    Problem(DateTime.MaxValue);
}
public void Problem(DateTime dt)
{
}

并使用以下任一方式调用它:

Problem(); //defaults to maxvalue
Problem(myDateTime); //uses input value

编辑:
只是对一些评论做出答复;

public class FooBar
{
    public bool Problem()
    {
        //creates a default person object
        return Problem(new Person());
    }

    public void Problem(Person person)
    {
        //Some logic here
        return true;
    }
}

public class Person
{
    public string Name { get; private set; }
    public DateTime DOB { get; private set; }
    public Person(string name, DateTime dob)
    {
        this.Name = name;
        this.DOB = dob;
    }

    /// <summary>
    /// Default constructor
    /// </summary>
    public Person()
    {
        Name = "Michael";
        DOB = DateTime.Parse("1980-07-21");
    }
}

I'm not familiar with C#4.0, but in c#3.5 I'd use overloading;

public void Problem()
{
    Problem(DateTime.MaxValue);
}
public void Problem(DateTime dt)
{
}

And call it with either:

Problem(); //defaults to maxvalue
Problem(myDateTime); //uses input value

Edit:
Just to put an answer to some of the comments;

public class FooBar
{
    public bool Problem()
    {
        //creates a default person object
        return Problem(new Person());
    }

    public void Problem(Person person)
    {
        //Some logic here
        return true;
    }
}

public class Person
{
    public string Name { get; private set; }
    public DateTime DOB { get; private set; }
    public Person(string name, DateTime dob)
    {
        this.Name = name;
        this.DOB = dob;
    }

    /// <summary>
    /// Default constructor
    /// </summary>
    public Person()
    {
        Name = "Michael";
        DOB = DateTime.Parse("1980-07-21");
    }
}
江挽川 2024-11-05 22:00:59

简单的答案是你不能使用我认为不可选的参数。

如果这是唯一的参数,则可以使用重载。如果这是具有许多可选参数的方法的示例,那么这可能不可行。

您可以做的是将其设为 DateTime? 并传递 null,然后将 null 解释为 DateTime.MaxValue方法。

有一篇关于可选参数的很好的文章,我将为您挖掘。

编辑

The simple answer is you can't with optional parameters I don't think.

you could use an overload if this is the only parameter. If this is an example for a method with many optional params, then this might not be feasible.

What you could do is make it DateTime? and pass null, then interpret null as DateTime.MaxValue in your method.

There is a good write up of optional arguments which I'll dig up for you.

EDIT

article here

沐歌 2024-11-05 22:00:59

如果,正如您在评论之一中所述,您的方法有很多参数,您可以将它们全部转换为参数类并使用其属性初始值设定项。这样您就不必初始化所有属性,并且可以在该类的构造函数中将日期设置为 DateTime.MaxValue

If, as you stated in one of your comments, your method has a lot of parameters, you can possibly turn them all into a parameter class and use its property initializers. Then you won't have to initialize all properties, and you can set the date to DateTime.MaxValue in the constructor of that class.

爱情眠于流年 2024-11-05 22:00:59

loadDefault 参数值是常量,即不能是 string.Empty/Guid.Empty 等。您可以使用方法重载:

void M(int intValue)
{
   M(intValue, Guid.Empty);
}
void M(int intValue, Guid guid)
{
   //do something
}

loadDefault parameter values are constants, that is, it can't be string.Empty/Guid.Empty and etc. You can use a method overload:

void M(int intValue)
{
   M(intValue, Guid.Empty);
}
void M(int intValue, Guid guid)
{
   //do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文