简体   繁体   English

C#泛型:成员的默认非空值

[英]C# generics: default non-null value for member

In this generic "class": 在这个通用的“类”中:

public class Param<ValueType>{
    public ValueType value;
}

When ValueType is string I want value to be non-null for all newly created instances of class without explicitly passing default value through constructor. ValueTypestring我希望所有新创建的类实例的value都为非空,而无需通过构造函数显式传递默认值。 In my scenario ValueType can only be int , float , bool , string or struct (that doesn't need to be enforced). 在我的场景中, ValueType只能是intfloatboolstringstruct (不需要强制执行)。

How can I do that? 我怎样才能做到这一点?

When I try to do this: 当我尝试这样做时:

public class Param<ValueType> where ValueType: new(){
    public ValueType value = new ValueType();
}

class stops accepting strings. 类停止接受字符串。

When I remove new() constraint, I can no longer use new ValueType() (obviously) and value is null for all new instances of Param<string> . 当我移除new()约束时,我不能再使用new ValueType() (显然),并且对于Param<string>所有新实例, value为null。

What can I do about it? 我该怎么办?

The reason why I want this to happen is because I have .Equals() defined which does value.Equals(other.value) , which causes exception to be thrown when value is null. 我之所以想发生这种情况,是因为我定义了.Equals()来执行value.Equals(other.value) ,当value为null时,这会引发异常。

You could: 你可以:

public class Param<ValueType>
{
    public ValueType value = typeof(ValueType) == typeof(string) ? (ValueType)(object)string.Empty : default(ValueType);
}

note that default(int) == 0 and so on. 请注意, default(int) == 0等等。

Ah and for the comparison, you should: 嗯,为了进行比较,您应该:

bool areEqual = EqualityComparer<ValueType>.Equals(value, other.Value);

It correctly works for null values. 它适用于null值。

Note even that with generics, this: 即使使用泛型,也要注意:

if (value == null)
{

}

is perfectly valid code. 是完全有效的代码。 Clearly it will be true only if ValueType can be null and is null (so for ValueType == int it won't ever happen) 显然,只有当ValueType可以为null且为null它才会为真(因此对于ValueType == int它将永远不会发生)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#中的非空类默认 - Non-null class default in C# 为什么C#在转换后将实例的先前非null成员视为null? - Why does C# consider a previously non-null member of an instance as null after an upcast? 用于将具有非null值的多个属性收集到集合中的简明C#代码? - Concise C# code for gathering several properties with a non-null value into a collection? 将C#非空参数附加到字符串 - C# non-null parameters to be appended to a string C# 中是否有“true or throw”或“non-null or throw”表达式? - Is there a "true or throw" or "non-null or throw" expression in C#? 如何将NULL传递给具有NON-NULL默认值的SQL Server存储过程参数 - How to pass null to SQL Server stored procedure param with NON-NULL default value C#List,删除* last非null元素后的所有空值* - C# List, Removing all null values *after* last non-null element 为什么我会收到这些奇怪的 C# 8 “可空引用类型”警告:在退出构造函数之前,不可空字段必须包含非空值。? - Why am I getting these strange C# 8 “nullable reference types” warnings: Non-nullable field must contain a non-null value before exiting constructor.? 在c#中将默认值设置为成员类 - Set default value to member class in c# 有没有办法获取 C# DataTable 中每一列的最后一个非空值并将它们显示为单行? - Is there a way to get the last non-null values of each column in a C# DataTable and display them as a single row?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM