Immutable Dot Net Strings

I usually define my string variables in vb.net as

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

Given the unchanged nature of strings in .net, is there a better way to initialize strings and the Variable processing f_sName is used before it has been assigned a value. A null reference exception might occur at runtime. "Warning?

Also for classes that don't have constructors that take no arguments, such as System.Net.Sockets.NetworkStream, what is the best way to define and initialize a variable of this type?

+3
source share
3 answers

This has nothing to do with immutable strings.

A

 Dim f_sName as string = "Foo"

, .

Nothing Empty. , :

  • . "" xxx " , " - , .
  • . .
  • ( "", ).

, NetworkStream, : , . Disposable, NetworkStream, Using:

 Using fs As New NetworkStream(....)

 End Using

"fs", .

+3

String.Empty - . , string.empty . null , String.Empty .

, .

+1
Dim f_sName as String = Nothing

Dim f_sName As New String()

: mixedCase underscores ( f_sName) , ?

+1

Source: https://habr.com/ru/post/1747613/


All Articles