Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why we are using "Using" keyword in a class or function while "Using" keyword are using before declare a class.

like this

C#
using System;
using System.IO;
using System.Text;

class MemStream
{
    static void Main()
    {
        int count;
        byte[] byteArray;
        char[] charArray;
        UnicodeEncoding uniEncoding = new UnicodeEncoding();

        // Create the data to write to the stream.
        byte[] firstString = uniEncoding.GetBytes(
            "Invalid file path characters are: ");
        byte[] secondString = uniEncoding.GetBytes(
            Path.GetInvalidPathChars());

        using(MemoryStream memStream = new MemoryStream(100))
        {
            // Write the first string to the stream.
            memStream.Write(firstString, 0 , firstString.Length);

            // Write the second string to the stream, byte by byte.
            count = 0;
            while(count < secondString.Length)
            {
                memStream.WriteByte(secondString[count++]);
            }

            // Write the stream properties to the console.
            Console.WriteLine(
                "Capacity = {0}, Length = {1}, Position = {2}\n",
                memStream.Capacity.ToString(),
                memStream.Length.ToString(),
                memStream.Position.ToString());

            // Set the position to the beginning of the stream.
            memStream.Seek(0, SeekOrigin.Begin);

            // Read the first 20 bytes from the stream.
            byteArray = new byte[memStream.Length];
            count = memStream.Read(byteArray, 0, 20);

            // Read the remaining bytes, byte by byte.
            while(count < memStream.Length)
            {
                byteArray[count++] =
                    Convert.ToByte(memStream.ReadByte());
            }

            // Decode the byte array into a char array
            // and write it to the console.
            charArray = new char[uniEncoding.GetCharCount(
                byteArray, 0, count)];
            uniEncoding.GetDecoder().GetChars(
                byteArray, 0, count, charArray, 0);
            Console.WriteLine(charArray);
        }
    }
}







The web site referral
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx#Y3500[^]
Posted
Updated 7-Sep-11 19:18pm
v2

One "using" has nothing to do with another "using". They have nothing in common; this is just the economy of keywords.

First "using" provide a name alias and carry no functionality whatsoever. You can work without a single "using" clause at all, but then you will have to have all names fully qualified.

The second case is using statement. It is a syntactic sugar for automatic call of System.IDisposable.Dispose. Practically, you should always use it whenever you need to create an object of the class implementing this interface. It calls System.IDisposable.Dispose automatically on the exit of the block even if an exception is thrown. This functionality is strictly equivalent to the instantiation and disposal using try-finally block.

See http://msdn.microsoft.com/en-us/library/yh598w02.aspx[^]. By the way, read the language and platform manual, and do it with close attention to detail. If you did it, you would save a lot of time; and this question would not be needed.

—SA
 
Share this answer
 
v3
Comments
Suresh Suthar 8-Sep-11 1:24am    
Well explained. 5!.
Sergey Alexandrovich Kryukov 8-Sep-11 1:34am    
Thank you, Suresh.
--SA
In C# the using keyword has two major uses:

using Directive: Creates an alias for a namespace or imports types defined in other namespaces. e.g. in your code sample above
C#
using System;
using System.IO;
using System.Text;



using Statement: Defines a scope at the end of which an object will be disposed. i.e.

C#
using(MemoryStream memStream = new MemoryStream(100))


For more details on using statement click[^] here
and for using directive click[^] here.

A good article on using Statement[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 8-Sep-11 1:35am    
Same time? All correct, my 5.
--SA
Suresh Suthar 8-Sep-11 1:38am    
Thanks SA.
When we use Using keyword ,it will call internally Dispose method of that class after executing the using block.

The using Keyword and IDisposable[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900