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

后端 未结 29 3512
有刺的猬
有刺的猬 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:58

    Everything outside the curly brackets is disposed, so it is great to dispose your objects if you are not using them. This is so because if you have a SqlDataAdapter object and you are using it only once in the application life cycle and you are filling just one dataset and you don't need it anymore, you can use the code:

    using(SqlDataAdapter adapter_object = new SqlDataAdapter(sql_command_parameter))
    {
       // do stuff
    } // here adapter_object is disposed automatically
    

提交回复
热议问题