2

I am not sure how to use using statement correct.

I try like this:

[HttpDelete("{ClubId}", Name = "DeleteOpenings_Club")] public async Task<IActionResult> DeleteClub_IsOpening([FromRoute] string ClubId) { using (_db_ClubIsOpen) { var result = _db_ClubIsOpen.ClubIsOpen_TBL.Where(x => x.FK_Club == ClubId); foreach (var item in result) { _db_ClubIsOpen.ClubIsOpen_TBL.Remove(item); } _db_ClubIsOpen.SaveChanges(); return Ok(); } } 
12
  • no is my DBContext i work with EF6 Commented Apr 22, 2021 at 14:34
  • 2
    What is _db_ClubIsOpen? where did it come from? what is the expected lifetime? I would not assume that your controller method is meant to be disposing something that it didn't create, so... who did create it? lifetime management is complex, and we don't have all the context here Commented Apr 22, 2021 at 14:34
  • Then you probably don't want using - it's just syntactic sugar, using(X){ ... } is translated to X; try { ... } finally {X.Dispose()} Commented Apr 22, 2021 at 14:35
  • _db_ClubIsOpen almost sounds like it would be a bool, to be honest. Commented Apr 22, 2021 at 14:35
  • 2
    @Lawrence using absolutely generates a try/finally; this is formally documented in the language specification §13.14 (ECMA 334); additionally, using has no relation whatsoever directly to GC Commented Apr 22, 2021 at 14:49

2 Answers 2

5

using is tied into lifetime management, which is a complex question.

Usually, the usage of using you're deploying here would be for things that your method owns; for example, here:

using (var dbContext = new DbContext(whatever)) { // some code } 

(or just using var dbContext = new DbContext(whatever); in recent C# versions)

In the above, we are clearly creating the thing, so it is our job to make sure that it gets disposed, which is happening thanks to the using.

However, in the example in the question, it isn't clear what _db_ClubIsOpen is, or what the lifetime is, or who owns it. By default, I would absolutely not assume that a method is responsible for taking ownership of an arbitrary field, so I would not expect to use using here, in the sense of using (_someField) as shown in the question. Instead, I would expect either some DI/IoC framework to deal with that (if it is being injected), or I would expect the type to implement IDipsosable, and deal with disposing the field in Dispose().

Sign up to request clarification or add additional context in comments.

Comments

0

From the C# Reference:

Provides a convenient syntax that ensures the correct use of IDisposable objects. Beginning in C# 8.0, the using statement ensures the correct use of IAsyncDisposable objects.

string manyLines=@"This is line one This is line two Here is line three The penultimate line is line four This is the final, fifth line."; using (var reader = new StringReader(manyLines)) { string? item; do { item = reader.ReadLine(); Console.WriteLine(item); } while(item != null); } 

The using statement allows you to limit the scope of IDisposable objects and streamline their disposal. Typically you would declare the IDisposable object within the statement of the using structure and use it within the body.

2 Comments

I think everything in here is true, but that at the same time: it doesn't really help answer whether the code in the question is correct/incorrect
Yeah I realize that now :/ I was hoping it would steer OP in the right direction but your answer fleshed that out much better. Still trying to figure out how best to answer questions on SO

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.