Developer knowledge network · moderated exchange

UnreliableCode 커뮤니티

개발자 연구, 리버스 엔지니어링 및 코딩 커뮤니티

Knowledge index살다
4Categories
919Threads
2.8K게시물
Tutorial

C# 11 Generic Math (INumber<T>) for 3D Vector & Matrix Arithmetic

dotnet_native_aot
C# Veteran
MEMBER
대표: 174
가입 날짜: Sep 2021
게시물: 16
감사해요: 62
1개월 전 · Jul 4, 2026 6:54 PM
#1

In C# 11, static abstract interface members enable writing generic math classes that work on float, double, and Half simultaneously:

CSHARP
public readonly record struct Vector3<T>(T X, T Y, T Z) where T : INumber<T>, IRootFunctions<T> {
    public static Vector3<T> operator +(Vector3<T> a, Vector3<T> b) => new(a.X + b.X, a.Y + b.Y, a.Z + b.Z);
    public static Vector3<T> operator -(Vector3<T> a, Vector3<T> b) => new(a.X - b.X, a.Y - b.Y, a.Z - b.Z);
    public T LengthSquared() => X * X + Y * Y + Z * Z;
    public T Length() => T.Sqrt(LengthSquared());
}

One single struct definition handles any numeric precision with JIT monomorphization!

matrix_math_guy
Math Specialist
MEMBER
대표: 105
가입 날짜: Aug 2018
게시물: 52
감사해요: 41
1개월 전 · Jul 4, 2026 8:21 PM
#2

Generic Math in C# is so elegant. Eliminates code duplication between Vector3f and Vector3d.

ptr_arithmetic
C++ Wizard
MEMBER
대표: 162
가입 날짜: May 2018
게시물: 73
감사해요: 42
1개월 전 · Jul 5, 2026 11:05 AM
#3

JIT generates identical machine code to dedicated float structs.