What are the uses of “using” in C#?

后端 未结 29 2977
有刺的猬
有刺的猬 2020-11-21 07:31

User kokos answered the wonderful Hidden Features of C# question by mentioning the using keyword. Can you elaborate on that? What are the uses of

29条回答
  •  眼角桃花
    2020-11-21 07:55

    The using keyword defines the scope for the object and then disposes of the object when the scope is complete. For example.

    using (Font font2 = new Font("Arial", 10.0f))
    {
        // use font2
    }
    

    See here for the MSDN article on the C# using keyword.

提交回复
热议问题