不可变的点网字符串

发布于 2024-09-03 08:33:35 字数 288 浏览 9 评论 0原文

我通常将 vb.net 中的字符串变量定义为

Dim f_sName as string=String.Empty
f_sName = "foo"

鉴于 .net 中字符串的不可变性质,是否有更好的方法来初始化字符串并处理“变量 'f_sName ' 在被赋值之前使用。一个 null运行时可能会导致引用异常。”警告?

另外,对于没有不接受任何参数的构造函数的类(例如 System.Net.Sockets.NetworkStream),定义和初始化该类型变量的最佳方法是什么?

I usually define my string variables in vb.net as

Dim f_sName as string=String.Empty
f_sName = "foo"

Given the immutable nature of strings in .net, is there a better way to initialize strings and deal with the "Variable 'f_sName ' is used before it has been assigned a value. A null reference exception could result at runtime." warning?

Also for classes that do not have constructors which accept no arguments such as System.Net.Sockets.NetworkStream, what is the best way to define and initialize a variable of that type?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

高跟鞋的旋律 2024-09-10 08:33:35

这与字符串不可变无关。

一个更好的优选模式是

 Dim f_sName as string = "Foo"

但这取决于具体情况是否可以使用。

与 Nothing 或 Empty 初始化相同。如果您想尽早捕获错误,那么:

  1. 只需根本不初始化即可。 “变量‘xxx’在被赋值之前就被使用”是你的朋友,而不是你的敌人。
  2. 初始化为Nothing。它也可能会暴露错误。
  3. 用 Empty 初始化(或只是“”,没有区别)。

对于像 NetworkStream 这样的类,同样的事情:不要创建实例,也不要声明变量,直到需要它们为止。对于像 NetworkStream 这样的 Disposable 类,这意味着使用 using 子句:

 Using fs As New NetworkStream(....)

 End Using

在这个狭窄的范围之外提供“fs”既不理想也没有用。

This has nothing to do with the strings being immutable.

A better preferable pattern would be

 Dim f_sName as string = "Foo"

But it depends on the situation if you can use that.

Same for initializing with Nothing or Empty. If you want to catch errors early, then:

  1. just don't initialize at all. "Variable 'xxx' is used before it has been assigned a value" is your friend, not your enemy.
  2. Initialize with Nothing. It too might bring errors to light.
  3. Initialize with Empty (or just "", no difference).

And concerning classes like NetworkStream, same thing: don't create instance and don't declare variables until you need them. For a Disposable class like NetworkStream this means use a Using clause:

 Using fs As New NetworkStream(....)

 End Using

It is neither desirable nor useful to have 'fs' available outside that narrow scope.

痴者 2024-09-10 08:33:35
Dim f_sName as String = Nothing

或者

Dim f_sName As New String()

顺便说一句:将 mixedCase 变量命名与 下划线 混合使用(如 f_sName 中)大多数时候看起来很奇怪,这个变量名代表什么?

Dim f_sName as String = Nothing

or

Dim f_sName As New String()

By the way: mixing mixedCase variable naming with underscores (as in f_sName) looks strange most of the time, what does this variable name stand for?

十年九夏 2024-09-10 08:33:35

将字符串初始化为 null 和 String.Empty 几乎是同一件事。就我个人而言,我认为将其设置为 string.empty 确实是处理它的最佳方法。将其设置为 null 将导致抛出 null 引用异常,但将其设置为 String.Empty 则不会。

人们可以争论两种方式之一初始化它的优点,但优化方面没有明显的区别。

Initializing a string to null and to String.Empty are pretty much the same thing. Personally, I think setting it to string.empty really is the best way of handling it. Setting it to null will result in throwing null reference exceptions though where as setting it to String.Empty will not.

One can argue the merits of initializing it one of two ways, but optimization wise there is no discernible difference.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文