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

Commit

Permalink
Adding null check for implicit cast from array/arraysegment to Span (#…
Browse files Browse the repository at this point in the history
…15044)

Signed-off-by: dotnet-bot-corefx-mirror <dotnet-bot@microsoft.com>
  • Loading branch information
ahsonkhan authored and dotnet-bot committed Jan 13, 2018
1 parent bdc2bbf commit babb960
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/Common/src/CoreLib/System/ReadOnlySpan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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'.
Expand Down
5 changes: 3 additions & 2 deletions src/Common/src/CoreLib/System/Span.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}"/>
Expand Down

0 comments on commit babb960

Please sign in to comment.