在何处以及如何使用 && 运算符

Where and how to use && Operators

我有这段代码是我从我的应用程序中简化而来的。它做我想做的,但我知道它没有以最有效的方式放置,因为我在理解 &&& 运算符时仍然遇到一些问题。

if (AgeInput.Text.Equals(""))
{
    Textblock1.Text = "✘";
}
else if (AgeInput.Text.All(Char.IsNumber)){
    Textblock1.Text = "✔";
    //DO-THIS-A
}
else
{
    Textblock1.Text = "✘";
}

我需要它来确保字符串中没有空格,并检查它是否为空,最后检查它是否是一个数字,如果它符合所有这些要求,它将 //DO-THIS-A

最有效的方法是什么?

编辑: 如果有人知道如何使 XAML 文本框仅包含数字(因此没有空格)那会更好(只有一个 属性 否则不用担心)

if(!String.IsNullOrEmpty(AgeInput.Text) && AgeInput.Text.All(char.IsNumber))
    Textblock1.Text = "✔";
else
    Textblock1.Text = "✘";

String.IsNullOrEmpty returns 如果输入如所述:Null 或 Empty,则为真。

我们用“!”反转它,这样如果它不为空,它 returns 为真。

然后我们添加&&运算符来扩展if条件,询问文本是否只包含数字。

另请看这里:For a description of the difference between &, && and |, ||

不太确定我理解你的问题,因为 && 和 & 的用途完全不同。

if (string.IsNullOrWhiteSpace(AgeInput.Text))
{
    Textblock1.Text = "✘";
}
else if(Char.IsNumber(AgeInput.Text.All))
{
    Textblock1.Text = "✔";
}

&是二元运算符,&&是逻辑运算符。