Questions tagged [using]

"using" is a keyword in some programming languages (C++, C#, VB.NET, Haxe)

In the C# language, the using keyword is employed in two different contexts, as a Directive and a Statement.

The using directive is used to qualify namespaces and create namespace or type aliases.

using System.Text;
using Project = PC.MyCompany.Project;

The using statement provides a convenient syntax that ensures the correct use of IDisposable objects.

using (MyTypeImplementingIDisposable myInstance)) 
{
    // Do something with myInstance
}

Beginning with C# 8.0, you can use the alternative syntax that doesn't require braces

using var myInstance = new MyTypeImplementingIDisposable(...);
// Do something with myInstance

In the VB.NET language the Using keyword is used only as a Statement and provides the same features as the C# language:

Using sr As New StreamReader(filename)
    ' read the sr stream
End Using

In Haxe, the using keyword allows pseudo-extending existing types without modifying their source (syntactical sugar). It is achieved by declaring a static method with a first argument of the extending type and then bringing the defining class into context through using.

using StringTools;

// works because of the `using`:
var myEncodedString = "Haxe is great".replace("great", "awesome");
// Without the using one should type this: 
var myEncodedString = StringTools.replace("Haxe is great", "great", "awesome");

In gnuplot, the using qualifier allows specific columns in a datafile to be specified, for plotting and fitting.


In C++, the using keyword can be used in 3 ways;

  1. using declarations

    using std::swap;

  2. using directives

    using namespace std;

  3. type alias and alias template declaration (since C++11); similar to typedef

    template <class CharT> using mystring = std::basic_string<CharT,std::char_traits<CharT>>;

1795 questions
23
votes
4 answers

What does a leading :: mean in "using namespace ::X" in C++

can somebody explain me the difference between the following namespace usages: using namespace ::layer::module; and using namespace layer::module; What causes the additional :: before layer?
Dudero
  • 607
  • 7
  • 15
23
votes
3 answers

Why does C# define two different uses for `using`?

More a question out of curiosity than anything, but why does C# define two different "purposes" for the keyword using? On one hand, it's a directive... used to create an alias for a namespace or to import types defined in other…
David Hoerster
  • 28,421
  • 8
  • 67
  • 102
23
votes
5 answers

In LINQ-SQL, wrap the DataContext is an using statement - pros cons

Can someone pitch in their opinion about pros/cons between wrapping the DataContext in an using statement or not in LINQ-SQL in terms of factors as performance, memory usage, ease of coding, right thing to do etc. Update: In one particular…
hIpPy
  • 4,649
  • 6
  • 51
  • 65
23
votes
3 answers

Why can't I put a "using" declaration inside a class declaration?

I understand the troubles you can get into when you put a using declaration inside a header file, so I don't want to do that. Instead I tried to put the using (or a namespace foo =) within the class declaration, to cut down on repetitive typing…
Dan
  • 5,929
  • 6
  • 42
  • 52
23
votes
1 answer

When using yield within a "using" statement, when does Dispose occur?

I have a question regarding deferred execution and the disposing of data. Consider the following example: private IEnumerable ParseFile(string fileName) { using(StreamReader sr = new StreamReader(fileName)) { string line; …
Middas
  • 1,862
  • 1
  • 29
  • 44
23
votes
4 answers

How write several using instructions?

Possible Duplicate: using statement with multiple variables I have several disposable object to manage. The CA2000 rule ask me to dispose all my object before exiting the scope. I don't like to use the .Dispose() method if I can use the using…
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200
22
votes
2 answers

How do you resolve .Net namespace conflicts with the 'using' keyword?

Here's the problem, you include multiple assemblies and add 'using namespaceX' at the top of your code file. Now you want to create a class or use a symbol which is defined in multiple namespaces, e.g. System.Windows.Controls.Image &…
Gishu
  • 134,492
  • 47
  • 225
  • 308
22
votes
8 answers

Detecting a Dispose() from an exception inside using block

I have the following code in my application: using (var database = new Database()) { var poll = // Some database query code. foreach (Question question in poll.Questions) { foreach (Answer answer in question.Answers) { …
Edwin Jarvis
  • 5,980
  • 6
  • 36
  • 41
22
votes
3 answers

C#: "Using" Statements with HttpWebRequests/HttpWebResponses

Jon Skeet made a comment (via Twitter) on my SOApiDotNet code (a .NET library for the pre-alpha Stack Overflow API): @maximz2005 One thing I've noticed just from browsing the source quickly: you don't disposed (sic) of WebResponses. "using"…
Maxim Zaslavsky
  • 17,787
  • 30
  • 107
  • 173
22
votes
4 answers

Questions about Entity Framework Context Lifetime

I have some questions about the desired lifetime of an Entity Framework context in an ASP.NET MVC application. Isn't it best to keep the context alive for the shortest time possible? Consider the following controller action: public ActionResult…
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
21
votes
1 answer

VS 2015 - C# simplify/truncate using namespaces

I would prefer the following using Truncating.Long.Using.Namespace.Xxx; Visual Studio 2015, does the following using Xxx; I figured out, that I can change the behavior for the code hint (IDE0001) by adjusting "Code Analysis" settings. But I could…
iwhp
  • 843
  • 6
  • 16
21
votes
8 answers

How to write a 'using' statement for enum classes?

In a rock, paper, scissors program that I am writing, I am enumerating the three different moves and declaring them as a class. However, when I try to write a using statement so that I have to avoid using the scope operator, it doesn't seem to work.…
Victor Odouard
  • 1,345
  • 3
  • 15
  • 28
20
votes
10 answers

Is there a better deterministic disposal pattern than nested "using"s?

In C#, if I want to deterministically clean up non-managed resources, I can use the "using" keyword. But for multiple dependent objects, this ends up nesting further and further: using (FileStream fs = new FileStream("c:\file.txt",…
Eclipse
  • 44,851
  • 20
  • 112
  • 171
20
votes
1 answer

Why can't I call an extension method as a static method when using static import?

Background: I had a static class, but the static methods weren't extension methods. I decided to refactor the methods into extension methods and didn't expect any code to break since extension methods can be called like static methods. However,…
astidham2003
  • 966
  • 1
  • 11
  • 33
20
votes
2 answers

Is it considered bad style to declare multiple "use" statements in Rust?

Is it considered bad style to declare multiple "use" statements in Rust? I am a C++ programmer that recently began trying out Rust. One thing I've noticed as I review Rust code is that in many Rust programs there will be a bunch of use statements at…
Daniel Robertson
  • 1,354
  • 13
  • 22