0

Please forgive me I'm a self educated c# coder, I never learned coding at a school. And sometimes i miss the terminology or logic behind common practices. I am wondered about the "using" keyword in C#.

using can be placed in the header of a program like:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;

But I also see it sometimes "inline" like below. I say inline and that's probably a wrong term, but I don't know how this is called

using (System.Xml.XmlWriter writer = System.Xml.XmlWriter.Create(saveSettingsDialog.FileName)) 
{ 
//code to be placed to do something (in this case fill an xml)
}

Thinking about code design, I wonder when is it preferred to use the "inline" syntax. And does it provide some extra benefit ?

user613326
  • 2,140
  • 9
  • 34
  • 63
  • There are actually 3 ways now with the new static using in C#6, but lets focus on the two you're asking about. – Glenn Ferrie Oct 29 '15 at 14:01
  • 5
    The first is the [using directive](https://msdn.microsoft.com/en-us/library/sf0df423.aspx) the other is the [using statement](https://msdn.microsoft.com/en-us/library/yh598w02.aspx). – juharr Oct 29 '15 at 14:01
  • `this` can also mean multiple things, depending on context. – CodeCaster Oct 29 '15 at 14:03
  • When should i use "using" as directive or as statement – user613326 Oct 29 '15 at 15:22
  • You should do what you want. The two as totally different and only share the same name by chance. – TaW Oct 29 '15 at 22:33

0 Answers0