Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

Commit

Permalink
Adding null check for implicit cast from array/arraysegment to Span (#…
Browse files Browse the repository at this point in the history
ahsonkhan authored Nov 15, 2017
1 parent bd3fc01 commit b6257c4
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/mscorlib/shared/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
@@ -261,12 +261,13 @@ public override int GetHashCode()
/// <summary>
/// Defines an implicit conversion of an array to a <see cref="ReadOnlySpan{T}"/>
/// </summary>
public static implicit operator ReadOnlySpan<T>(T[] array) => new ReadOnlySpan<T>(array);
public static implicit operator ReadOnlySpan<T>(T[] array) => array != null ? new ReadOnlySpan<T>(array) : default;

/// <summary>
/// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="ReadOnlySpan{T}"/>
/// </summary>
public static implicit operator ReadOnlySpan<T>(ArraySegment<T> arraySegment) => new ReadOnlySpan<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
public static implicit operator ReadOnlySpan<T>(ArraySegment<T> arraySegment)
=> arraySegment.Array != null ? new ReadOnlySpan<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count) : default;

/// <summary>
/// Forms a slice out of the given read-only span, beginning at 'start'.
5 changes: 3 additions & 2 deletions src/mscorlib/shared/System/Span.cs
Original file line number Diff line number Diff line change
@@ -342,12 +342,13 @@ public override int GetHashCode()
/// <summary>
/// Defines an implicit conversion of an array to a <see cref="Span{T}"/>
/// </summary>
public static implicit operator Span<T>(T[] array) => new Span<T>(array);
public static implicit operator Span<T>(T[] array) => array != null ? new Span<T>(array) : default;

/// <summary>
/// Defines an implicit conversion of a <see cref="ArraySegment{T}"/> to a <see cref="Span{T}"/>
/// </summary>
public static implicit operator Span<T>(ArraySegment<T> arraySegment) => new Span<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count);
public static implicit operator Span<T>(ArraySegment<T> arraySegment)
=> arraySegment.Array != null ? new Span<T>(arraySegment.Array, arraySegment.Offset, arraySegment.Count) : default;

/// <summary>
/// Defines an implicit conversion of a <see cref="Span{T}"/> to a <see cref="ReadOnlySpan{T}"/>

0 comments on commit b6257c4

Please sign in to comment.