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
15
votes
2 answers

Something similar to "using" that will create an object and call a method on it when done, but let me do what I want in between

I'm using Lidgren and for every new type of message I make, I end up writing the same kind of code. I'm creating an instance of NetOutgoingMessage, running various assignment calls on it, then sending it when I'm done. Creation and send are the…
Dan B
  • 357
  • 1
  • 2
  • 15
15
votes
6 answers

Try/Finally block vs calling dispose?

Is there any difference between these two code samples and if not, why does using exist? StreamWriter writer; try { writer = new StreamWriter(...) writer.blahblah(); } finally { writer.Dispose(); } vs: using (Streamwriter writer = new…
rollsch
  • 2,518
  • 4
  • 39
  • 65
15
votes
5 answers

Using statement and try-catch()-finally repetition?

The using(...) statement is syntactic sugar for try{} finally {}. But if I then have a using statement like below: using (FileStream fs = File.Open(path)) { } Now I want to catch the exceptions that opening this file could cause (and this is…
GurdeepS
  • 65,107
  • 109
  • 251
  • 387
14
votes
4 answers

Does a MemoryStream get disposed of automatically when returning it as an ActionResult?

public ActionResult CustomChart(int reportID) { Chart chart = new Chart(); // Save the chart to a MemoryStream var imgStream = new MemoryStream(); chart.SaveImage(imgStream); imgStream.Seek(0, SeekOrigin.Begin); // Return…
Sean Anderson
  • 27,963
  • 30
  • 126
  • 237
14
votes
1 answer

Multiple using directives in function

Instead of including a whole namespace in a given function, I like to only include the stuff I'm going to use, like: void doStuff() { using std::sin; using std::cos; ... // do stuff } Sometimes this list grows longer. I wanted to…
pingul
  • 3,351
  • 3
  • 25
  • 43
14
votes
8 answers

What are the benefits of maintaining a "clean" list of using directives in C#?

I know VS2008 has the remove and sort function for cleaning up using directives, as does Resharper. Apart from your code being "clean" and removing the problem of referencing namespaces which might not exist in the future, what are the benefits of…
Carl
  • 2,483
  • 4
  • 28
  • 30
14
votes
6 answers

Calling Dispose() vs when an object goes out scope/method finishes

I have a method, which has a try/catch/finaly block inside. Within the try block, I declare SqlDataReader as follows: SqlDataReader aReader = null; aReader = aCommand.ExecuteReader(); In the finally block, the objects which are manually…
csharpdev
  • 297
  • 2
  • 5
  • 9
13
votes
3 answers

Defining a Type Alias in C# across multiple files

In C++, it's easy to write something along the lines of: #ifdef FAST typedef Real float; #endif #ifdef SLOW typedef Real double; #endif #ifdef SLOWER typedef Real quad; #endif In some common header file so I could simply write one version of code…
Mike Bailey
  • 12,479
  • 14
  • 66
  • 123
13
votes
1 answer

Using declaration for type alias template in derived class with tempate base

If the base class depends on the template parameter, its scope is not examined in the unqualified name lookup. I can use the using declaration to introduce names from the base class. Suppose now I have a type alias template in the base class. Can…
Evg
  • 25,259
  • 5
  • 41
  • 83
13
votes
11 answers

Why is 'using' improving C# performances

It seems that in most cases the C# compiler could call Dispose() automatically. Like most cases of the using pattern look like: public void SomeMethod() { ... using (var foo = new Foo()) { ... } // Foo isn't use after…
Wernight
  • 36,122
  • 25
  • 118
  • 131
13
votes
1 answer

Apply a 'using std::foo' directive to a constructor initializer list locally (C++)

Given a custom type, the following fragment shows the common approach for allowing a function to automatically select a user provided overload specific to the type, or a generic implementation of a function from the standard library if not. //…
marack
  • 2,024
  • 22
  • 31
13
votes
5 answers

WCF Errors using WCFTestClient to test a simple WCF Web Service

When I try to test the AutoLotWCFService using "wcftestclient", I get the following error. What am I doing wrong? Any insight will help. This is a simple Web Service that has wshttpbinding with interface contract and the implementation in the…
Neal Unadkat
13
votes
4 answers

C++ - Forward declaration and alias (with using or typedef)

I need to implement the following interface struct mutex; struct interface { //... mutex& getMutex(); }; Intuition would I could use using mutex = ParticularMutex in my implementation, but gcc tells me otherwise: error: conflicting declaration…
ricab
  • 2,697
  • 4
  • 23
  • 28
12
votes
5 answers

Create a temporary file from stream object in c#

Given a stream object which contains an xlsx file, I want to save it as a temporary file and delete it when not using the file anymore. I thought of creating a class that implementing IDisposable and using it with the using code block in order to…
Yair Nevet
  • 12,725
  • 14
  • 66
  • 108
12
votes
2 answers

How does LINQ defer execution when in a using statement

Imagine I have the following: private IEnumerable MyFunc(parameter a) { using(MyDataContext dc = new MyDataContext) { return dc.tablename.Select(row => row.parameter == a); } } private void UsingFunc() { var result = MyFunc(new…
Spence
  • 28,526
  • 15
  • 68
  • 103