本文整理匯總了C#中System.Object.ReferenceEquals方法的典型用法代碼示例。如果您正苦於以下問題:C# Object.ReferenceEquals方法的具體用法?C# Object.ReferenceEquals怎麽用?C# Object.ReferenceEquals使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。
在下文中一共展示了Object.ReferenceEquals方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: Object
object o = null;
object p = null;
object q = new Object();
Console.WriteLine(Object.ReferenceEquals(o, p));
p = q;
Console.WriteLine(Object.ReferenceEquals(p, q));
Console.WriteLine(Object.ReferenceEquals(o, p));
輸出:
True True False
示例2:
int int1 = 3;
Console.WriteLine(Object.ReferenceEquals(int1, int1));
Console.WriteLine(int1.GetType().IsValueType);
輸出:
False True
示例3:
String s1 = "String1";
String s2 = "String1";
Console.WriteLine("s1 = s2: {0}", Object.ReferenceEquals(s1, s2));
Console.WriteLine("{0} interned: {1}", s1,
String.IsNullOrEmpty(String.IsInterned(s1)) ? "No" : "Yes");
String suffix = "A";
String s3 = "String" + suffix;
String s4 = "String" + suffix;
Console.WriteLine("s3 = s4: {0}", Object.ReferenceEquals(s3, s4));
Console.WriteLine("{0} interned: {1}", s3,
String.IsNullOrEmpty(String.IsInterned(s3)) ? "No" : "Yes");
輸出:
s1 = s2: True String1 interned: Yes s3 = s4: False StringA interned: No