What is the use of fixed statement

A) To stop the garbagecollector to move the objects in use
B) To dispose the object at the end of the defined scope
C) To tell the garbage collector to move the object is use
D) To fix the size of an object.

Showing Answers 1 - 1 of 1 Answers

naveentej

  • Jan 7th, 2006
 

Ans: A

The fixed statement sets a pointer to a managed variable and "pins" that variable during the execution of statement.

Without fixed, pointers to managed variables would be of little use since garbage collection could relocate the variables unpredictably. (In fact, the C# compiler will not allow you to set a pointer to a managed variable except in a fixed statement.)

Eg: Class A { public int i; }

A objA = new A;  // A is a .net managed type

fixed(int *pt = &objA.i) // use fixed while using pointers with managed 

                               // variables 

{

   *pt=45;              // in this block use the pointer the way u want

}

    

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions