简体   繁体   English

为什么有Nullable <T>结构和Nullable类?

[英]Why is there a Nullable<T> struct and Nullable class?

There is a Nullable<T> struct and there is also another static Nullable class with three static methods. 有一个Nullable<T>结构,还有另一个带有三个静态方法的静态Nullable类。

My question is, why can't these static methods in static Nullable class go into Nullable<T> struct? 我的问题是,为什么静态Nullable类中的这些静态方法不能进入Nullable<T> struct? What is the reason for defining them in two different types? 在两种不同类型中定义它们的原因是什么?

And there is a INullable interface as well.What does it do? 并且还有一个INullable接口。它做什么?

  1. Is a standard practice with Generics to have class with the same name of your generic class with utility methods related to the generic class. Generics的标准做法是使用与泛型类相同的实用程序方法使用与泛型类相同的类。 Because when you use a generic class you cant't infer the generics in the type declaration you'll end up with many more characters. 因为当你使用泛型类时,你不能在类型声明中推断出泛型,你最终会得到更多的字符。 Look how it would be if the static methods were placed in the generic class: 看看静态方法放在泛型类中会是怎样的:

     Nullable<int?>.Compare(x, Y); var n = new Tuple<int,string>(10, "Text"); 

    vs VS

     Nullable.Compare(x, Y); var n = Tuple.Create(10, "Text"); 

    I included Tuple as another example. 我把Tuple作为另一个例子。

  2. Interfaces and base classes are very useful in Generics, and since Nullable<> is struct and structs cannot have a base class, we are left with interfaces. 接口和基类在泛型中非常有用,并且因为Nullable <>是struct而struct不能有基类,所以我们留下了接口。 Now here is a possible usage of it. 现在可以使用它。

     { int? a = 10; long? b = 10L; Process(a); Process(b); } private static void Process(INullable value) { if (value.IsNull) { // Process for null .. } else { // Process for value .. } } 

they are written is a separate class because those methods are utility methods and are intended to be used anywhere without actually having to create a nullable structure instance every time you want to use these methods for example, 它们是一个单独的类,因为这些方法是实用程序方法,并且打算在任何地方使用,而无需每次要使用这些方法时实际创建nullablenullable结构实例,例如,

public static bool Is<T>(this T variable,Type type) {
    if (var != null) {
        Type currentType = variable.GetType();
        type = Nullable.GetUnderlyingType(type) ?? type;
        while (currentType != typeof(object)) {
            if (currentType == type) {
                return true;
            }
            currentType = currentType.BaseType;
        }
    }                  
    return false;
}

In the above example the variable can be of any type(not necessarily nullable ), but it can actually be nullable , and hence has to be checked if nullable . 在上面的例子中,变量可以是任何类型(不一定是nullable ),但它实际上可以为nullable ,因此必须检查它是否可以为nullable

For that very case I used the Nullable.GetUnderlyingType(type) , because it is a utility method.This is intended behaviour for utility methods ie making them reusable anywhere required without having to create an instance, every time you need them. 对于这种情况,我使用了Nullable.GetUnderlyingType(type) ,因为它是一个实用方法。这是实用程序方法的预期行为,即在任何需要的地方使它们可重用,而不必在每次需要时创建实例。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM