.text =="" vs .length == 0 [英] .text == "" vs .length==0
问题描述
我正在尝试学习用c做一些事情的最佳方法#
测试空字符串时,测试它的最佳方法是什么。
例如,如果我有一个名为txtValue的文本框
我应该使用
txtValue.Text =="" < br $>
或
txtValue.Length == 0
为什么我会使用一个其他。如果没关系,
也让我知道。
谢谢
" j_ruez" <Ĵ_ **** @ yahoo.com>在留言中写道
news:17 ************************** @ posting.google.c om ...测试空字符串时,测试它的最佳方法是什么。
例如,如果我有一个名为txtValue的文本框
我应该使用
txtValue.Text ==""
txtValue.Length == 0
>
更具描述性,可能是平台安全的方式可能是:
txtValue.Text == string.Empty
但是,请记住,字符串也可以为空,所以你可能想要检查这个也是
!
托宾
j_ruez,
我个人会在使用
一般的字符串时使用以下内容:
>
if(pstrString == null || pstrString.Length == 0)
//在这里执行操作。
原因是你不能保证总是持有
参考nce到字符串。现在你关于如何使用null
引用的逻辑可能与其他引用不同,但你应该把它带到
考虑。
另外,TextBox类可能总是返回一个空字符串,其中
的情况下,我只是检查长度(它应该比检查
对比更快)空字符串,你应该使用String.Empty来表示。
希望这会有所帮助。
-
- 尼古拉斯Paldino [.NET / C#MVP]
- 尼克(点)paldino =在= exisconsulting<点> COM
" j_ruez" <Ĵ_ **** @ yahoo.com>在留言中写道
news:17 ************************** @ posting.google.c om ...我正在尝试学习在c中做一些事情的最佳方法#
在测试空字符串时,测试它的最佳方法是什么。
例如,如果我有一个名为txtValue的文本框
我是否应该使用
txtValue.Text ==""
或
txtValue.Length == 0
为什么我会使用一个而不是另一个。如果没关系,
也让我知道。
谢谢
最高性能的方法是检查长度== 0.
-
米奇威廉姆斯
作者,Microsoft Visual C# .NET Core Reference,MS Press
www.servergeek.com
" Tobin Harris" <共********** @ hotmail.com>在消息中写道
news:bl ************ @ ID-135366.news.uni-berlin.de ..." ; j_ruez" <Ĵ_ **** @ yahoo.com>在消息中写道
新闻:17 ************************** @ posting.google.c om ...测试空字符串时,测试它的最佳方法是什么。
例如,如果我有一个名为txtValue的文本框
我应该使用
txtValue.Text ==""
txtValue.Length == 0
更具描述性,可能是平台安全的方式:
txtValue.Text == string.Empty
但是,请记住,字符串也可以为null,所以你可以想要检查这个!
托宾
I am trying to learn the best ways to do some things in c#
When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue
Should I use
txtValue.Text == ""
or
txtValue.Length == 0
and why would I use one over the other. And if it doesn''t matter,
just let me know that also.
Thanks
"j_ruez" <j_****@yahoo.com> wrote in message
news:17**************************@posting.google.c om...When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue
Should I use
txtValue.Text == ""
or
txtValue.Length == 0
A more descriptive, and potentially platform-safe way could be this:
txtValue.Text == string.Empty
However, bear in mind that strings can also be null, so you may want to
check this also!
Tobin
j_ruez,
I personally would use the following when working with strings in
general:
if (pstrString == null || pstrString.Length == 0)
// Perform operation here.
The reason for this is that you are not guaranteed to always hold a
reference to the string. Now your logic for what to do with a null
reference might be different than others, but you should take it into
consideration.
Also, the TextBox class might always return an empty string, in which
case, I would just check the length (it should be quicker than checking
against the empty string, which you should use "String.Empty" for as well).
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- nick(dot)paldino=at=exisconsulting<dot>com
"j_ruez" <j_****@yahoo.com> wrote in message
news:17**************************@posting.google.c om...I am trying to learn the best ways to do some things in c#
When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue
Should I use
txtValue.Text == ""
or
txtValue.Length == 0
and why would I use one over the other. And if it doesn''t matter,
just let me know that also.
Thanks
The most performant way is to check for Length == 0.
--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com
"Tobin Harris" <co**********@hotmail.com> wrote in message
news:bl************@ID-135366.news.uni-berlin.de..."j_ruez" <j_****@yahoo.com> wrote in message
news:17**************************@posting.google.c om...When testing for an empty string, what is the best way to test it.
For example, if I have a textbox named txtValue
Should I use
txtValue.Text == ""
or
txtValue.Length == 0
A more descriptive, and potentially platform-safe way could be this:
txtValue.Text == string.Empty
However, bear in mind that strings can also be null, so you may want to
check this also!
Tobin
这篇关于.text =="" vs .length == 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!