Undeclare variables in c #

Is it possible to decompress variables in C #? If so, how?

+5
source share
6 answers

You don't care about non-declaration in C # (I think you mean non-allocation by the way, right?) Or any other .Net languages, the garbage collector takes care of non-allocation of memory associated with a variable.

For unmanaged resources (fonts, database connections, files, etc.) you need to call the Dispose method, either explicitly or by placing this variable in the use block.

Additional information about the .Net garbage collector: http://www.csharphelp.com/2006/08/garbage-collection/

+4
source

( '{'), :

int c=0;
{
 int a=1;
 {
  int b=2; 
  c=a+b;
 } // this "undeclares" b
 c=c+a;
} // this "undeclares" a
+10

- , .

; ? , .

+1

IDisposable, using. , Luther.

using (Car myCar = new Car())
{
    myCar.Run();
}
+1

, , undeclaring i.Net. , , IDisposable. .

0
source

I do not think this is possible in modern languages. A possible scenario is as follows:

void doSomething(String objectId) {
  BusinessObject obj = findBusinessObject(objectId);

  // from this point on the objectId should not be used anymore
  undef objectId;

  // continue using obj ...
}

Another scenario is when you implement a method from the interface, and want you to not use one of the parameters, especially in long methods.

Object get(int index, Object defaultValue) {
  undef index, defaultValue;

  return "constant default value";
}

This will also serve as documentation that the programmer thought about these unused parameters.

0
source

All Articles