Relative Content

Tag Archive for c#generics

Generics and possibly circular dependencies

public abstract class Display { } public abstract class Display<TLogic> : Display where TLogic : Logic<Display<TLogic>> { private TLogic logic; protected Display(){ } public void SetLogic(TLogic log)=> logic = log; } public abstract class Logic { } public abstract class Logic<TDisplay> : Logic where TDisplay : Display<Logic<TDisplay>>, new() { private TDisplay display; public void OnStart() […]

Adding 2 different generic types that implement INumber

I am making a Matrix class in C#, and would like for it to be able to include any type that implements the INumber interface. I am unsure of how to be able to implement addition for matrices with different generic types e.g. Matrix<int> and Matrix<float>.

Adding 2 different generic types that implement INumber

I am making a Matrix class in C#, and would like for it to be able to include any type that implements the INumber interface. I am unsure of how to be able to implement addition for matrices with different generic types e.g. Matrix<int> and Matrix<float>.

C generics using void * vs. macros

As I understand it, the standard way to implement generic data types in C is with void pointers. However, an alternate approach would be to use macros. Here’s an example implementation of a generic “Option” type using macros: