标签: non-nullable

没有NULL我们会做什么?

我曾经读过,拥有可空类型是一种绝对的邪恶.我相信这是由创建它们的人写的一篇文章(在Ada中?)我相信这是文章

无论如何,那么如果默认情况下像C#这样的语言使用非可空类型呢?你会如何替换C#或Ruby中的一些常用习语或任何其他null可接受值的常用语言?

language-agnostic null language-design non-nullable

20
推荐指数
3
解决办法
3159
查看次数

启用数据捕获时,在DB2中使列可为空

我正在使用db2版本9.7*,似乎不可能以任何直接的方式使NOT NULL列可以为空.

不幸的是,使用更加开发人员友好的数据库的解决方案不可用.基本上,在MySQL中,我想做类似的事情(其中MY_COLUMN曾经是VARCHAR(200)NOT NULL):

ALTER TABLE MY_TABLE MODIFY COLUMN MY_COLUMN VARCHAR(200);
Run Code Online (Sandbox Code Playgroud)

db2 nullable alter non-nullable notnull

19
推荐指数
3
解决办法
6万
查看次数

为什么我允许将非可空类型与null进行比较?

可能重复:
C#可以将值类型与null进行比较

如果我尝试null在C#中分配一个不可为空的类型:

System.DateTime time = null;
Run Code Online (Sandbox Code Playgroud)

我会得到一个编译时错误:

错误CS0037:无法将null转换为'System.DateTime',因为它是一个不可为空的值类型

这是有道理的.但如果比较相同的类型null:

System.DateTime time = obtainFromSomewhere();
if( time == null ) {
    //whatever;
}
Run Code Online (Sandbox Code Playgroud)

没有编译时错误.这对我来说没有意义 - 如果我不能分配null那么为什么会这样null呢?

为什么我可以将非可空类型与null?进行比较?

.net c# nullable non-nullable

17
推荐指数
2
解决办法
3544
查看次数

什么时候使用C#结构(值类型)牺牲性能?

我一直在使用结构作为隐式验证复杂值对象的机制,以及围绕更复杂类的通用结构以确保有效值.我对性能影响有点无知,所以我希望你们都能帮助我.例如,如果我要做一些事情,比如将域对象注入值类型包装器,会导致问题吗?为什么?我理解值类型和引用类型之间的区别,我的目标是利用值类型的不同行为.为了负责任地做到这一点,我到底需要考虑什么?

这是我正在思考的一个非常基本的想法.

public struct NeverNull<T>
    where T: class, new()
{

    private NeverNull(T reference)
    {
        _reference = reference;
    }

    private T _reference;

    public T Reference
    {
        get
        {
            if(_reference == null)
            {
                _reference = new T();
            }
            return _reference;
        }
        set
        {
            _reference = value;
        }
    }

    public static implicit operator NeverNull<T>(T reference)
    {
        return new NeverNull<T>(reference);
    }

    public static implicit operator T(NeverNull<T> value)
    {
        return value.Reference;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# performance struct value-type non-nullable

14
推荐指数
2
解决办法
2316
查看次数

在构造函数调用的方法内初始化的不可为空字段

我有一些不可为 null 的字段,我想在从构造函数调用的辅助方法中初始化这些字段,以减少构造函数内部的混乱:

private FlowLayoutPanel _flowPanel;
private ComboBox _printersComboBox;
//...

public PrintSettingsView(IServiceProvider serviceProvider, IPrintSettings printSettings)
{
    InitializeComponent();
    PostInitializeComponent(); // this is where _flowPanel etc get initialized
    // ...
}
Run Code Online (Sandbox Code Playgroud)

如何避免类似的警告Non-nullable field '_flowPanel' must contain a non-null value when exiting constructor. Consider declaring the field as nullable

到目前为止我想到的最好的主意是:

public static void Assert([DoesNotReturnIf(false)] bool condition)
{
  if (!condition) throw new InvalidOperationException();
}

public PrintSettingsView(IServiceProvider serviceProvider, IPrintSettings printSettings)
{
    InitializeComponent();
    PostInitializeComponent();

    Assert(_flowPanel != null);
    Assert(_printersComboBox != null);
    //...
}
Run Code Online (Sandbox Code Playgroud)

当有很多田地时,它仍然变得混乱。还有比这更好的事情吗?

这是一个 …

.net c# non-nullable .net-core nullable-reference-types

13
推荐指数
3
解决办法
2437
查看次数

不可为空的引用类型

我正在设计一种语言,我想知道默认情况下使引用类型不可为空是否合理,并使用"?" 可以为空的值和引用类型.这有什么问题吗?你会怎么做:

class Foo {
    Bar? b;
    Bar b2;
    Foo() {
        b.DoSomething(); //valid, but will cause exception
        b2.DoSomething(); //?
    }
}
Run Code Online (Sandbox Code Playgroud)

language-design non-nullable

12
推荐指数
3
解决办法
2981
查看次数

C#中可空类型的替代方案

我正在编写适用于一系列数字数据的算法,有时,系列中的值必须为null.但是,由于此应用程序对性能至关重要,因此我避免使用可空类型.我已经对这些算法进行了性能测试,专门比较了使用可空类型和非可空类型的性能,在最好的情况下,可空类型的速度慢了2倍,但往往差得多.

最常用的数据类型是double,目前选择null的替代方法是double.NaN.但是我知道这不是NaN值的确切用途,所以我不确定是否有任何问题,我无法预见,最佳做法是什么.

我有兴趣找出以下数据类型的最佳空替代品:double/float,decimal,DateTime,int/long(尽管其他数据类型非常受欢迎)

编辑:我想我需要澄清我对性能的要求.数字数据的演出通过这些算法在几个小时的时间内处理.因此,虽然例如10毫秒或20毫秒之间的差异通常是微不足道的,但在这种情况下,它确实对所花费的时间产生了重大影响.

c# nullable nan non-nullable

12
推荐指数
3
解决办法
3605
查看次数

为什么整数== null是C#中有效的布尔表达式?

为什么integer == nullC#中的有效布尔表达式,如果integer(类型的变量int)不可为空?(我不是反对它,事实上我喜欢它,但我不知道它是可能的)

c# null boolean non-nullable

11
推荐指数
1
解决办法
631
查看次数

C#创建一个不可为空的字符串.可能吗?不知何故?

所以你不能继承string.你不能成为不可空的string.但我想这样做.我想要一个类,让我们称之为nString,否则返回默认值为null.我有JSON对象可能有谁知道有多少空字符串,甚至是空对象.我想创建具有永远不会返回null的字符串的结构.

public struct Struct
{
    public nString value;
    public nString value2;
}
Run Code Online (Sandbox Code Playgroud)

我想我可以这样做:

public struct Struct
{
    public string val { get { return val ?? "N/A"; } set { val = value; } }
    public string val2 { get { return val2 ?? "N/A"; } set { val2 = value; } };
}
Run Code Online (Sandbox Code Playgroud)

但那是更多的工作.有没有办法做到这一点?

c# string non-nullable

11
推荐指数
1
解决办法
9277
查看次数

类型推断在对可空和非可空int的'join'调用中失败

在我的Linq中,我试图将内部联接设置为可以为空的字段.员工和部门有关系,部门可能有EmployeeID或者可能有null.那么什么是我的连接,如果我只想要满足内部连接的记录(没有结果为null EmployeeIDs):

var result = from emp in employees
             join dept in departments
             on new { Source = emp.EmployeeID }
             equals new { Source = dept.EmployeeID };
Run Code Online (Sandbox Code Playgroud)

我得到一个例外:

join子句中某个表达式的类型不正确.在"加入"调用中类型推断失败.

谢谢

c# linq entity-framework nullable non-nullable

9
推荐指数
3
解决办法
2万
查看次数