C# の string オブジェクトの参照値
C#の文字列型(object)の参照についていくつか試してみた結果
記事にあったもの
実データはコピーされず、参照先が同じになります
コンパイラの最適化によるものだろうか、定数はまとめられるらしい
記事にあったもの
static void Main(string[] args)
{
string a = "hello";
string b = "h";
b += "ello";
Console.WriteLine(a == b); // True
Console.WriteLine(object.ReferenceEquals(a, b)); // Flase
}
実データはコピーされず、参照先が同じになります
static void Main(string[] args)
{
string a = "hello";
string b = a;
Console.WriteLine(a == b); // True
Console.WriteLine(object.ReferenceEquals(a, b)); // True
}
コンパイラの最適化によるものだろうか、定数はまとめられるらしい
static void Main(string[] args)
{
string a = "hello";
string b = "he" + "llo";
Console.WriteLine(a == b); // True
Console.WriteLine(object.ReferenceEquals(a, b)); // True
}
static void Main(string[] args)
{
string a = "hello";
string b = Hello();
Console.WriteLine(a == b); // True
Console.WriteLine(object.ReferenceEquals(a, b)); // True
}
static string Hello()
{
string s = "hello";
return s;
}
コメント