1

https://github.com/qntmfred/FooTimestamp

The following program in the AnyCPU build configuration evaluates maxTimestamp as DateTime.MinValue.

public class Foo
{
    public DateTime Timestamp { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<Foo> foos = new List<Foo>() { new Foo() { Timestamp = new DateTime(2012, 7, 1) } };
        var maxTimestamp = foos.Any() ? foos.Max(x => x.Timestamp) : new DateTime(2012, 7, 2);
    } // put a breakpoint here - maxTimestamp evaluates to DateTime.MinValue
}

* I've been chasing this bug from a couple different angles since I first posted this, so the comments probably don't make much sense. See the revision history if you want to see it all but I've scrapped most of the original post to focus on what I have now simplified this problem to.

kenwarner
  • 28,650
  • 28
  • 130
  • 173
  • EF doesn't apply here any more since you threw the items into a list. – Jeff Mercado Jul 03 '12 at 02:39
  • I didn't think so either, but I listed it anyways just in case somebody knew something I didn't about how EF might be causing this - the objects are still `DynamicProxy`s so I thought it might be plausible – kenwarner Jul 03 '12 at 02:40
  • Looks like it should work to me.. How much simplification have you applied to this code? – Blorgbeard Jul 03 '12 at 02:42
  • As a debugging tip, try using a different value instead of `DataTime.MinValue` and see if you get the value you set. – Jeff Mercado Jul 03 '12 at 02:42
  • good idea @JeffMercado that returned `DateTime.MinValue` too – kenwarner Jul 03 '12 at 02:45
  • @qntmfred, _fooRepository.GetAll().ToList() returns a List, an IList os something else? I'm running the same code here and it's giving the expected results (not this DateTime.MinValue thing). – Andre Calil Jul 03 '12 at 02:51
  • .GetAll() returns an IQueryable, .ToList() then of course returns a List – kenwarner Jul 03 '12 at 02:56
  • As that's the case, it would help to see what your `foo` objects look like. `DateTime.MinValue` corresponds to the default value of a `DateTime`. It looks like something is failing somewhere so you just get back default values for all your items. Verify that the list contains foos that have reasonable values and their timestamps are not set to the default value. – Jeff Mercado Jul 03 '12 at 03:02
  • I can hover inspect `foos` and see recent timestamps in the objects and I still get the `DateTime.MinValue` for `maxTimestamp` – kenwarner Jul 03 '12 at 03:08
  • Is the Foo.Timestamp-property of type DateTime? – Maarten Jul 03 '12 at 06:31
  • Yes Foo.Timestamp is of type DateTime – kenwarner Jul 03 '12 at 14:13
  • are you using some kind of code postprocessing stuff like code contracts? – ie. Jul 03 '12 at 20:06
  • Did you try to use a manually created list instead of pulling the list from the repo, like `var foos = new List { new Foo { Timestamp = ... }, new Foo { Timestamp = ... }, ...}`? – Slauma Jul 03 '12 at 20:39

1 Answers1

1

So it looks like this ended up being a bona fide compiler bug. neat. It's been fixed in .NET 4.5

Bizarre ternary operator behavior in debugger on x64 platform https://connect.microsoft.com/VisualStudio/feedback/details/684202

Community
  • 1
  • 1
kenwarner
  • 28,650
  • 28
  • 130
  • 173