在C#中,静态变量将被垃圾收集。 [英] In C# when static variables will be garbage collected.

查看:327
本文介绍了在C#中,静态变量将被垃圾收集。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在c中垃圾收集静态变量?


Is it possible to garbage collect static variable in c#

public static int staticVariable = 0;





我的尝试:



试图将静态变量设置为null。



What I have tried:

Tried to set static variable to null.

推荐答案

变量 - C#语言规范| Microsoft Docs [ ^ ]。


引用:

在C#中静态变量将是垃圾收集。

In C# when static variables will be garbage collected.



简答:你何时退出应用程序。

根据定义,静态变量在应用程序的生命周期内持续存在。


Short answer: when you will quit the app.
By definition, a static variable persist for the lifetime of the app.


public static int staticVariable = 0;




Quote:

试图将静态变量设置为null。

Tried to set static variable to null.



要添加其他人所说的内容,你不能设置任何整数到 null 因为所有整数都是值类型,不能包含 null 值。只能将引用类型和可空类型分配给 null。

所有C#原语类型都是值类型,但<$ c $除外c> string :


To add to what the others have said, you can't set any integer to null as all integers are Value Types which cannot contain null values. Only Reference Types and Nullable Types can be assigned to null.
All of the C# primitive types are Value types with the exception of string:

byte	
sbyte	
char	
decimal
double
float	
int	
uint	
long	
ulong	
object
short	
ushort


这篇关于在C#中,静态变量将被垃圾收集。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆