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
197 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
275 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 ...