Right way to implement GetHashCode for this struct

前端 未结 7 1877
庸人自扰
庸人自扰 2021-02-13 06:13

I want to use a date range (from one date to another date) as a key for a dictionary, so I wrote my own struct:

   struct DateRange
   {
      public DateTime St         


        
7条回答
  •  旧巷少年郎
    2021-02-13 06:45

    In C# 7 you can do this:

    public override int GetHashCode() => (Start, End).GetHashCode();
    

    The ValueTuple is available in .NET Framework 4.7 and .NET Core, or via NuGet.

    Not sure how well it performs, but I would be surprised if any custom code would beat it.

提交回复
热议问题