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
19
votes
3 answers

C# ValueTuple with disposable members

Let's say I have a method foo which returns a ValueTuple where one of it's members is disposable, for example (IDisposable, int). What is the best way to make sure the returned disposable object is correctly disposed on the calling side? I tried the…
Robert Hegner
  • 9,014
  • 7
  • 62
  • 98
19
votes
1 answer

Async inside Using block

I have the following async function in C#: private async Task CallDatabaseAsync(Func> execAsync) { using (var connection = new SqlConnection(_connectionString)) { connection.Open(); return await…
Andrew
  • 1,139
  • 1
  • 12
  • 27
19
votes
4 answers

How to get project wide Remove and Sort Usings for Visual Studio 2010?

We had the Power Commands for Visual Studio 2008 that add a context menu command that Removed Unused Usings and Sorted Usings for all files in a project/solution. How to do the same in VS2010 since this plugin is incompatible?
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
17
votes
8 answers

Struct and IDisposable

I wonder why does it not compile? public static void Main(string[] args) { using (MyStruct sss = new MyStruct()) { sss.s = "fsdfd";// Cannot modify members of 'sss' because it is a 'using variable' //sss.Set(12); //but…
Alexandre
  • 13,030
  • 35
  • 114
  • 173
17
votes
2 answers

Yield return inside usings

If I recall correctly that when I used yield inside using SqlConnection blocks I got runtime exceptions. using (var connection = new SqlConnection(connectionString)) { var command = new SqlCommand(queryString, connection); …
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
17
votes
5 answers

Does a C# using statement perform try/finally?

Suppose that I have the following code: private void UpdateDB(QuoteDataSet dataSet, Strint tableName) { using(SQLiteConnection conn = new SQLiteConnection(_connectionString)) { conn.Open(); using (SQLiteTransaction…
Kiril
  • 39,672
  • 31
  • 167
  • 226
17
votes
6 answers

Accessing FTP on Google Compute Engine

I'm running an instance on debian-7-wheezy and I'm sort of new to the Google Compute Engine. I have looked through both the support requests on this site and the FAQ post on the Google website; however I found nothing that I could follow…
user3877342
  • 171
  • 1
  • 1
  • 3
17
votes
12 answers

What requires me to declare "using namespace std;"?

This question may be a duplicate, but I can't find a good answer. Short and simple, what requires me to declare using namespace std; in C++ programs?
vette982
  • 4,782
  • 8
  • 34
  • 41
17
votes
8 answers

Do you prefer explicit namespaces or 'using' in C++?

When using C++ namespaces, do you prefer to explicitly name them, like this: std::cout << "Hello, world!\n"; Or do you prefer using namespace: using namespace std; cout << "Hello, world!\n"; And if if you prefer the latter, do you declare your…
Rob
  • 76,700
  • 56
  • 158
  • 197
16
votes
2 answers

Explain why "using" won't work in service?

So I was stuck on this problem for about a week. I was trying to run a project to recieve a TCP connection and start a SignalR Hub as a Service. Both worked perfectly running the project as a .exe file. The TCP part would work perfectly, however I…
Tomaltach
  • 913
  • 1
  • 11
  • 30
16
votes
3 answers

I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object

Here is my sample code that I am using to fetch data from database: on DAO layer: public IEnumerable GetDATA(ICommonSearchCriteriaDto commonSearchCriteriaDto) { using(DbContext) { DbDataReader reader =…
Sanjeev Rai
  • 6,721
  • 4
  • 23
  • 33
16
votes
7 answers

What's this C# "using" directive?

I saw this C# using statement in a code example: using StringFormat=System.Drawing.StringFormat; What's that all about?
Ronnie Overby
  • 45,287
  • 73
  • 267
  • 346
15
votes
3 answers

What are the differences between typedef and using?

What are the differences between using typedef Some::Nested::Namespace::TypeName TypeName; or using Some::Nested::Namespace::TypeName; to provide the shorthand TypeName in the local scope?
palm3D
  • 7,970
  • 6
  • 28
  • 33
15
votes
5 answers

Get checkbox status using javascript

This is my checkbox HTML code class="checkbox"> this is javascript code var terms = $("#termsCheckbox"); function…
Sasindu H
  • 1,538
  • 7
  • 24
  • 43
15
votes
2 answers

Java equivalent of C# 'using' statement

Possible Duplicate: “using” keyword in java I'm transitioning from C# to java, so please bear with me... When reading a file in C#, you simply wrap it all in a big 'using' block, so that if you have an exception, the file will still be closed.…
Chris
  • 39,719
  • 45
  • 189
  • 235