值类型的变量。由于值变量创建在栈中,当栈回位时,变量就被马上抛弃了。 而引用类型的变量,创建在托管堆中,只有到垃圾回收时,才有可能被抛弃。但是否删除(比如覆盖为零),则取决于CLR。如果你要删除敏感信息,可以考虑参考System.Security中的SecureString。 void MyMethod() { int i = 123; // 123 - 栈 string s = "who"; // "who" - 托管堆} // 123 is gone but "who" could survive till garbage collection
而引用类型的变量,创建在托管堆中,只有到垃圾回收时,才有可能被抛弃。但是否删除(比如覆盖为零),则取决于CLR。如果你要删除敏感信息,可以考虑参考System.Security中的SecureString。
void MyMethod()
{
int i = 123; // 123 - 栈
string s = "who"; // "who" - 托管堆} // 123 is gone but "who" could survive till garbage collection