using-statement

Can “using” with more than one resource cause a resource leak?

点点圈 提交于 2019-11-26 22:36:56
问题 C# lets me do the following (example from MSDN): using (Font font3 = new Font("Arial", 10.0f), font4 = new Font("Arial", 10.0f)) { // Use font3 and font4. } What happens if font4 = new Font throws? From what I understand font3 will leak resources and won't be disposed of. Is this true? (font4 won't be disposed of) Does this mean using(... , ...) should be avoided altogether in favor of nested using? 回答1: No. The compiler will generate a separate finally block for each variable. The spec (§8

using statement with multiple variables [duplicate]

这一生的挚爱 提交于 2019-11-26 22:30:52
问题 This question already has an answer here: Nested using statements in C# 17 answers Is it possible to make this code a little more compact by somehow declaring the 2 variable inside the same using block? using (var sr = new StringReader(content)) { using (var xtr = new XmlTextReader(sr)) { obj = XmlSerializer.Deserialize(xtr) as TModel; } } 回答1: The accepted way is just to chain the statements: using (var sr = new StringReader(content)) using (var xtr = new XmlTextReader(sr)) { obj =

returning in the middle of a using block

旧街凉风 提交于 2019-11-26 20:10:29
Something like: using (IDisposable disposable = GetSomeDisposable()) { //..... //...... return Stg(); } I believe it is not a proper place for a return statement, is it? As several others have pointed out in general this is not a problem. The only case it will cause you issues is if you return in the middle of a using statement and additionally return the in using variable. But then again, this would also cause you issues even if you didn't return and simply kept a reference to a variable. using ( var x = new Something() ) { // not a good idea return x; } Just as bad Something y; using ( var x

The C# using statement, SQL, and SqlConnection

点点圈 提交于 2019-11-26 18:26:36
问题 Is this possible using a using statement C# SQL? private static void CreateCommand(string queryString, string connectionString) { using (SqlConnection connection = new SqlConnection( connectionString)) { SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); } } What if there’s a error while opening the connection? The using statement is try and finally No catch So if I catch outside the using brackets will the catch catch the

How to handle WCF exceptions (consolidated list with code)

我的未来我决定 提交于 2019-11-26 15:06:28
问题 I'm attempting to extend this answer on SO to make a WCF client retry on transient network failures and handle other situations that require a retry such as authentication expiration. Question: What are the WCF exceptions that need to be handled, and what is the correct way to handle them? Here are a few sample techniques that I'm hoping to see instead of or in addition to proxy.abort() : Delay X seconds prior to retry Close and recreate a New() WCF client. Dispose the old one. Don't retry

What happens if i return before the end of using statement? Will the dispose be called?

谁说胖子不能爱 提交于 2019-11-26 12:57:24
问题 I\'ve the following code using(MemoryStream ms = new MemoryStream()) { //code return 0; } The dispose() method is called at the end of using statement braces } right? Since I return before the end of the using statement, will the MemoryStream object be disposed properly? What happens here? 回答1: Yes, Dispose will be called. It's called as soon as the execution leaves the scope of the using block, regardless of what means it took to leave the block, be it the end of execution of the block, a

Using Statements vs Namespace path? C#

本秂侑毒 提交于 2019-11-26 12:47:05
问题 I recently stopped using using-statements and instead use the full namespace path of any .net object that I call. Example: using System; namespace QuizViewer { class Class1 { Console.WriteLine(\"Hello World!\"); } } This is what I do now. namespace QuizViewer { class Class1 { System.Console.WriteLine(\"Hello World!\"); } } Before you ask why I do this, I am using this style so that I can see exactly where my objects are coming from and it\'s easier when using the different Timer objects and

When should I use the using Statement? [duplicate]

旧街凉风 提交于 2019-11-26 12:45:01
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: What is the C# Using block and why should I use it? I\'m converting an old site to C# and I\'m not sure when I should be using \'using\'. Are there any general guidelines? I know of the benefits, but I\'m not 100% sure how to use it. Is it every time I \'new\' a method/property? SqlConnection awesomeConn = new SqlConnection(connection); 回答1: If a class implements IDisposable then it will create some unmanaged

Will a using statement rollback a database transaction if an error occurs?

亡梦爱人 提交于 2019-11-26 12:41:43
问题 I\'ve got an IDbTransaction in a using statement but I\'m unsure if it will be rolled back if an exception is thrown in a using statement. I know that a using statement will enforce the calling of Dispose()...but does anyone know if the same is true for Rollback()? Update: Also, do I need to call Commit() explicitly as I have below or will that also be taken care of by the using statement? My code looks sort of like this: using Microsoft.Practices.EnterpriseLibrary.Data; ... using

Do using statements and await keywords play nicely in c#

北城以北 提交于 2019-11-26 10:33:20
问题 I have a situation where I am making an async call to a method that returns and IDisposable instance. For example: HttpResponseMessage response = await httpClient.GetAsync(new Uri(\"http://www.google.com\")); Now before async was on the scene, when working with an IDisposable instance, this call and code that used the \"response\" variable would be wrapped in a using statement. My question is whether that is still the correct approach when the async keyword is thrown in the mix? Even though