I want to check if textbox is not empty.
Is there a better (more elegant, simpler) way than:
String.IsNullOrWhiteSpace(null == txtBox ? null : txtBox.Text)
It is worth to note that String.IsNullOrWhiteSpace(txtBox.Text)
throws NullReferenceException
if txtBox
is null
.
Not really - however, you could make a little extension if it would save you time:
public static class TextBoxExtensions
{
public static bool IsEmpty(this TextBox textBox)
{
return string.IsNullOrWhiteSpace(null == textBox ? null : textBox.Text);
}
}
usage:
if(TextBox1.IsEmpty())
{
....
假设您的txtBox
可以为null(我想这是一个动态创建的控件),则可以执行以下操作:
bool isEmptyOrNull = txtBox == null || string.IsNullOrWhiteSpace(txtBox.Text)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.