# 你觉得那个代码实现更好，为什么呢？

0

//片段1

``````        public static bool IsOverlapping(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
{

if (start2 >= start1 && start2 <= end1 || end2 >= start1 && end2 <= end1)
{
return true;
}
else
{
return false;
}

}``````

//片段2

``````        public static bool IsOverlapping(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
{

if (start1 == start2 || end1 == end2)
{
return true;
}

DateTime[] array = new DateTime[] { start1, end1, start2, end2 };
int[] indices = Enumerable.Range(0, 4).ToArray();
Array.Sort(array, indices);
return indices[1] - indices[0] != 1 && indices[3] - indices[2] != 1 && array[indices[1]] != array[indices[2]];
}``````
czd890 | 专家六级 | 园豆：10438

0

1 好一些。

``````public static bool IsOverlapping(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
{
return IsInRange(start1, end1, start2) || IsInRange(start1, end1, end2);
}

public static bool IsInRange(DateTime start, DateTime end, DateTime x)
{
return x >= start && x <= end;
}``````

``````public static bool IsOverlapping((DateTime start, DateTime end) range1, (DateTime start, DateTime end) range2)
{
return IsInRange(range2.start, range1) || IsInRange(range2.end, range1);
}

public static bool IsInRange(DateTime x, (DateTime start, DateTime end) range)
{
return x >= range.start && x <= range.end;
}``````

``````public struct DateTimeRange
{
public DateTime Start { get; }
public DateTime End { get; }

public DateTimeRange(DateTime start, DateTime end)
{
this.Start = start;
this.End = end;
}

public bool IsIn(DateTime value)
{
return value >= Start && value <= end;
}

public bool IsOverlapping(DateTimeRange other)
{
return IsIn(other.Start) || IsIn(other.End);
}
}``````

blackheart | 老鸟四级 |园豆：3592 | 2019-09-18 11:23

2

``````bool FuckMe(start1, end1, start2, end2){
return  !(end2 < start1 || start2 > end1)
}``````

0

t1=s1~e1
t2=s2~e2
t2包含在t1段内；

您需要登录以后才能回答，未注册用户请先注册