How to unset variable in C#?

There is not really an equivalent to "unset".

The closest match I know is the use of the default keyword.

For example:

MyType myvar = default(MyType);
string a = default(string);

The variable will still be "set", but it will have its default value.


You can't. There's no notion of "unsetting" a variable. You can set it to a different value - 0, null, whatever's appropriate. Instance/static variables don't even have a concept of whether the variable is set/unset, and local variables only have "definitely assigned" or "not definitely assigned".

What is it you're trying to achieve?

Tags:

C#