0% found this document useful (0 votes)
111 views3 pages

.NET Finalize vs Dispose Methods

The document compares the Finalize() and Dispose() methods in .NET. Finalize() is called automatically by the garbage collector to reclaim memory, while Dispose() must be called manually to instantly dispose of objects and is deterministic. The document also compares DataTable, which stores a single table of rows and columns, to DataSet, which stores multiple related DataTables and enforces data integrity through relations.

Uploaded by

88nikp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
111 views3 pages

.NET Finalize vs Dispose Methods

The document compares the Finalize() and Dispose() methods in .NET. Finalize() is called automatically by the garbage collector to reclaim memory, while Dispose() must be called manually to instantly dispose of objects and is deterministic. The document also compares DataTable, which stores a single table of rows and columns, to DataSet, which stores multiple related DataTables and enforces data integrity through relations.

Uploaded by

88nikp
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

Difference between Finalize() and Dispose() methods in .

NET
Finalize:
1. Finalize() belongs to the Object class.
2. It is automatically called by the Garbage Collection mechanism when the object goes
out of the scope(usually at the end of the program
3. It is slower method and not suitable for instant disposing of the objects.
4. It is non-deterministic function i.e., it is uncertain when Garbage Collector will call
Finalize() method to reclaim memory.
Example: class employee
{
~employee() //This is the destructor of emp class
{
}
//This destructor is implicitly compiled to the Finalize method.
}
Dispose:
1. Dispose() belongs to the IDisposable interface
2. We have to manually write the code to implement it(User Code)
ex: if we have emp class we have to inherit it from the IDisposable interface
and write code. We may have to suppress the Finalize method using
GC.SuppressFinalize() method.
3. Faster method for instant disposal of the objects.
4. It is deterministic function as Dispose() method is explicitly called by the User Code.
Example: user interface Controls. Forms, SqlConnection class have built in
implementaion of Dispose method.
try
{
string constring = "Server=(local);Database=my; User Id=sa; Password=sa";
SqlConnection sqlcon = new SqlConnection(constring);
sqlcon.Open(); // here connection is open
// some code here which will be execute
}
catch
{
// code will be execute when error occurred in try block
}
finally
{
sqlcon.Close(); // close the connection
sqlcon.Dispose(); // detsroy the connection object
}

Difference between DataTable and DataSet

DataTable:
This class stores rows and columns of data.
It is part of the System.Data namespace.
We add, select and iterate over stored data.
1. Meaning: A DataTable is an in-memory representation of a single database table which
has collection of rows and columns
2. Number of rows retrieved at a time: DataTable fetches only one TableRow at a time
3. Provision of DataRelation Objects: As DataTable is a single database table, so there is
no DataRelation object in it.
4. Enforcing Data Integrity: In DataTable, there is no UniqueConstraint and
ForeignKeyConstraint objects available.
5. DataSource can be Serialized or Not: In DataTable, DataSource cannot be serialized.
6. To know the example for DataTable, please try http://www.dotnetperls.com/datatable
DataSet:
DataSet is a collection of DataTables.
We use the DataSet type to store many DataTables in a single collection.
Conceptually, the DataSet acts as a set of DataTable instances.
1. Meaning: A DataSet is an in-memory representation of a database-like structure which
has collection of DataTables.
2. Number of rows retrieved at a time: DataSet can fetch multiple TableRows at a time
3. Provision of DataRelation Objects: In DataSet, DataTable objects can be related to
each other with DataRelation objects.
4. Enforcing Data Integrity: In DataSet, data integrity is enforced by using the
UniqueConstraint and ForeignKeyConstraint objects.
5. DataSource can be Serialized or Not: DataSet is serialized DataSource .That is why
web services can always returns DataSet as the result but not the DataTables.
6. To know the example for DataSet, please try http://www.dotnetperls.com/dataset
// Create two DataTable instances.
DataTable table1 = new DataTable("patients");
table1.Columns.Add("name");
table1.Columns.Add("id");
table1.Rows.Add("sam", 1);
table1.Rows.Add("mark", 2);

DataTable table2 = new DataTable("medications");


table2.Columns.Add("id");
table2.Columns.Add("medication");
table2.Rows.Add(1, "atenolol");
table2.Rows.Add(2, "amoxicillin");
// Create a DataSet and put both tables in it.
DataSet set = new DataSet("office");
set.Tables.Add(table1);
set.Tables.Add(table2);

You might also like