What is the difference
between the destructor and the Finalize() method? When does the Finalize()
method get called?
Finalize() corresponds to the .Net Framework and is part of the System.Object class. Destructors are C#'s implementation of the Finalize() method. The functionality of both Finalize() and the destructor is the same, i.e., they contain code for freeing the resources when the object is about to be garbage collected. In C#, destructors are converted to the Finalize() method when the program is compiled. The Finalize() method is called by the .Net Runtime and we can not predict when it will be called. It is guaranteed to be called when there is no reference pointing to the object and the object is about to be garbage collected.
Finalize() corresponds to the .Net Framework and is part of the System.Object class. Destructors are C#'s implementation of the Finalize() method. The functionality of both Finalize() and the destructor is the same, i.e., they contain code for freeing the resources when the object is about to be garbage collected. In C#, destructors are converted to the Finalize() method when the program is compiled. The Finalize() method is called by the .Net Runtime and we can not predict when it will be called. It is guaranteed to be called when there is no reference pointing to the object and the object is about to be garbage collected.
Garbage Collection
·
<!--[if
!supportLists]--> Garbage collection is the mechanism that reclaims the
memory resources of an object when it is no longer referenced by a
variable.<!--[endif]-->
·
<!--[if
!supportLists]--> .Net Runtime performs automatically performs garbage
collection, however you can force the garbage collection to run at a certain
point in your code by calling System.GC.Collect().<!--[endif]-->
·
<!--[if
!supportLists]--> Advantage of Garbage collection : It prevents programming
error that could otherwise occur by incorrectly deleting or failing to delete
objects.<!--[endif]-->
Enumeration
Enumeration improves
code readability. It also helps in avoiding typing mistake.
Concept of Heap and Stack
Local Variables
|
Stack
|
Free Memory
(Larger Memory Area than Stack).
|
Heap
|
Global Variables
|
Permanent Storage area
|
Program Instruction
|
The Program
Instruction and Global and Static variables are stored in a region known as
permanent storage area and the local variables are stored in another area
called stack. The memory space located between these two regions is
available for dynamic memory allocation during execution of program. This free
memory region is called heap. The size of heap keeps on changing when
program is executed due to creation and death of variables that are local to
functions and blocks. Therefore, it is possible to encounter memory “overflow”
during dynamic allocation process.
<!--[if !supportLineBreakNewLine]--> <!--[endif]-->
Value Type and
Reference Type
A variable is value
type or reference type is solely determined by its data type.
Eg: int, float, char,
decimal, bool, decimal, struct, etc are value types, while object
type such as class, String, Array, etc are reference type.
Value Type
<!--[if !supportLists]--> As name suggest Value Type
stores “value” directly.<!--[endif]-->
<!--[if !supportLists]--> For eg: <!--[endif]-->
<!--[if !supportLists]--> //I and J are
both of type int<!--[endif]-->
<!--[if !supportLists]--> I =
20;<!--[endif]-->
<!--[if !supportLists]--> J =
I;<!--[endif]-->
<!--[if !supportLists]--> int is a value
type, which means that the above statements will results in two locations in
memory.<!--[endif]-->
<!--[if !supportLists]--> For each instance of value type
separate memory is allocated.<!--[endif]-->
<!--[if !supportLists]--> Stored in a
Stack.<!--[endif]-->
<!--[if !supportLists]--> It Provides Quick Access,
because of value located on stack.<!--[endif]-->
Reference Type
<!--[if !supportLists]--> As name suggest Reference Type
stores “reference” to the value.<!--[endif]-->
<!--[if !supportLists]--> For eg: <!--[endif]-->
<!--[if !supportLists]--> Vector X, Y;
//Object is defined. (No memory is allocated.)<!--[endif]-->
<!--[if !supportLists]--> X = new
Vector(); //Memory is allocated to Object. //(new is responsible for allocating
memory.)<!--[endif]-->
<!--[if !supportLists]--> X.value = 30;
//Initialising value field in a vector class.<!--[endif]-->
<!--[if !supportLists]--> Y = X; //Both
X and Y points to same memory location. //No memory is created for Y.<!--[endif]-->
<!--[if !supportLists]-->
Console.writeline(Y.value); //displays 30, as both points to same
memory<!--[endif]-->
<!--[if !supportLists]--> Y.value =
50;<!--[endif]-->
<!--[if !supportLists]-->
Console.writeline(X.value); //displays 50.<!--[endif]-->
<!--[if !supportLists]--> Note: If a
variable is reference it is possible to indicate that it does not refer to any
object by setting its value to null;<!--[endif]-->
<!--[if !supportLists]--> Reference type are stored on
Heap.<!--[endif]-->
<!--[if !supportLists]--> It provides comparatively slower
access, as value located on heap.<!--[endif]-->
ref keyword
Passing variables by
value is the default. However, we can force the value parameter to be passed by
reference. Note: variable “must” be initialized before it is passed into a
method.
out keyword
out keyword is used
for passing a variable for output purpose. It has same concept as ref keyword,
but passing a ref parameter needs variable to be initialized while out
parameter is passed without initialized.
It is useful when we
want to return more than one value from the method.
Note: You must
assigned value to out parameter in method body, otherwise the method won’t
compiled.
No comments:
Post a Comment