CollectionsMarshal.AsSpan allow null refs (dotnet/coreclr#26903)
[mono-project.git] / netcore / System.Private.CoreLib / shared / System / Runtime / InteropServices / CollectionsMarshal.cs
blob8b38858d084f41d5e092a8c35c9a6ac9fc723532
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
5 using System.Collections.Generic;
7 namespace System.Runtime.InteropServices
9 /// <summary>
10 /// An unsafe class that provides a set of methods to access the underlying data representations of collections.
11 /// </summary>
12 public static class CollectionsMarshal
14 /// <summary>
15 /// Get a <see cref="Span{T}"/> view over a <see cref="List{T}"/>'s data.
16 /// Items should not be added or removed from the <see cref="List{T}"/> while the <see cref="Span{T}"/> is in use.
17 /// </summary>
18 public static Span<T> AsSpan<T>(List<T>? list)
19 => list is null ? default : new Span<T>(list._items, 0, list._size);