About

地図会社で働いていたプログラマ。

2013年2月15日金曜日

.NET 空文字列とSystem.Emptyの違い


結論から言うと同じ。ただし、.NET Framework 2.0の頃は違った。
2.0以降を使う場合は、コーディング規約や可読性を優先するべし。

What is the difference between String.Empty and “” (empty string)?
http://stackoverflow.com/questions/151472/what-is-the-difference-between-string-empty-and-empty-string?lq=1

In .Net pre 2.0, "" creates an object while String.Empty creates no object. So it is more efficient to use String.Empty.
.NET 2.0以前では空文字列はオブジェクトを生成していた。一方、String.Emptyは作成しない。そのためString.Emptyの方が効率が良かった。

"" would only have created an object once anyway due to string interning. Basically the performance tradeoff is peanuts - readability is more important.
空文字列はどのみち一回だけしかオブジェクトを生成しないため、基本的にはパフォーマンストレードオフは大したことない。可読性を優先するべき。

0 Kommentarer:

コメントを投稿