为什么我们在Main方法中或类中使用 [英] Why we are using in Main method or inside a class

查看:78
本文介绍了为什么我们在Main方法中或类中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在声明类之前使用正在使用"关键字时,为什么在类或函数中使用正在使用"关键字.

像这样

Why we are using "Using" keyword in a class or function while "Using" keyword are using before declare a class.

like this

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);
        }
    }
}








网站推荐
http://msdn.microsoft.com/en-us/library/system. io.memorystream.aspx#Y3500 [ ^ ]








The web site referral
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx#Y3500[^]

推荐答案

一个使用中"与另一个使用中"无关.没有共同点这只是关键字的经济性.

首先使用"提供名称别名,并且不包含任何功能.您可以完全不用单个"using"子句来工作,但是随后您将必须使所有名称完全限定.

第二种情况是using语句.它是自动调用System.IDisposable.Dispose的语法糖.实际上,无论何时需要创建实现此接口的类的对象,都应始终使用它.即使抛出异常,它也会在块的出口自动调用System.IDisposable.Dispose.此功能严格等效于使用try-finally块进行实例化和处理.

请参阅 http://msdn.microsoft.com/en-us/library/yh598w02.aspx [ ^ ].顺便说一句,请阅读语言和平台手册,并特别注意细节.如果这样做,则可以节省大量时间;并且不需要这个问题.

—SA
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


在C#中,using关键字有两个主要用途:

使用指令:为名称空间创建别名或导入在其他名称空间中定义的类型.例如在上面的代码示例中
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
using System;
using System.IO;
using System.Text;




using语句:定义一个范围,在该范围的末尾将放置一个对象.即




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

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[^]


当我们使用Using关键字时,它将在执行后内部调用该类的Dispose方法using块.

使用关键字和IDisposable [
When we use Using keyword ,it will call internally Dispose method of that class after executing the using block.

The using Keyword and IDisposable[^]


这篇关于为什么我们在Main方法中或类中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆