Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6717833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:53:55+00:00 2026-05-26T08:53:55+00:00

Normally, if I have a nullable type for an optional parameter, I would put

  • 0

Normally, if I have a nullable type for an optional parameter, I would put null as the default value. This way I know that if the value is null, the caller doesn’t want to specify any value for that one.

public void Foo(string text, string text2= null);

If the parameter is normally a positive integer, I can use a negative number

public void Foo(string text, int index=-1);

How about DateTime? It is not nullable, and (as far as I know) it doesn’t have a meaningless number that cannot be a true input either (like -1 for positive integer). Or is there? What would you use in this situation?

I also know that I can use the nullable DateTime type, but this means that the method caller will have to use Nullable as well as opposed to just conveniently pass a DateTime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:53:55+00:00Added an answer on May 26, 2026 at 8:53 am

    You can make value types nullable using the ? operator in C#:

    DateTime? myDate = null;

    From this, you can make the parameter optional:

    void Foo(DateTime? myDate = null)
    {
    }
    

    Further reading on Nullable Types.

    This is not the only way to skin the cat however, you can use default(DateTime), however you cannot use DateTime.MinValue, MaxValue, or Now in optional parameters because they are not compile time constants.

    Of course, you don’t need to use optional parameters, you can use overloaded methods if you wish to make use of Min, Max, or Now.

    void Foo()
    {
        Foo(DateTime.MinValue);
    }
    
    void Foo(DateTime d)
    {
    }
    

    If you want to go overkill (well, maybe not overkill, plenty of valid reasons to do this), then you could define a new date type that understands when it has a value:

    class SmarterDateTime
    {
        public bool IsSet { get; set; }
    
        // Wrapper around DateTime etc excluded.
    }
    

    As for what should be the default, you can choose to make any date represent a default if you wish, but for things like optional parameters you’ll have limitations.

    Personally, I tend to use DateTime.MinValue.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have normally hand written xml like this: <tag><?= $value ?></tag> Having found tools
Normally I would have one junit test that shows up in my integration server
One would normally have this query in their sphinx.conf file : sql_query = SELECT
In WebForms, I would normally have code like this to let the browser present
I'm doing this query: SomeObject.objects.annotate(something=Avg('something')).order_by(something).all() I normally have an aggregate field in my model
I have a winforms application that normally is at about 2-4% CPU. We are
I have a dictionary that I normally access with a key, so I need
We have an application that is run in multi-monitor environments. Users normally have the
I have a select statement which generates a Type and quantity: I normally have
I have a fact table that has 17 keys. Normally I have been designating

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.