using-statement

SqlCommand-SqlConnection Using Disposing issue

一笑奈何 提交于 2019-12-23 12:30:47
问题 According to MSDN If the IDisposable resource of the nested inner using statement contains the resource of the outer using statement, the Dispose method of the nested resource releases the contained resource. MSDN (http://msdn.microsoft.com/en-us/library/ms182334.aspx) => Example Nested using statements (Using in Visual Basic) can cause violations of the CA2202 warning. If the IDisposable resource of the nested inner using statement contains the resource of the outer using statement, the

Dispose MemoryStream when using with .Net Mail Attachment

这一生的挚爱 提交于 2019-12-23 10:16:12
问题 I am using a MemoryStream to add attachments from binary that is stored in a DB. My problem is that I want to properly dispose of the MemoryStream. This is easily done using a "using" statement, but when I have more than one attachment I don't know how to properly dispose of the multiple MemoryStreams. Is there a good way to iterate over and attach the files, but yet at the same time properly dispose of the MemoryStreams that I am using to attach? When I tried to flush/close prior to using

C# using statement

余生长醉 提交于 2019-12-22 05:05:12
问题 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

Use of Process with using block [duplicate]

梦想与她 提交于 2019-12-22 03:44:28
问题 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

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

倖福魔咒の 提交于 2019-12-21 16:47:14
问题 This question already has answers here : try/catch + using, right syntax (7 answers) Closed 5 years ago . 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 =

Where do I put try/catch with “using” statement? [duplicate]

只谈情不闲聊 提交于 2019-12-20 11:18:07
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: try/catch + using, right syntax I would like to try/catch the following: //write to file using (StreamWriter sw = File.AppendText(filePath)) { sw.WriteLine(message); } Do i put the try/catch blocks inside the using statement or, around it? Or both? 回答1: If your catch statement needs to access the variable declared in a using statement, then inside is your only option. If your catch statement needs the object

Object disposal and garbage collection prior to event triggering

余生长醉 提交于 2019-12-20 04:12:20
问题 A piece of code was brought up by someone I was talking to: private void DownloadInformation(string id) { using (WebClient wc = new WebClient()) { wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted); wc.DownloadStringAsync(new Uri("http://www.fake.com/" + id)); } } The above is a simplified version of this: (I have the author's permission to post the image.) What bothers me about that code is that an event handler is attached, DownloadStringAsync()

Object disposal and garbage collection prior to event triggering

蓝咒 提交于 2019-12-20 04:12:17
问题 A piece of code was brought up by someone I was talking to: private void DownloadInformation(string id) { using (WebClient wc = new WebClient()) { wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted); wc.DownloadStringAsync(new Uri("http://www.fake.com/" + id)); } } The above is a simplified version of this: (I have the author's permission to post the image.) What bothers me about that code is that an event handler is attached, DownloadStringAsync()

Do I need to use multiple using statements?

╄→гoц情女王★ 提交于 2019-12-20 03:32:28
问题 Both classes for practicality sake are disposable. I understand what a using block does. But I'm not sure of all of the ways it can or needs to be used. For example is this correct? using (MyClass myClass = new MyClass(params)) { myClass.name = "Steve"; SecondClass myClassSecond = new SecondClass(params); myClassSecond.name = "George"; myClassSecond.msg = "Hello Man in the Yellow Hat"; } Are both classes above disposed of? Or do I need both inside a using statement? using (MyClass myClass =

What's the value in removing and/or sorting Usings?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-20 02:46:17
问题 I've always run Remove and Sort Usings as a matter of course, because it seems the right thing to do. But just now I got to wondering: Why do we do this? Certainly, there's always a benefit to clean & compact code. And there must be some benefit if MS took the time to have it as a menu item in VS. Can anyone answer: why do this? What are the compile-time or run-time (or other) benefits from removing and/or sorting usings? 回答1: As @craig-w mentions, there's a very small compile time