using-statement

Closing SqlConnection and SqlCommand c#

为君一笑 提交于 2019-11-30 11:33:19
In my DAL I write queries like this: using(SQLConnection conn = "connection string here") { SQLCommand cmd = new ("sql query", conn); // execute it blah blah } Now it just occurred to me that I am not explicitly closing the SQLCommand object. Now I know the 'using' block will take care of the SQLConnection object, but will this also take care of the SQLCommand object? If not than I have a serious problem. I would have to put in the 'using' on the SQLCommand on thousands and thousands of lines of code or do a cmd.Close() on hundreds of methods. Please tell me that if putting in the using or

Bad practice? Non-canon usage of c#'s using statement

不想你离开。 提交于 2019-11-30 11:08:00
C# has the using statement, specifically for IDisposable objects. Presumably, any object specified in the using statement will hold some sort of resource that should be freed deterministically. However, it seems to me that there are many designs in programming which have a single, definite beginning and end, but lack intrinsic language support. The using construct provides an opportunity to use the built in features of a code editor to, at least, clearly and naturally highlight the scope of such a design or operation. What I have in mind is the sort of operation that frequently starts with a

Identify IDisposable objects

戏子无情 提交于 2019-11-30 09:01:19
i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin. thanks. Neil Barnwell I know what you mean. I don't know, but look at FxCop. It might have a rule in there somewhere that checks whether objects implementing IDisposable are not disposed. Just a hunch, mind. UPDATE : Mitch Wheat writes: FxCop includes the rule,

Best practice regarding returning from using blocks [duplicate]

早过忘川 提交于 2019-11-30 03:41:22
问题 This question already has answers here : returning in the middle of a using block (7 answers) Closed 6 years ago . Which way is better practice: return a value from a method inside an using statement or declare a variable before, set it inside and return it after? public int Foo() { using(..) { return bar; } } or public int Foo() { var b = null; using(..) { b = bar; } return b; } 回答1: I prefer the first example. Fewer variables, fewer lines of code, easier to follow, easier to maintain...

Closing SqlConnection and SqlCommand c#

佐手、 提交于 2019-11-29 17:10:55
问题 In my DAL I write queries like this: using(SQLConnection conn = "connection string here") { SQLCommand cmd = new ("sql query", conn); // execute it blah blah } Now it just occurred to me that I am not explicitly closing the SQLCommand object. Now I know the 'using' block will take care of the SQLConnection object, but will this also take care of the SQLCommand object? If not than I have a serious problem. I would have to put in the 'using' on the SQLCommand on thousands and thousands of lines

Identify IDisposable objects

霸气de小男生 提交于 2019-11-29 12:15:03
问题 i have to review a code made by some other person that has some memory leaks. Right now i'm searching the disposable objects to enclause them with the using statement and i would like to know if there is a quick way that tells you all the disposable objects declared in. I mean something like resharper or another visual studio plugin. thanks. 回答1: I know what you mean. I don't know, but look at FxCop. It might have a rule in there somewhere that checks whether objects implementing IDisposable

How to determine whether a .NET exception is being handled?

北城以北 提交于 2019-11-29 11:48:21
问题 We're investigating a coding pattern in C# in which we'd like to use a "using" clause with a special class, whose Dispose() method does different things depending on whether the "using" body was exited normally or with an exception. To the best of my understanding, the CLR keeps track of the current exception being handled until it's been consumed by a "catch" handler. However it's not entirely clear whether this information is exposed in any way for the code to access. Do you know whether it

Which is better, and when: using statement or calling Dispose() on an IDisposable in C#?

泄露秘密 提交于 2019-11-29 10:37:34
Suppose I have the following: using(var ctx = DataContextFactory.Create(0)) { ... Some code ... } Why not just do the following and lose a couple of curly braces?: var ctx = DataContextFactory.Create(0); ctx.Dispose(); Thanks for the advice! The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where Create(0) returns null (i.e. it doesn't attempt to call Dispose() on a null instance). Trevor Robinson A using statement is always better because... you can't forget to call Dispose() , even as the code evolves into different code paths

Disposables, Using & Try/Catch Blocks

时间秒杀一切 提交于 2019-11-29 10:32:31
Having a mental block today, need a hand verifying my logic isn't fubar'ed. Traditionally I would do file i/o similar to this: FileStream fs = null; // So it's visible in the finally block try { fs = File.Open("Foo.txt", FileMode.Open); /// Do Stuff } catch(IOException) { /// Handle Stuff } finally { if (fs != null) fs.Close(); } However, this isn't very elegant. Ideally I'd like to use the using block to dispose of the filestream when I'm done, however I am unsure about the synergy between using and try/catch. This is how i'd like to implement the above: try { using(FileStream fs = File.Open(

MemoryStream in Using Statement - Do I need to call close()

萝らか妹 提交于 2019-11-29 09:03:35
When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here? using (MemoryStream ms = new MemoryStream(byteArray)) { // stuff ms.Close(); } No, it's not. using ensures that Dispose() will be called, which in turn calls the Close() method. You can assume that all kinds of Streams are getting closed by the using statement. From MSDN : When you use an object that accesses unmanaged resources, such as a StreamWriter, a good practice is to create the instance with a using statement. The using statement automatically closes the stream and calls