A `using` statement is a C# language feature that simplifies deterministic cleanup of disposable resources. Not to be confused with the `using` directive (related to namespaces), for which use tag `using-directives`.

learn more… | top users | synonyms

1
vote
3answers
40 views

Returning IQueryable from within a using block. Need better design

I have created a PhoneBook style application; on my phonebook object I have a local member _site which is used as a filter, since there are approximately 1000 phone numbers, split across 12 sites ...
1
vote
2answers
80 views

C# “using” blocks and catching exceptions [duplicate]

I have this code: using (var requestStream = request.GetRequestStream()) { byte[] data = Encoding.UTF8.GetBytes(xmlData); requestStream.Write(data, 0, data.Length); } If the request I'm ...
1
vote
2answers
29 views

Using for IDbConnection/IDbTransaction safe to use?

While my assumption may seem to sound subjective, after some research, I found that it's not uncommon to find developers who favour a dummy Try/Catch instead of using the Using statement for ...
1
vote
4answers
50 views

Which is the better, an instance variable or a local variable for an EF context?

I know that the title looks a bit bad :) But I dont know how to explain my problem.. This is typically a basic problem for me but I dont know answer.. I am writing a server application which is ...
2
votes
2answers
77 views

Best practice to avoid multiple disposals with the `using` keyword in c#

When a variable is IDisposable, we have the using keyword to manage the disposal. But what if we return the value in a method, should we have using twice? StringContent stringToStringContent(string ...
6
votes
5answers
208 views

Using 'Using' for things other than resource disposal [duplicate]

We all know that the using statement is really good for resources you want to cleanup in a timely manner, such as an open file or database connection. I was wondering if it would be considered a good ...
3
votes
3answers
81 views

Should I use a using statement to create a Windows.Forms.Form object?

I've read (on using Statement (C# Reference)) that the using statement should be used to release the resources used by managed types (like File and Font) that use unmanaged resources. So started using ...
9
votes
1answer
120 views

Do using statements and await keywords play nicely in c#

I have a situation where I am making an async call to a method that returns and IDisposable instance. For example: HttpResponseMessage response = await httpClient.GetAsync(new ...
1
vote
1answer
35 views

oracle dynamic sql using params with brackets as table name

I have a table in my schema with brackets in its name (that's a legacy, cannot be modified): CREATE TABLE "Addresses" ("ID" NUMBER(*,0) , "FullAddress" ...
4
votes
2answers
54 views

Is object eligible for garbage collection if it's created within using-statement and not explicitly bound to a reference?

I have this (illustration only) C# code: using( new System.IO.MemoryStream() ) { System.Threading.Thread.Sleep(1000); } Note that here a MemoryStream is created and not explicitly bound to a ...
0
votes
2answers
51 views

Saving/Loading Using()/Try-Catch

I've been digging around the past couple hours on how to implement simple save/load functionality to a game i'm making. The save data is being stored in a text file, during the game the player can ...
1
vote
3answers
156 views

With… End With vs Using in VB.NET

I just found out that like C#, VB.NET also has the using keyword. Until now I thought it didn't have it (stupid of me, I know...) and did stuff like this instead: With New ...
3
votes
5answers
83 views

Correct scope of using using

I'm in a quarrel with a colleague regarding the appropriate scope of the using statement. This is the method in question. public Guid IsServerReachable() { try { WhoAmIResponse whoAmI; ...
2
votes
1answer
73 views

How to mock an object, that is instanciated in a using() block

When reading/writing to a context from Entity Framework I often read, that it is recommended to keep the context's lifecycle as short as possible (one context per unit of work). It makes a lot of ...
3
votes
1answer
57 views

Will the clean-up logic still get called if an exception is thrown from within a using statement?

I have some questions about using the using keyword. I have the following code: try { using (System.Net.WebResponse response = httpWebRequest.GetResponse()) { throw new Exception("Example"); ...

1 2 3 4 5 15
15 30 50 per page