标签: nullable

C#Nullable类型的编码实践

我从未在C#代码中使用过可空类型.现在我决定通过在我的代码中引入可空类型来改变我的编码实践.

在应用程序编程的情况下,在从普通数据类型转换为可空数据类型时,应该对编码实践进行哪些主要更改?

应该注意哪些方面?

我应该经常注意哪些要点?

c# nullable

20
推荐指数
4
解决办法
9902
查看次数

如何在.net中引入可空类型?

在我们自己的Jon Skeet的C#中,他讨论了模拟值类型的'null'的3种方法:

  • 魔术值(例如最早可能的DateTime被视为'null')
  • 引用类型包装器
  • 布尔标志

提到可空类型使用第三种方法.可空类型究竟如何在引擎盖下工作?

.net c# nullable

20
推荐指数
1
解决办法
3848
查看次数

为什么我不能将通用列表声明为可空?

我试图使用以下代码:

private Nullable<List<IpAddressRange>> ipAddressRangeToBind;
Run Code Online (Sandbox Code Playgroud)

但我收到以下警告:

类型List必须是非可空值类型,以便在泛型类型或方法'System.Nullable'中将其用作参数'T'.

c# null nullable

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

如何检查泛型类型参数是否可为空?

可能重复:
确定通用参数是否为Nullable类型

我正在尝试确定类型参数是否为Nullable.

    public T Get<T>(int index)
    {
        var none=default(T);
        var t = typeof(T);
        BaseVariable v = this[index].Var;
        if (T is Nullable) //compiler error
        {
            if (v == ... )
            {
                return none;
            }
        }
        //....
    }
Run Code Online (Sandbox Code Playgroud)

我该怎么做呢?我尝试过,t == typeof(Nullable)但总是导致错误.

我想要发生的是foo.Get<bool?>(1)有时无效.

.net c# generics nullable

19
推荐指数
1
解决办法
9631
查看次数

启用数据捕获时,在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万
查看次数

为什么as运算符可以与Nullable <T>一起使用?

根据as运营商的文档,as"用于在兼容的参考类型之间执行某些类型的转换".由于Nullable 实际上是一个类型,我希望as不会使用它.但是,此代码编译并运行:

object o = 7;
int i = o as int? ?? -1;
Console.WriteLine(i); // output: 7
Run Code Online (Sandbox Code Playgroud)

这是正确的行为吗?文件是as错的吗?我错过了什么吗?

c# autoboxing nullable

19
推荐指数
1
解决办法
6491
查看次数

C#中具有可空类型的+ =运算符

在C#中,如果我写的话

int? x = null;
x += x ?? 1
Run Code Online (Sandbox Code Playgroud)

我希望这相当于:

int? x = null;
x = x + x ?? 1
Run Code Online (Sandbox Code Playgroud)

因此在第一个例子中,x将包含1如第二个例子中那样.但它没有,它包含null.+ =运算符在尚未分配时似乎不适用于可空类型.为什么会这样呢?

编辑:正如所指出的,这是因为null + 1 = null和运营商的优先级.在我的辩护中,我认为MSDN中的这一行含糊不清!:

预定义的一元和二元运算符以及值类型存在的任何用户定义的运算符也可以由可空类型使用.如果[操作数]中任何一个为空,则这些运算符产生空值; 否则,运算符使用包含的值来计算结果.

c# nullable operators

19
推荐指数
1
解决办法
1241
查看次数

当可选项为空时如何返回?

我喜欢自选是在Java标准库现在.但是我仍然遇到一个基本问题,我还没有弄清楚如何以最好的方式解决(最容易阅读和理解,最漂亮,最短)的方式:

当可选项为空时如何从方法返回?

我正在寻找一个通用的解决方案,适用于选项数和代码块大小的不同组合.

在下面的例子中,我将尝试展示我的意思:

void m1() {
    // When I get an optional:
    Optional<String> o = getOptional();

    // And want to return if it's empty
    if (!o.isPresent()) return;

    // In the whole rest of the method I have to call Optional.get 
    // every time I want the value:
    System.out.println(o.get());

    // Which is pretty ugly and verbose!
}


void m2() {
    // If I instead return null if a value is absent:
    String s = getNullabe();
    if (s == …
Run Code Online (Sandbox Code Playgroud)

java nullable optional

19
推荐指数
4
解决办法
5万
查看次数

我怎么能在打字稿中返回一个可为空的值

在 NPM 模块中,我使用打字稿

  "devDependencies": {
    "@types/node": "^8.0.0",
    "typescript": "^2.8.1"
  }
Run Code Online (Sandbox Code Playgroud)

我想使用公共方法返回一个私有的可为空参数。请参考下面的例子。我看到的错误是

Property 'string1' has no initializer and is not definitely assigned in the constructor.
Run Code Online (Sandbox Code Playgroud)

如果我在构造函数中分配了一个 undefined 我得到了错误

[ts]
Type 'string | undefined' is not assignable to type 'string'.
  Type 'undefined' is not assignable to type 'string'
Run Code Online (Sandbox Code Playgroud)

我应该如何在打字稿中做到这一点,我来自 c# 方面:)

export class HowToDoThis {

    private string1?: string;

    public constructor() {

        //this.string1 = undefined;
    }

    public add2String1(content: string) {

        this.string1 += content;
    }

    public getString1(): string {

        return this.string1;
    }
}
Run Code Online (Sandbox Code Playgroud)

nullable typescript

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

C# Nullable 注解,如果参数不为空,则方法返回不为空

如果输入不为空,如何告诉编译器以下扩展方法返回不为空?

public static string? SomeMethod(this string? input)
{
    if (string.IsNullOrEmpty(input))
        return input;

    // Do some work on non-empty input
    return input.Replace(" ", "");
}
Run Code Online (Sandbox Code Playgroud)

.net c# nullable nullable-reference-types

19
推荐指数
1
解决办法
3641
查看次数