Linked Questions

333 votes
9 answers
402k views

What is the purpose of the Using block in C#? How is it different from a local variable?
Ryan Michela's user avatar
  • 8,414
75 votes
14 answers
91k views

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

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 ...
Naruto's user avatar
  • 9,654
4 votes
7 answers
6k views

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{...
Ivan Prodanov's user avatar
0 votes
6 answers
643 views

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 ...
Omar Kooheji's user avatar
  • 55.9k
5 votes
6 answers
2k views

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: ...
user avatar
3 votes
1 answer
1k views

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); //...
Jasmeet's user avatar
  • 1,495
0 votes
5 answers
221 views

I have the following code: try { using (var context = new DataContext()) { if (!context.Database.Exists()) { ...
user avatar
-1 votes
2 answers
314 views

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, ...
user avatar
0 votes
2 answers
199 views

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 ...
Mark Roworth's user avatar
1 vote
3 answers
161 views

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 ...
user avatar
2 votes
2 answers
87 views

I am not sure how to use using statement correct. I try like this: [HttpDelete("{ClubId}", Name = "DeleteOpenings_Club")] public async Task<IActionResult> ...
JdDEV's user avatar
  • 43
5 votes
0 answers
276 views

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 ...
S Itzik's user avatar
  • 564
0 votes
1 answer
240 views

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() ...
Brondahl's user avatar
  • 8,743
-2 votes
2 answers
104 views

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 ...
Gabriel W's user avatar
  • 155
1 vote
0 answers
163 views

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 = ...
FoolongC's user avatar
-2 votes
3 answers
77 views

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 ...
TimBro's user avatar
  • 1
0 votes
0 answers
91 views

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 ...
Nicolas Martinez 's user avatar
0 votes
1 answer
71 views

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.
Peter's user avatar
  • 2,336
234 votes
14 answers
62k views

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 ...
steffenj's user avatar
  • 8,115
124 votes
10 answers
119k views

Are there situations where it is appropriate to use a try-finally block without a catch block?
mkus's user avatar
  • 3,537
9 votes
5 answers
5k views

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 (...
CSharpened's user avatar
  • 12.8k
7 votes
5 answers
4k views

What does this statement means in C#? using (object obj = new object()) { //random stuff }
Ricardo's user avatar
  • 12.1k
10 votes
3 answers
10k views

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 (...
c00000fd's user avatar
  • 22.8k
6 votes
3 answers
17k views

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 ...
e-motiv's user avatar
  • 5,893
3 votes
4 answers
342 views

Consider this code: public int DownloadSoundFile() { using (var x= new X()) { return x.Value; } } and this code: public int DownloadSoundFile() { if (x!=null) { return ...
user avatar
8 votes
4 answers
14k views

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... ...
LEM's user avatar
  • 863
7 votes
3 answers
5k views

Currently my code is constructed as follows. using (var sqlCon = new SqlConnection(Context.ReturnDatabaseConnection())) { sqlCon.Open(); try { ...
Code Ratchet's user avatar
  • 6,077
5 votes
2 answers
20k views

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. ...
Surbhi's user avatar
  • 4,078
1 vote
4 answers
11k views

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 ...
Mohammad Shafi Shaikh's user avatar

15 30 50 per page