Skip navigation

Properties Vs Constructor Parameters- A Closer Analysis

We all know that we can set the properties of the ADO.NET objects by 2 ways.. By passing parameters to the constructors. Consider the following code snippet that creates a newSqlConnection object   and sets the connection string by directly specifying the value of the property.

 

Dim objSqlCon as new System.Data.SqlClient.SqlConnection()

objSqlCon.ConnectionString = "Data source=localhost;Database=Northwind;User ID=sa;Pwd="

 

Above task can be performed by passing the connection string as a parameter to the constructor, see the code below..

 

Dim objSqlCon as new System.Data.SqlClient.SqlConnection()

objSqlCon.ConnectionString = "Data source=localhost;Database=Northwind;User ID=sa;Pwd="
 

Practically I feel there is no significant difference in the performance of both the approaches and the choice depends on the perception of every individual.

 

By setting the properties directly by specifying values makes code readable and debugging simple when compared with passing parameters to the constructors.

 

In some cases passing parameters to a constructor may provide an advantage to our application. In the case of DataView object, setting the values of the RowFilter and RowStateFilter properties directly results in creating an Index at least twice while creating the object and when the properties are set.

 

In this scenario pass the values for the properties, as parameters to the constructors is the profitable approach.

 

Happy Learning !!! 

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish