We can use Using keyword in following ways:1. Generally we use Using keyword when we add any namespace in our class, code-behind files.. Like: Using System.IO;2. Secondly we can use Using keyword when an object need to destroy automatically. It can be used in ADO.NET, Entity Framework, Web Service call. Here system automatically closes/disposes the object. Like: Using(WCFService cc = new WCFService()) {cc.method(); }
1) using statement is used to call the Dispose method to destroy an object when it's usage is no more. Ex: using (TextWriter w = File.CreateText("log.txt")) { w.WriteLine("This is line one"); } 2) using keyword is used to call namespaces. Ex: using System.IO;
We are using "Using" Keyword in C# .Net for including pre-defined classes in .Net Framework.
using statement is used to import the namespaces, and to dispose the objects.
To allow the normal use of types in a namespace, using System.IO;To create an alias for a namespace or a type. This is called a using alias directive.using myp = System.Data;The using statement ensures that Dispose() is called even if an exception occurs when you are creating objects and calling methods, properties and so on. Dispose() is a method that is present in the IDisposable interface that helps to implement custom Garbage Collection. In other words, if I am doing some database operation (Insert, Update, Delete) but somehow an exception occurs, then here the using statement closes the connection automatically. here’s no need to call the connection Close() method explicitly.
using (SqlConnection conn = new SqlConnection(connString))
Using Keyword to used pre-defined classes
Using key is used for: 1. To include name spaces 2. To close/dispose object.
1. It used for including reference of other dll in your project. 2. It is used to dispose object from memory which is inherited from IDisposible interface.
1.For importing name spaces. 2.For providing the scope of object.
I am sure that till now you get to know what are the uses of 'Using'. Here is the link for what happen when we use 'using'. Hope the best.http://www.codeproject.com/Articles/17106/The-using-Keyword-in-C
"Using" key word suing for importing name spaces and dispose the connections like ADO.NET,WEB SERVICES,ENTITY FRAME WORK etc...
keywords are some special words that are used for special purpose. for example if if is a keyword and it has some special functionality in the program
1. Used to import namespace. 2. Used to dispose object.