Questions tagged «type-constraints»



2
具有“任何泛型类型”定义的C#泛型“ where约束”?
让我举个例子: 我有一些通用的类/接口定义: interface IGenericCar< T > {...} 我还有另一个要与上面的类相关的类/接口,例如: interface IGarrage< TCar > : where TCar: IGenericCar< (**any type here**) > {...} 基本上,我希望我的通用IGarrage依赖IGenericCar,无论它是IGenericCar<int>还是IGenericCar<System.Color>,因为我对该类型没有任何依赖。

5
如何定义基本类型的泛型类型限制?
我有以下具有通用类型的方法: T GetValue<T>(); 我想将T限制为基本类型,例如int,string,float,但不限于类类型。我知道我可以为这样的类类型定义泛型: C GetObject<C>() where C: class; 我不确定原始类型是否可行,如何确定。

2
C#不能使`notnull`类型为可空
我正在尝试创建类似于Rust Result或Haskell的类型Either并且我已经了这一点: public struct Result<TResult, TError> where TResult : notnull where TError : notnull { private readonly OneOf<TResult, TError> Value; public Result(TResult result) => Value = result; public Result(TError error) => Value = error; public static implicit operator Result<TResult, TError>(TResult result) => new Result<TResult, TError>(result); public static implicit operator Result<TResult, …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.