标签: null-coalescing-operator

null-coalescence运算符是否匹配空字符串?

我有一个非常简单的C#问题:处理空字符串时,以下语句是否相等?

s ?? "default";
Run Code Online (Sandbox Code Playgroud)

要么

(!string.IsNullOrEmpty(s)) ? s : "default";
Run Code Online (Sandbox Code Playgroud)

我认为:因为string.Empty!=null,当我真正想要的是第二个时,合并运算符可以将第一个语句的结果设置为空值.因为字符串是特殊的(==和!=重载到值比较)我只是想请C#专家确认.

谢谢.

c# null-coalescing-operator

4
推荐指数
1
解决办法
2149
查看次数

Null-Coallescing运算符 - 为什么选择铸造?

任何人都可以请告诉我为什么以下第一个语句抛出编译错误而第二个不抛出?

NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, SomeString ?? DBNull.Value); // <-- Throws compilation error!
NewDatabase.AddInParameter(NewCommand, "@SomeString", DbType.String, (object)(SomeString) ?? DBNull.Value); // <-- Compiles!
Run Code Online (Sandbox Code Playgroud)

我尝试了其他可以为空的类型,byte?并得到了相同的结果.任何人都可以告诉我为什么我需要先投射到物体?

.net c# casting nullable null-coalescing-operator

4
推荐指数
2
解决办法
397
查看次数

有没有办法实现和使用"NOT null coalescing"运算符?

是否有一个 空的合并运算符,在C#这种情况下可以使用如下:

public void Foo(string arg1)
{
    Bar b = arg1 !?? Bar.Parse(arg1);   
}
Run Code Online (Sandbox Code Playgroud)

以下案例让我想到了:

public void SomeMethod(string strStartDate)
{
    DateTime? dtStartDate = strStartDate !?? DateTime.ParseExact(strStartDate, "dd.MM.yyyy", System.Globalization.CultureInfo.InvariantCulture);
}
Run Code Online (Sandbox Code Playgroud)

我可能没有strStartDate信息,以防万一,null但如果我这样做; 我总是确定它将是预期的格式.因此,而不是初始化dtStartDate = null并尝试parsetry catch块内设置值.它似乎更有用.

我想答案是否定的(并且没有这样的运算符!??或其他任何东西)我想知道是否有一种方法可以实现这种逻辑,它是否值得以及它会变得有用的情况.

.net c# null-coalescing-operator null-coalescing

4
推荐指数
1
解决办法
178
查看次数

有没有一种巧妙的方法可以在 JavaScript 中进行未定义的合并?

我正在寻找一种在 javascript 中使用布尔值进行未定义合并的方法。我习惯于执行以下操作,例如,正整数:

var x = i1 || i2 || i3 || i4;
Run Code Online (Sandbox Code Playgroud)

这是一种“巧妙”的方式,但不是我所追求的。我正在寻找??C# 中的等效项。

var b1 = undefined;
var b2 = null;
var b3 = false;
var b4 = true;
var x = b1 || b2 || b3 || b4; //x == true (b4)
Run Code Online (Sandbox Code Playgroud)

但是,我希望上面的内容在 false 上“停止”(在未定义或 null 上合并,但不是 false)。我正在执行此操作的应用程序类似于以下内容:

var threshold = argv[2] ?? process.env.threshold ?? config.threshold ?? 0.3;
var verbose = isTrue(argv[3] ?? process.env.verbose ?? config.verbose ?? false);
Run Code Online (Sandbox Code Playgroud)

我正在寻找一种巧妙的方法来做到这一点,类似于||运算符,而不是:

var verbose …
Run Code Online (Sandbox Code Playgroud)

javascript null undefined null-coalescing-operator

4
推荐指数
1
解决办法
1098
查看次数

PHP null合并+三元运算符奇怪的行为

将新的 PHP7 空合并运算符与三元运算符结合使用时,我遇到了意外行为。

具体情况(伪代码):

function a()
{
    $a = 1;
    $b = 2;
    return $b ?? (false)?$a:$b;
}

var_dump(a());
Run Code Online (Sandbox Code Playgroud)

结果是 int(1)。

谁能解释我为什么?

php ternary-operator null-coalescing-operator

4
推荐指数
1
解决办法
393
查看次数

JavaScript:理解 ?? 中的 void 0 运算符填充

我很好奇空合并运算符??在默认设置中是否有填充create-react-app。事实证明确实如此:

const App = () => {
    const foo = 'custom value';

    return (
        <div>
            {foo ?? 'default value'}
        </div>
    );
};
Run Code Online (Sandbox Code Playgroud)

变成:

const App = () => {
    const foo = 'custom value';

    return (
        <div>
            {foo ?? 'default value'}
        </div>
    );
};
Run Code Online (Sandbox Code Playgroud)

最有趣的部分是foo !== null && foo !== void 0 ? foo : 'default value',它是运算符的 polyfill ??。虽然foo !== null很明显,但我不太明白这foo !== void 0部分。这是什么意思?

我检查了一下,void 0 === undefined …

javascript null-coalescing-operator reactjs

4
推荐指数
1
解决办法
874
查看次数

在您看来,什么更具可读性?(运营商)或使用if

我有一个方法会收到一个string,但在我可以使用它之前,我必须将其转换为int.有时它可以null,我必须改变它的价值"0".今天我有:

public void doSomeWork(string value)
{
   int SomeValue = int.Parse(value ?? "0"); //it can throw an exception(i know)
}
Run Code Online (Sandbox Code Playgroud)

我做到了,但我的老板让我重构它:

public void doSomeWork(string value)
{
    if(string.IsNullOrEmpty(value))
        value = "0";
    int SomeValue = int.Parse(value);
}
Run Code Online (Sandbox Code Playgroud)

在您看来,什么是最好的选择?

c# refactoring coalesce null-coalescing-operator

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

在C#中有更简单的方法吗?(null-coalescing type question)

有更简单的方法吗?

string s = i["property"] != null ? "none" : i["property"].ToString();
Run Code Online (Sandbox Code Playgroud)

注意它和null-coalesce(??)之间的区别是在返回之前访问not-null值(op的第一个操作数).

c# null if-statement null-coalescing-operator

3
推荐指数
1
解决办法
205
查看次数

c#这条线是什么意思?

有人可以解释下面的代码return total ?? decimal.Zero吗?

public decimal GetTotal()
{
    // Part Price * Count of parts sum all totals to get basket total
    decimal? total = (from basketItems in db.Baskets
                      where basketItems.BasketId == ShoppingBasketId
                      select (int?)basketItems.Qty * basketItems.Part.Price).Sum();
    return total ?? decimal.Zero;
}
Run Code Online (Sandbox Code Playgroud)

这是否意味着以下?

    if (total !=null) return total;
    else return 0;
Run Code Online (Sandbox Code Playgroud)

c# return-value null-coalescing-operator

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

有没有办法在C#中对属性执行某种null合并?

我不太确定如何表达这个问题,但场景如下:

说我有以下课程:

public class SampleClass
{
    public int Number { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我知道你可以null合并包含类:

SampleClass newSampleClass = possibleNullSampleClass ?? notNullSampleClass;
Run Code Online (Sandbox Code Playgroud)

有没有办法在属性上执行一种null合并,所以我不必这样做:

int? num = sampleClass != null ? new int?(sampleClass.Number) : 5;
Run Code Online (Sandbox Code Playgroud)

看起来像???运算符之类的东西执行此检查是非常有用的,所以我可以这样做:

int? num = sampleClass.Number ??? 5;
Run Code Online (Sandbox Code Playgroud)

在C#中有可能是这样的吗?

c# syntax null-coalescing-operator

3
推荐指数
1
解决办法
124
查看次数