简体   繁体   English

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

[英]Where and how to use && Operators

I have this piece of code which i simplified from my app. 我有这段代码,是从我的应用程序简化的。 It does what I want but I know its not put in the most efficient way because I'm still having some trouble understanding the && and & operators. 它可以实现我想要的功能,但是我知道它没有以最有效的方式放置,因为我在理解&&&运算符时仍然遇到一些麻烦。

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

I need it to make sure there is no white spaces in the string and to also check so its not empty and finally check if its a number, If it ticks all those requirements it will //DO-THIS-A 我需要它来确保字符串中没有空格,并检查它是否不为空,最后检查它是否是一个数字,如果它满足所有要求,它将// DO-THIS-A

Whats the most efficient way to do this? 最有效的方法是什么?

EDIT: If somebody knows how to make a XAML textbox numerical only (so no whitespaces) that would be even better (only a single property or don't worry otherwise) 编辑:如果有人知道如何仅使数字XAML文本框(因此没有空格)会更好(仅单个属性,否则不必担心)

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

String.IsNullOrEmpty returns true if the input is as stated: Null or Empty. 如果输入如以下所述,则String.IsNullOrEmpty返回true:Null或Empty。

We Invert that with the " ! ", so that it returns true if it isn't empty. 我们使用“ ! ”将其取反,因此如果不为空,则返回true。

Then we add the && Operator to expand the if condition and ask if the text only contains numbers. 然后,我们添加&&运算符以扩展if条件,并询问文本是否仅包含数字。

Also look here: For a description of the difference between &, && and |, || 也请看这里: 有关&,&&和|,||之间的区别的描述。

Not really sure I understand your question, because && and & are for totally different uses. 不确定我是否理解您的问题,因为&&和&用于完全不同的用途。

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

The & is a binary operator and && is a logical operator. &是二进制运算符, &&是逻辑运算符。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM