using-statement

return the variable used for using inside the using C#

孤者浪人 提交于 2019-12-05 16:57:21
问题 I am returning the variable I am creating in a using statement inside the using statement (sounds funny): public DataTable foo () { using (DataTable properties = new DataTable()) { // do something return properties; } } Will this Dispose the properties variable?? After doing this am still getting this Warning: Warning 34 CA2000 : Microsoft.Reliability : In method 'test.test', call System.IDisposable.Dispose on object 'properties' before all references to it are out of scope. Any Ideas? Thanks

I don't quite understand the workings of using/Disposable objects

房东的猫 提交于 2019-12-05 06:01:13
I asked a question regarding returning a Disposable ( IDisposable ) object from a function , but I thought that I would muddle the discussion if I raised this question there. I created some sample code: class UsingTest { public class Disposable : IDisposable { public void Dispose() { var i = 0; i++; } } public static Disposable GetDisposable(bool error) { var obj = new Disposable(); if (error) throw new Exception("Error!"); return obj; } } I coded it this way deliberately, because then I call: using (var tmp = UsingTest.GetDisposable(true)) { } Using the debugger, I notice that the Dispose

using(IDisposable obj = new …) in C# to write code blocks in stream (e.g. XML)

前提是你 提交于 2019-12-05 05:50:50
I have started to use classes implementing IDisposable to write blocks in streams, with the using statement. This is helpful to keep a correct nesting and avoid missing or wrongly placed start/end parts. Basically, the constructor writes the start of a block (e.g. opening XML tag), Dispose() the end (e.g. closing XML tag). Example is the UsableXmlElement below (it's for large XMLs, so LINQ to XML or XmlDocument in memory are no options). However, these IDisposable do not implement the sophisticated pattern recommended by Microsoft, with Destructor/Finalizer, separate Dispose(bool) method and

C# using statement

半城伤御伤魂 提交于 2019-12-05 05:16:23
I really want to get this out of my head. Please see below code: using (DataTable resultTable = DBUtility.GetSingleDBTableResult(connectionString, "SELECT * FROM MyDBTable")) { List<string> resultsList = new List<string>(); foreach (DataRow dataRow in resultTable.Rows) { resultsList.Add(dataRow[0].ToString()); } return resultsList; } Is the datatable disposed? Can someone explain how this is translated to a try/catch/finally block? MSDN states that if an exception occurred, the Dispose method will still be called but what about the return statement? Or should i just use below code: List<string

Use of Process with using block [duplicate]

空扰寡人 提交于 2019-12-05 01:44:57
This question already has answers here : Closed 8 years ago . Possible Duplicate: What happens if I don't close a System.Diagnostics.Process in my C# console app? As System.Diagnostics.Process inherits from Component which implements IDisposable , should I always create a Process with a using block? For example, this...: using (var process = new Process()) { process.StartInfo.FileName = "some process.exe"; process.Start(); process.WaitForExit(); } ...instead of this: var process = new Process { StartInfo = { FileName = "some process.exe" } }; process.Start(); process.WaitForExit(); I ask

Using statement around dialog form to ensure garbage collection

痞子三分冷 提交于 2019-12-04 19:54:45
问题 We have a Windows Forms application that contains thousands of forms. Many of these are temporarily displayed as dialogs via the ShowDialog() method. This application has been around for years and we've discovered that many of the forms are not getting garbage collected in a timely manner due to various resource leaks in the form or the controls that it uses. Specifically, we've found examples of GDI+ resources that aren't being disposed of properly, although there may be other types of

When are C# “using” statements most useful?

强颜欢笑 提交于 2019-12-04 16:46:57
问题 So a using statement automatically calls the dispose method on the object that is being "used", when the using block is exited, right? But when is this necessary/beneficial? For example let's say you have this method: public void DoSomething() { using (Font font1 = new Font("Arial", 10.0f)) { // Draw some text here } } Is it necessary to have the using statement here, since the object is created in the method? When the method exits, wont the Font object be disposed of anyway? Or does the

Nested using statements

懵懂的女人 提交于 2019-12-04 15:17:31
问题 As Eric Gunnerson shows in this blog post, in C# you can nest using statements as: using (StreamWriter w1 = File.CreateText("W1")) using (StreamWriter w2 = File.CreateText("W2")) { // code here } Is there a similar way to do it in VB.Net? I want to avoid too many indentation levels. 回答1: Like this: Using a As New Thingy(), _ b As New OtherThingy() ... End Using 回答2: Well, you can do: Using w1 = File.CreateText("W1"), w2 = File.CreateText("W2") ' Code goes here. ' End Using 来源: https:/

sqlConnection/Command using statement + try/catch block [duplicate]

风流意气都作罢 提交于 2019-12-04 09:11:30
This question already has an answer here: try/catch + using, right syntax 7 answers What is a correct approach try/catch inside using or using inside try/catch? using (SqlConnection connection = CreateSqlConnection(connString)) { using (SqlCommand command = CreateSqlCommand() { try{//open connection + execute command + do something else} catch{//do something} } } vs. try { using (SqlConnection connection = CreateSqlConnection(connString)) { using (SqlCommand command = CreateSqlCommand() { //open connection + execute command + do something else } } } catch { //do something } From my point of

Exception in “using” statement with WCF not closing connections properly. How does one close faulted WCF client connections or those with exceptions?

假如想象 提交于 2019-12-04 07:41:18
There are several questions on StackOverflow regarding closing WCF connections, however the highest ranking answers refers to this blog: http://marcgravell.blogspot.com/2008/11/dontdontuse-using.html I have a problem with this technique when I set a breakpoint at the server and let the client hang for more than one minute. (I'm intentionally creating a timeout exception) The issue is that the client appears to "hang" until the server is done processing. My guess is that everything is being cleaned up post-exception. In regard to the TimeOutException it appears that the retry() logic of the