的String.Empty [英] string.Empty

查看:69
本文介绍了的String.Empty的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被告知*总是*总是声明字符串变量

,默认值为string.Empty - 对于初始值为
$ b的情况$ b不知道......像这样:


string myString = string.Empty; //这样做

string myString; //不要这样做


问题

1.这是一个好规则吗?

2.如果是这样,为什么?如果不是,为什么不呢?


谢谢!

解决方案

Smithers写道:


我被告知*总是*总是*声明字符串变量

,默认值为string.Empty - 对于初始值的情况

未知......就像这样:


string myString = string.Empty; //这样做

string myString; //不要这样做


问题

1.这是一个好规则吗?



No.


2.如果是,为什么?如果没有,为什么不呢?



一般来说,如果规则说总是它涉及限制你超出语言允许的范围,这不是一个好的规则。


在这种特殊情况下,我看不到原因是什么初始化字符串

变量与null在各种情况下是适当的。


使用string.Empty(或等效的)初始化是可取的当你想要总是能够假设字符串初始化为

实例时,空字符串是一个有用的值。但是要说做b / b $ b $做正确的事情是错的。


Pete


< blockquote>

" Smithers" < A@B.com写了留言

新闻:Ow ************** @ TK2MSFTNGP04.phx.gbl ...


>我被告知*总是*声明字符串
变量,默认值为string.Empty - 对于初始的情况
价值是未知的...像这样:



不知道为什么要这样做。


string myString = string.Empty; //这样做

string myString; //不要这样做



你知道那两行不一样吗? String是一个引用

类型,这意味着在第二行它将被取消分配(引用

null因缺乏更好的说法)。

第一行将其分配给实例。现在,如果你有一个类型,你想要使用
作为默认的String.Empty,那么它是有意义的。


但这远非*总是*


Smithers写道:


我被告知*总是*声明是个好主意字符串变量

,默认值为string.Empty - 对于初始值为

未知的情况......如下:


string myString = string.Empty; //这样做

string myString; //不要这样做


问题

1.这是一个好规则吗?



没有规则说你应该*总是*做一些事情,无论

情况是多么好的规则。所有规则都有例外。


2.如果是,为什么?如果没有,为什么不呢?



我已经看过,虽然不常见,但是如果字段未初始化或者设置为未知,那么知道

的情况很重要价值。

遵循上述规则,这种区别将不再可能。

-

Tom Porterfield


I have been told that it is a good idea to *always* declare string variables
with a default value of string.Empty - for cases where an initial value is
not known... like this:

string myString = string.Empty; // do this
string myString; // do not do this

Questions
1. Is that a good rule?
2. If so, why? If not, why not?

Thanks!

解决方案

Smithers wrote:

I have been told that it is a good idea to *always* declare string variables
with a default value of string.Empty - for cases where an initial value is
not known... like this:

string myString = string.Empty; // do this
string myString; // do not do this

Questions
1. Is that a good rule?

No.

2. If so, why? If not, why not?

Generally speaking, if a rule says "always" and it involves restricting
you above and beyond what the language allows, it''s not a good rule.

In this particular case, I see no reason at all that initializing string
variables with null is in appropriate in a variety of situations.

Initializing with string.Empty (or the equivalent) is desirable when you
want to always be able to assume the string is initialized to an
instance, and an empty string is a useful value. But to say that doing
do is _always_ the right thing to do is wrong.

Pete


Hi,
"Smithers" <A@B.comwrote in message
news:Ow**************@TK2MSFTNGP04.phx.gbl...

>I have been told that it is a good idea to *always* declare string
variables with a default value of string.Empty - for cases where an initial
value is not known... like this:

Not idea why you want to do that.

string myString = string.Empty; // do this
string myString; // do not do this


You know that those two lines are not the same right? String is a reference
type, that means that in the second line it will be unassigned (referencing
null for lack of a better way of saying).
The first line assign it to an instance. Now if you have a type where you
want to use as a default String.Empty then it makes sense.

But that is far from being *always*


Smithers wrote:

I have been told that it is a good idea to *always* declare string variables
with a default value of string.Empty - for cases where an initial value is
not known... like this:

string myString = string.Empty; // do this
string myString; // do not do this

Questions
1. Is that a good rule?

No rule that says you should *always* do something no matter what the
situation is ever a good rule. All rules have exceptions.

2. If so, why? If not, why not?

I have seen, though not often, scenarios where it was important to know
if the field was uninitialized or if it was set to a value of "".
Following the above rule, that distinction would no longer be possible.
--
Tom Porterfield


这篇关于的String.Empty的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆