All the c# programmers daily coding life start's with "using" why i am saying like this,the first word of the every c# code file is "using". so we have to know what is this first word ? How to use it?.That's what i am trying to explain in this post here.
In c#,we can use the using keyword as two possible ways are as a Directive and as a Statement.
1) using as a directive:
1) As all the programmers know using is used to importing the namespaces.we can use types available in that namespaces by declaring the namespace with "using"
Example:- using System.Web;
For example here i am importing the System.Web namespace,now i can use all the types like HtmlString, HttpApplication, HttpApplicationState etc. of Web namespace in further coding of current file.
2) Also use to define the alias for the nested namspaces.
Example:- using MyControls=MyComponent.Web.UI.Controls.;
In the above example, we can use Mycontrols instead of using MyComponent.Web.UI.Controls to use Controls class like.
MyComponent.Web.UI.Controls objControl=new MyComponent.Web.UI.Controls();
MyControls objControl=new MyControls();
2) using as a statement:
When we use "using" as Statement it defines the scope and allows the programmer to release the resources used by the object in the defined scope.
with out using keyword:
A simple and straightforward approach to connect to a database server and read data
SqlConnection con = new SqlConnection(conString);
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(cmdString,con);
con .Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
//Do
}
reader.Close();
con .Close();
However, the above given code may generate error.
If any exception occurs inside while block it throws exception the connection.close() process will not executed due to the exception.To avoid this situation, we can take the help of the try....catch... finally block by closing the connection inside the finally block or inside the catch block.
with using keyword:
"Using" keyword takes the parameter of type IDisposable.Whenever you are using any IDisposable type object you should use the "using" keyword to handle automatically when it should close or dispose.Internally using keyword calls the Dispose() method to dispose the IDisposable object.
So the code will be modified like :
using(SqlConnection con= new SqlConnection(conString) )
{
SqlCommand cmd = new SqlCommand(cmdString, con);
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// statements
}
}
}
In the above code if any exception occurs then dispose() will be called and connection will closed automatically.
Multiple objects can be used in with a using statement, but they must be declared inside the using statement.
In the above example also we used multiple objects are cmd,reader.so we removed the both objects closed methods.
Example:-
using (Type t1 = v1, t2 = v2, …, tN = vN)
{
//statements
}
or
using (Type r1 = v1)
{
using (Type t2 = v2)
{
…......
using (Type tN = vN)
{
// statement
}
}
}
Above both code examples are equal.
In c#,we can use the using keyword as two possible ways are as a Directive and as a Statement.
1) using as a directive:
1) As all the programmers know using is used to importing the namespaces.we can use types available in that namespaces by declaring the namespace with "using"
Example:- using System.Web;
For example here i am importing the System.Web namespace,now i can use all the types like HtmlString, HttpApplication, HttpApplicationState etc. of Web namespace in further coding of current file.
2) Also use to define the alias for the nested namspaces.
Example:- using MyControls=MyComponent.Web.UI.Controls.;
In the above example, we can use Mycontrols instead of using MyComponent.Web.UI.Controls to use Controls class like.
MyComponent.Web.UI.Controls objControl=new MyComponent.Web.UI.Controls();
MyControls objControl=new MyControls();
2) using as a statement:
When we use "using" as Statement it defines the scope and allows the programmer to release the resources used by the object in the defined scope.
with out using keyword:
A simple and straightforward approach to connect to a database server and read data
SqlConnection con = new SqlConnection(conString);
SqlDataReader reader = null;
SqlCommand cmd = new SqlCommand(cmdString,con);
con .Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
//Do
}
reader.Close();
con .Close();
However, the above given code may generate error.
If any exception occurs inside while block it throws exception the connection.close() process will not executed due to the exception.To avoid this situation, we can take the help of the try....catch... finally block by closing the connection inside the finally block or inside the catch block.
with using keyword:
"Using" keyword takes the parameter of type IDisposable.Whenever you are using any IDisposable type object you should use the "using" keyword to handle automatically when it should close or dispose.Internally using keyword calls the Dispose() method to dispose the IDisposable object.
So the code will be modified like :
using(SqlConnection con= new SqlConnection(conString) )
{
SqlCommand cmd = new SqlCommand(cmdString, con);
con.Open();
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
// statements
}
}
}
In the above code if any exception occurs then dispose() will be called and connection will closed automatically.
Multiple objects can be used in with a using statement, but they must be declared inside the using statement.
In the above example also we used multiple objects are cmd,reader.so we removed the both objects closed methods.
Example:-
using (Type t1 = v1, t2 = v2, …, tN = vN)
{
//statements
}
or
using (Type r1 = v1)
{
using (Type t2 = v2)
{
…......
using (Type tN = vN)
{
// statement
}
}
}
Above both code examples are equal.
No comments:
Post a Comment