Table of Contents

Class NDScalarClassCalls

Namespace
NumSharp
Assembly
NumSharp.dll

NumPy's class calls (np.float32(1.5f), np.int8(), np.complex128(1, 2), np.str_(5)) as C# 14 static extension members of np, so each concrete scalar-class name is both a VALUE - the DType field np.float32, unchanged - and a CALL that constructs NumPy's scalar. Reach them through np..

public static class NDScalarClassCalls
Inheritance
NDScalarClassCalls
Inherited Members

Remarks

How the two meanings coexist. C# invokes only a method group or a delegate-typed value, and np cannot declare a method beside its same-named field (CS0102). When a name is INVOKED, C#'s member lookup drops the non-invocable members first (ECMA-334 12.5): for np.float32(x) that leaves no member of np, so the lookup moves on to the extension members and binds the one here. Without parentheses, np.float32 finds the field: the same DType object as always, every member intact.

What a call returns. NumSharp's NumPy scalar: a read-only 0-d NDArray of the class's dtype (NumSharp.NDArray.PyArray_Return()). Where NumPy's constructor returns an ndarray - a list or an array argument (np.float32(new object[] { 1, 2 })) - the call returns that array. The conversion is NumPy's constructor (NumSharp.ArrTypeNew): a C# number is NumPy's Python scalar, packed with NEP 50's range check (np.int8(300) raises OverflowError); an NDArray (a NumPy scalar or an array) is cast (np.uint8(np.int64(300)) is 44); a string is parsed (np.int8("12")); omitting the argument gives the zero, and null is Python's None (np.float64(null) is nan).

Who can call it. The call needs C# 14 at the CALL SITE (the .NET 10 SDK, for any target framework, net8.0 included). A C# 12 consumer compiles every value use and gets CS9202 on the call; it writes NDScalarClassCalls.float32(x) instead.

Footguns, all compile-time. Under using static NumSharp.np; a bare float32(x) is CS1955: write np.float32(x), or import this class (using static NumSharp.NDScalarClassCalls;). A method-group conversion (Func<object, NDArray> f = np.float32;) finds the field (CS0029): use a lambda. A dynamic argument is CS1973 (extension members are not dispatched dynamically): cast it to object.

Only NumPy's 36 class names with a NumSharp dtype have a call. The abstract classes (np.floating, ...) cannot be instantiated in NumPy either; the classes without a NumSharp dtype (complex64, datetime64, void, object_) come with their dtypes; NumPy 1.x's removed aliases (np.float_, np.bool8, ...) and NumSharp's own dtypes (np.@decimal) have none.