开发者

How do I default a parameter to DateTime.MaxValue in C#?

开发者 https://www.devze.com 2023-02-22 10:42 出处:网络
I wish to say: public void Problem(DateTime optional = DateTime.MaxValue) { } But the compiler complains that DateTime.MaxValue is not a compile time constant.

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.Min开发者_C百科Value 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!


I would substitute this for something like:

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


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.


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.


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.


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");
    }
}


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


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.


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
}
0

精彩评论

暂无评论...
验证码 换一张
取 消