Table of Contents

Class NDArrayNetInterop

Namespace
NumSharp
Assembly
NumSharp.dll

Fluent bridges from NumSharp iteration to built-in .NET types — the "other types (foreach or non-foreach)" companions to the by-ref T/Span<T> walks and to NDArray._Unsafe.

These are the SAFE side of the integration: AsEnumerable<T>(NDArray) gives LINQ/collection interop with UNBOXED elements (only the enumerator is heap-allocated, once, not each element), and the iterator ToArray/CopyTo extensions MATERIALIZE or COPY into caller-owned .NET storage — so, unlike the aliasing nd.Unsafe.* views, the result does not depend on the NDArray staying alive. For a zero-copy aliasing Span<T>/Memory<T> over the buffer, use nd.Unsafe (see Unsafe).

public static class NDArrayNetInterop
Inheritance
NDArrayNetInterop
Inherited Members

Methods

AsEnumerable<T>(NDArray)

Walk the array's elements in logical C-order as an IEnumerable<T> — the LINQ / foreach / collection bridge, with elements UNBOXED (T, not object). Works for EVERY memory layout (transposed, sliced, strided, negative-stride, broadcast) — each element is read through the array's strides — so it needs no contiguity and never copies the whole array. Only the enumerator is a heap object (one allocation per enumeration); no per-element boxing.

double mean = a.AsEnumerable<double>().Average();
var evens   = a.AsEnumerable<int>().Where(x => x % 2 == 0).ToList();

For the fastest, allocation-free element walk prefer foreach (ref T x in np.flat<T>(a)); reach for this when you need LINQ or an IEnumerable<T>-shaped API.

public static IEnumerable<T> AsEnumerable<T>(this NDArray a) where T : unmanaged

Parameters

a NDArray

Returns

IEnumerable<T>

Type Parameters

T

Must be the array's exact element type.

CopyTo<T>(FlatRefIter<T>, Span<T>)

Copy the elements of a np.FlatRefIter<T> into destination (logical C-order). Returns the number of elements copied; throws if too short.

public static int CopyTo<T>(this np.FlatRefIter<T> it, Span<T> destination) where T : unmanaged

Parameters

it np.FlatRefIter<T>
destination Span<T>

Returns

int

Type Parameters

T

CopyTo<T>(NDChunkIter<T>, Span<T>)

Copy the chunks of an np.NDChunkIter<T> into destination (iteration/memory order). Returns the number of elements copied; throws if too short.

public static int CopyTo<T>(this np.NDChunkIter<T> it, Span<T> destination) where T : unmanaged

Parameters

it np.NDChunkIter<T>
destination Span<T>

Returns

int

Type Parameters

T

CopyTo<T>(NDRefIter<T>, Span<T>)

Copy the elements of an np.NDRefIter<T> into destination (memory order). Returns the number of elements copied; throws if the destination is too short.

public static int CopyTo<T>(this np.NDRefIter<T> it, Span<T> destination) where T : unmanaged

Parameters

it np.NDRefIter<T>
destination Span<T>

Returns

int

Type Parameters

T

ToArray<T>(FlatRefIter<T>)

Materialize the elements of a np.FlatRefIter<T> (logical C-order, from np.flat<T>) into a fresh T[].

public static T[] ToArray<T>(this np.FlatRefIter<T> it) where T : unmanaged

Parameters

it np.FlatRefIter<T>

Returns

T[]

Type Parameters

T

ToArray<T>(NDChunkIter<T>)

Materialize the chunks of an np.NDChunkIter<T> (from np.nditer_chunks<T>) into a single fresh T[], concatenated in iteration (memory) order.

public static T[] ToArray<T>(this np.NDChunkIter<T> it) where T : unmanaged

Parameters

it np.NDChunkIter<T>

Returns

T[]

Type Parameters

T

ToArray<T>(NDRefIter<T>)

Materialize the elements of an np.NDRefIter<T> (memory-order, from np.nditer<T>) into a fresh T[].

public static T[] ToArray<T>(this np.NDRefIter<T> it) where T : unmanaged

Parameters

it np.NDRefIter<T>

Returns

T[]

Type Parameters

T