object.ReferenceEquals表现不同

时间:2016-05-06 10:07:59

标签: c#

string x = "alok b";

string y = "alok b";

string z = "alok";

//y += x.Replace(y, string.Empty);
z += " b";

Console.WriteLine(object.ReferenceEquals(x,y));
Console.WriteLine(object.ReferenceEquals(y, z));

第一行如何打印true和第二行false

并更改为以下语句正在打印true

Console.WriteLine(object.ReferenceEquals(y,string.Intern(z)));

2 个答案:

答案 0 :(得分:1)

它被称为string interning

答案 1 :(得分:0)

创建字符串时,它会创建一个对象(@interface ViewController () { UIDocumentInteractionController * docController; } @end - (void)sharePic:(UIImage *)image { NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]; NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"Image.ig"]; NSData *imageData = UIImageJPEGRepresentation(image, 0.8); [imageData writeToFile:savedImagePath atomically:YES]; NSURL *imageUrl = [NSURL fileURLWithPath:savedImagePath]; docController = [UIDocumentInteractionController interactionControllerWithURL:imageUrl]; [docController retain]; docController.UTI = @"com.instagram.exclusivegram"; docController.delegate = self; docController.annotation = [NSDictionary dictionaryWithObject:@"Caption Test" forKey:@"InstagramCaption"]; [docController presentOpenInMenuFromRect:CGRectZero inView:self.view animated:YES]; docController = nil; } )。

当您创建x时,您只需再次指向它,它指向同一个(拥有它)。

创建Z时,y完全创建一个新的,因此,它将与前一个内存中的地址不匹配。