Linked Questions
97 questions linked to/from What are the uses of "using" in C#?
333 votes
9 answers
402k views
What is the C# Using block and why should I use it? [duplicate]
What is the purpose of the Using block in C#? How is it different from a local variable?
75 votes
14 answers
91k views
When should I use "using" blocks in C#? [duplicate]
Are there particular instances where I should (or shouldn't?) be using "using" blocks: using(SomeType t = new SomeType()){ ... }
22 votes
14 answers
54k views
use of "using" keyword in c# [duplicate]
i want to know what is the use of "using" keyword in c#, i am new to it.. when we need to use "using" keyword.. i googled it, could not be satisfied with answers. still i want to know some more from ...
4 votes
7 answers
6k views
Whether should I use try{} or using() in C#? [duplicate]
I'm new to C#,been a pascal lover until I found C# in Depth.In Delphi there is a try{} statement that is also implemented in C#. However,I've seen some of you mentioning "Using(){} is better than try{...
0 votes
6 answers
643 views
What is the point of having using blocks in C# code? [duplicate]
I see loads of code snippets with the following Syntax using (RandomType variable = new RandomType(1,2,3)) { // A bunch of code here. } why not just declare the variable and use it? This Using ...
5 votes
6 answers
2k views
What is the purpose of Using? [duplicate]
DUPE: Uses of "using" in C# I have seen people use the following and I am wondering what is its purpose? Is it so the object is destroyed after its use by garbage collection? Example: ...
3 votes
1 answer
1k views
'using' keyword before local variable declaration in C#? [duplicate]
Hi I was going through some examples in C# and came across some code similar to below : private void WriteData(string filePath) { using var writer = new System.IO.StreamWriter(filePath); //...
0 votes
5 answers
221 views
What the reason for enclosing code in a "using" in c# [duplicate]
I have the following code: try { using (var context = new DataContext()) { if (!context.Database.Exists()) { ...
-1 votes
2 answers
314 views
dispose or use the keyword "using" [duplicate]
Do I really need to use comm.Dispose() and conn.Dispose() or directly used the using? using (OdbcConnection conn = new OdbcConnection()) { conn.Open(); OdbcCommand comm = new OdbcCommand(sql, ...
0 votes
2 answers
199 views
C# What is the point of the using statement? [duplicate]
I'm not talking about references to assemblies, rather the using statement within the code. For example what is the difference between this: using (DataReader dr = .... ) { ...stuff involving data ...
1 vote
3 answers
161 views
Using 'using' in front of StreamWriter [duplicate]
I'm doing a tutorial on how to save a text file with StreamWriter. I understand how to do it but I started reading a tutorial on the StreamWriter Class to get a better understanding of it. In an ...
2 votes
2 answers
87 views
Is this the right usage of using? [duplicate]
I am not sure how to use using statement correct. I try like this: [HttpDelete("{ClubId}", Name = "DeleteOpenings_Club")] public async Task<IActionResult> ...
5 votes
0 answers
276 views
.Net Framework: Finally block is not being called when the exception is not being caught [duplicate]
A simple console application, in Visual Studio 2019, .Net Framework 4.7, Windows: static void Main(string[] args) { try { Console.WriteLine("In try"); throw new ...
0 votes
1 answer
240 views
IDisposable, using and GarbageColleciton [duplicate]
I got stuck in the weeds of how IDisposable and the GarbageCollector work. Suppose you have an IDisposable object, which doesn't actually have an resources that it's holding onto (but the Dispose() ...
-2 votes
2 answers
104 views
Understanding C# proper uses of 'using' keyword [duplicate]
Considering the follow code can someone explain to me why and why not to use 'using'? I've read about the rule of thumb with IDisposable but why is IDisposable the determining factor? private string ...
1 vote
0 answers
163 views
C# Singleton in a using statement - what happens to the Singleton at the end of the using statement [duplicate]
Suppose I have private static MyClient _myClient that opens a connection to an external server. It implements IDisposable. The 'client' is created on first use: if (_myClient == null) _myClient = ...
-2 votes
3 answers
77 views
using on object vs not using it on object c# [duplicate]
Whats the difference and benifits of "using" in this case. I will state two simular cases please explain why I should or shoudn't use "using" before my reader in this case. string ...
0 votes
0 answers
91 views
USING statement use cases in C# [duplicate]
I would like to know in which specific cases to use the USING statement, which is its main functionality when including it in the code. In my case I am scheduling a job for the university, the ...
0 votes
1 answer
71 views
Inline using how to make use of it [duplicate]
In C# one can type the using verb in line width the code sometimes, like using (textwriter){ ..... } I like that writing style and am wondered what is required to allow that for my own Api's.
234 votes
14 answers
62k views
Why should you remove unnecessary C# using directives?
For example, I rarely need: using System.Text; but it's always there by default. I assume the application will use more memory if your code contains unnecessary using directives. But is there ...
124 votes
10 answers
119k views
Use a 'try-finally' block without a 'catch' block
Are there situations where it is appropriate to use a try-finally block without a catch block?
9 votes
5 answers
5k views
Using statement with try catch. What happens to instance from using statement?
If I have a using block surrounding a try catch statement what will happen to the object inside that using statement should the catch fire an exception? Consider the following code: using (...
7 votes
5 answers
4k views
What does using(object obj = new Object()) mean?
What does this statement means in C#? using (object obj = new object()) { //random stuff }
10 votes
3 answers
10k views
Do I need to close SQL Server connection with the using keyword?
I keep finding conflicting results for this question. Let's look at this C# code of running an SQL query: using (SqlConnection cn = new SqlConnection(strConnectString)) { cn.Open(); using (...
6 votes
3 answers
17k views
How to get column names from a table in sqlite via PRAGMA (.net / c#)?
I have been struggling to get the right c# code for getting the values after a PRAGMA table_info query. Since my edit with extra code was rejected in this post, I made this question for other people ...
3 votes
4 answers
342 views
Why do we not need to return a value outside of the `using` scope?
Consider this code: public int DownloadSoundFile() { using (var x= new X()) { return x.Value; } } and this code: public int DownloadSoundFile() { if (x!=null) { return ...
8 votes
4 answers
14k views
Will an object be disposed automatically after an asynchronous event it subscribed to is raised?
Let's suppose I have this function that can be called several times from the main thread. Every time this is called, I create a WebClient object to download some data asynchronously. My question... ...
7 votes
3 answers
5k views
Do I need to close connection when using a 'using' statement [duplicate]
Currently my code is constructed as follows. using (var sqlCon = new SqlConnection(Context.ReturnDatabaseConnection())) { sqlCon.Open(); try { ...
5 votes
2 answers
20k views
How to include multiple classes in one project with one namespace. How to use them?
I am new in C# that's why I am struggling with basic concepts. I have created multiple classes in one project under one namespace out which one class is having Main() and other classes don't have. ...
1 vote
4 answers
11k views
Syntax error in INSERT INTO statement. System.Data.OleDb.OleDbErrorCollection
Hi I am creating basic form in Visual Studio 2012 Express. I am using Microsoft Access 2012 as database. My problem is when I press submit button I nothing happens. Syntax error in INSERT INTO ...