45

I have a property that returns two items of type DateTime. When returning these values I have to reference them as Item1 and Item2. How do I return with custom names e.g.

filter?.DateRanges.From
filter?.DateRanges.To

 

public Tuple<DateTime, DateTime> DateRanges
{
    get
    {
        From = DateTime.Now.AddDays(-1).Date.AddMonths(-1);
        To = DateTime.Now.AddDays(-1).Date;

        if (Preset != 0)
        {
            if (Preset == DatePreset.SpecificRange)
            {
                From = From.Date;
                To = To.Date;
            }
            else
            {
                var dateRange = DateTime.Today.AddDays(-1).GetDateRangeByPreset(Preset);
                From = dateRange.From;
                To = dateRange.To;
            }
        }

        return new Tuple<DateTime, DateTime>(From, To);
    }

usage:

var from = filter?.DateRanges.Item1;
var to = filter?.DateRanges.Item2;
hellow
  • 12,430
  • 7
  • 56
  • 79
Karim Ali
  • 463
  • 1
  • 4
  • 7
  • 3
    Possible duplicate of [Better naming in Tuple classes than "Item1", "Item2"](https://stackoverflow.com/questions/7745938/better-naming-in-tuple-classes-than-item1-item2) – JasperMoneyshot Aug 03 '18 at 10:41
  • AFAIK there is no such thing as "named tuples". The alternative is to create a custom class with the property names you want – Rui Jarimba Aug 03 '18 at 10:42
  • 3
    @RuiJarimba: There are definitely C# tuples with named elements, as of C# 7. They use `ValueTuple<>` rather than `Tuple<>` though. – Jon Skeet Aug 03 '18 at 10:44
  • Oh that's nice, I wasn't aware of that new feature. Thanks @DaisyShipton – Rui Jarimba Aug 03 '18 at 10:47

2 Answers2

75

Like this:

public (DateTime Start, DateTime End) DateRanges
{
    get
    {
        return (DateTime.MinValue, DateTime.MaxValue);
    }
}

Note: This requires a recent version of C# and .Net.

Incidentally, watch out for this usage pattern:

var from = filter?.DateRanges.Start;
var to = filter?.DateRanges.End;

That's inefficient because it causes two identical tuples to be created.

This is better:

var range = filter?.DateRanges;

if (range.HasValue)
{
    var from  = range.Value.Start;
    var to    = range.Value.End;
}

However note that tuples cannot be null (they are value types) so you could write it like so:

if (filter != null)
{
    var range = filter.DateRanges;
    var from  = range.Start;
    var to    = range.End;
    ...
}

ADDENDUM (2022):

Nowadays you can much more simply assign the values of a tuple to local variables. Now we can rewrite the last example above like so:

if (filter != null)
{
    var (from, to) = filter.DateRanges;
    ...
}
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • strangely your second method does not work, var range = filter?.DateRanges; range variable doesn't allow Start and End – Karim Ali Aug 03 '18 at 10:51
  • @KarimAli Ah, that's because it's a nullable (with the `?`) so I should have been using `.Value`. I've edited my answer. But actually it's pointless using a nullable here because tuples CANNOT be null. – Matthew Watson Aug 03 '18 at 10:53
  • This is not what was asked. Your answer is more than a syntax difference, it's a valuetype not reference which the Tuple<> syntax. – John Stock Sep 05 '19 at 01:08
  • @JohnStock I pointed out that it's a value type in my answer where I said `(they are value types)`, so the fact that they are value types should be a surprise to no-one. I even bolded the part where I said `tuples cannot be null`. – Matthew Watson Sep 05 '19 at 08:07
  • @MatthewWatson I appreciate you pointing out how the code can be misused (made less efficient) by calling the `DateRanges` method twice thereby creating 2 tuples. And furthermore providing a better "usage" recommendation. While the fact you are creating 2 tuples is somewhat obvious by inspection, it can be easily missed - especially when you are using code written by someone else! – Dave Black Jul 22 '20 at 18:18
3
class Program
{
    static void Main(string[] args)
    {
        test t = new test();
        Console.WriteLine(t.NamedTuple.start);
        Console.WriteLine(t.NamedTuple.stop);
        Console.Read();
    }
}

class test
{
    DateTime From;
    DateTime To;

    public (DateTime start, DateTime stop) NamedTuple
    {
        get
        {
            From = DateTime.Now.AddDays(-1).Date.AddMonths(-1);
            To = DateTime.Now.AddDays(-1).Date;
            return (From, To);
        }

    }
}
Kolkil Kolk
  • 156
  • 1
  • 10