----------------------------------------------------------- New Message on BDOTNET
----------------------------------------------------------- From: VBGURU Message 1 in Discussion source: http://www.aravinda.in/Tech.aspx?Title=The_Advantage_of_Using_Using&WebPage=tech\dotnet\TheAdvantageofUsingUsing.htm @ www.aravinda.in Usage 1 using Directive The using directive has two uses: Create an alias for a namespace (a using alias). Permit the use of types in a namespace, such that, you do not have to qualify the use of a type in that namespace (a using directive). Create a using alias to make it easier to qualify an identifier to a namespace or class. Create a using directive to use the types in a namespace without having to specify the namespace. A using directive does not give you access to any namespaces that may be nested in the namespace you specify. Namespaces come in two categories: user-defined and system-defined. User-defined namespaces are namespaces defined in your code. See the documentation for the .NET Framework for a list of the system-defined namespaces The following sample shows how to define a using directive and a using alias for a class: // cs_using_directive2.cs using System; // using directive using AliasToMyClass = NameSpace1.MyClass; // using alias for a class namespace NameSpace1 { public class MyClass { public override string ToString() { return "You are in NameSpace1.MyClass"; } } } namespace NameSpace2 { class MyClass { } } namespace NameSpace3 { using NameSpace1; // using directive using NameSpace2; // using directive class Test { public static void Main() { AliasToMyClass somevar = new AliasToMyClass(); Console.WriteLine(somevar); } } } Output You are in NameSpace1.MyClass Usage 2 The other most important usage of using Using is using Statement (C# Reference) Defines a scope, outside of which an object or objects will be disposed. Syntax using (Font font1 = new Font("Arial", 10.0f)) { } C#, through the .NET Framework common language runtime (CLR), automatically releases the memory used to store objects that are no longer required. The release of memory is non-deterministic; memory is released whenever the CLR decides to perform garbage collection. However, it is usually best to release limited resources such as file handles and network connections as quickly as possible. The using statement allows the programmer to specify when objects that use resources should release them. The object provided to the using statement must implement the IDisposable interface. This interface provides the Dispose method, which should release the object's resources. A using statement can be exited either when the end of the using statement is reached or if an exception is thrown and control leaves the statement block before the end of the statement. The object can be declared in the using statement, as shown above, or before the using statement, like this: The following sample shows how a user-defined class can implement its own Dispose behavior. Note that your type must inherit from IDisposable. using System; class C : IDisposable { public void UseLimitedResource() { Console.WriteLine("Using limited resource..."); } void IDisposable.Dispose() { Console.WriteLine("Disposing limited resource."); } } class Program { static void Main() { using (C c = new C()) { c.UseLimitedResource(); } Console.WriteLine("Now outside using statement."); Console.ReadLine(); } } source: http://www.aravinda.in/Tech.aspx?Title=The_Advantage_of_Using_Using&WebPage=tech\dotnet\TheAdvantageofUsingUsing.htm @ www.aravinda.in ----------------------------------------------------------- To stop getting this e-mail, or change how often it arrives, go to your E-mail Settings. http://groups.msn.com/BDotNet/_emailsettings.msnw Need help? If you've forgotten your password, please go to Passport Member Services. http://groups.msn.com/_passportredir.msnw?ppmprop=help For other questions or feedback, go to our Contact Us page. http://groups.msn.com/contact If you do not want to receive future e-mail from this MSN group, or if you received this message by mistake, please click the "Remove" link below. On the pre-addressed e-mail message that opens, simply click "Send". Your e-mail address will be deleted from this group's mailing list. mailto:[EMAIL PROTECTED]