可空类? [英] Nullable class?

查看:124
本文介绍了可空类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个类

public class FilterQuery
    {
        public FilterQuery() { }

        public string OrderBy { set; get; }
        public string OrderType { set; get; }        

        public int? Page { set; get; }
        public int ResultNumber { set; get; }

    }

我想像这样使用

public IQueryable<Listing> FindAll(FilterQuery? filterQuery)

这是否可能?

感谢

推荐答案

这会立即引起为什么的问题。类按照定义引用类型,因此已经可以为空。

This immediately begs the question of why. Classes are by definition reference types and thus are already nullable. It makes no sense to wrap them in a nullable type.

在您的情况下,您可以简单定义:

In your case, you can simply define:

public IQueryable<Listing> FindAll(FilterQuery filterQuery)

并调用 FindAll(null)不传递过滤器查询。

and call FindAll(null) to pass no filter query.

如果你使用C#/ .Net 4.0,一个不错的选项是:

If you're using C#/.NET 4.0, a nice option is:

public IQueryable<Listing> FindAll(FilterQuery filterQuery = null)

这意味着你甚至不必指定null。

which means you don't even have to specify the null. An overload would do the same trick in previous versions.

编辑:每个请求,重载将如下所示:

Per request, the overload would simply look like:

public IQueryable<Listing> FindAll()
{
    return FindAll(null);
}

这篇关于可空类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆