using-statement

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

三世轮回 提交于 2019-11-27 06:56:23
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? 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 return statement, or an exception. As @Noldorin correctly points out, using a using block in code gets compiled

What is the relationship between the using keyword and the IDisposable interface?

强颜欢笑 提交于 2019-11-27 06:15:32
问题 If I am using the using keyword, do I still have to implement IDisposable ? 回答1: If you use the using statement the enclosed type must already implement IDisposable otherwise the compiler will issue an error. So consider IDisposable implementation to be a prerequisite of using. If you want to use the using statement on your custom class, then you must implement IDisposable for it. However this is kind of backward to do because there's no sense to do so for the sake of it. Only if you have

C# - Location of using statements

穿精又带淫゛_ 提交于 2019-11-27 06:05:17
问题 One thing I have noticed a lot of back and forth on is where using statements should be placed in a C# code file- whether its in the outermost scope or inside a namespace. I understand that the location of the using statement affects the scope of the references within that file, but what I don't understand is why, in most cases, someone would ever want their using statements inside their namespace. In almost all cases only one namespace declaration ever exists in a single file so scoping the

Using Statements vs Namespace path? C#

梦想与她 提交于 2019-11-27 05:00:20
I recently stopped using using-statement s 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 other objects with similar names. Is there any performance increase or decrease in this style of programming

When should I use the using Statement? [duplicate]

时光毁灭记忆、已成空白 提交于 2019-11-27 04:34:14
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); If a class implements IDisposable then it will create some unmanaged resources which need to be 'disposed' of when you are finished using them. So you would do something like: SqlConnection awesomeConn = new SqlConnection(connection); // Do some

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

孤者浪人 提交于 2019-11-27 04:01:42
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(IDbConnection connection = DatabaseInstance.CreateConnection()) { connection.Open(); using(IDbTransaction

Do using statements and await keywords play nicely in c#

穿精又带淫゛_ 提交于 2019-11-27 03:47:48
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 the code compiles, will the using statement still work as expected in both the examples below? Example 1

using on SQLDataReader

天大地大妈咪最大 提交于 2019-11-27 02:18:07
问题 I know I asked a related question earlier. I just had another thought. using (SqlConnection conn = new SqlConnection('blah blah')) { using(SqlCommand cmd = new SqlCommand(sqlStatement, conn)) { conn.open(); // *** do I need to put this in using as well? *** SqlDataReader dr = cmd.ExecuteReader() { While(dr.Read()) { //read here } } } } The argument is that: Since the SqlDataReader dr object is NOT A NEW OBJECT LIKE THE connection or command objects, its simply a reference pointing to the cmd

SqlCommand with using statement

ε祈祈猫儿з 提交于 2019-11-27 02:08:06
I saw that in most samples SqlCommand was used like this using (SqlConnection con = new SqlConnection(CNN_STRING)) { using (SqlCommand cmd = new SqlCommand("Select ID,Name From Person", con)) { SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); return ds; } } I know why we are using "using" statement. But SqlCommand doesn't inlcude Close() method, so should we really use it within using statement Because it also implements IDisposable . The purpose of Using statement is that when control will reach end of using it will dispose that object of using block and

Does Java have a using statement?

Deadly 提交于 2019-11-26 23:13:35
Does Java have a using statement that can be used when opening a session in hibernate? In C# it is something like: using (var session = new Session()) { } So the object goes out of scope and closes automatically. Java 7 introduced Automatic Resource Block Management which brings this feature to the Java platform. Prior versions of Java didn't have anything resembling using . As an example, you can use any variable implementing java.lang.AutoCloseable in the following way: try(ClassImplementingAutoCloseable obj = new ClassImplementingAutoCloseable()) { ... } Java's java.io.Closeable interface,