gpt4 book ai didi

c# - 从泛型方法返回 null

转载 作者:太空狗 更新时间:2023-10-29 18:08:48 29 4
gpt4 key购买 nike

如何从泛型方法返回空值?

protected T ValueOrDefault<T>(IDataReader reader, int ordinalId)
{
Type t = typeof(reader.GetValue(ordinalId));
if (t.IsValueType){
//Struct. How do I return null?
} else {
//Class
//just return null
return default(T);
}
}

最佳答案

default(T) 适用于这两种情况。

关于c# - 从泛型方法返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5224763/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com