using-statement

Is C#'s using statement abort-safe?

前提是你 提交于 2019-11-27 21:07:32
I've just finished reading "C# 4.0 in a Nutshell" (O'Reilly) and I think it's a great book for a programmer willing to switch to C#, but it left me wondering. My problem is the definition of using statement. According to the book (p. 138), using (StreamReader reader = File.OpenText("file.txt")) { ... } is precisely equivalent to: StreamReader reader = File.OpenText("file.txt"); try { ... } finally { if (reader != null) ((IDisposable)reader).Dispose(); } Suppose, however, that this is true and that this code is executed in a separate thread. This thread is now aborted with thread.Abort() , so a

C# “Using” Syntax

折月煮酒 提交于 2019-11-27 19:58:38
Does the using catch the exception or throw it? i.e. using (StreamReader rdr = File.OpenText("file.txt")) { //do stuff } If the streamreader throws an exception is it caught by using or thrown so the calling function can handle it? using statements do not eat exceptions. All "Using" does is scope your object to the using block, and automatically calls Dispose() on the object when it leaves the block. There is a gotcha though, if a thread is forcefully aborted by an outside source, it is possible that Dispose will never be called. jop When you see a using statement, think of this code:

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

无人久伴 提交于 2019-11-27 17:45:54
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? No. The compiler will generate a separate finally block for each variable. The spec (§8.13) says: When a resource-acquisition takes the form of a local-variable-declaration, it is possible to

C#: “Using” Statements with HttpWebRequests/HttpWebResponses

北城余情 提交于 2019-11-27 17:19:43
问题 Jon Skeet made a comment (via Twitter) on my SOApiDotNet code (a .NET library for the pre-alpha Stack Overflow API): @maximz2005 One thing I've noticed just from browsing the source quickly: you don't disposed (sic) of WebResponses. "using" statements FTW. He indicates that I need to wrap these Web sessions in "using" statements. However, I have a question about this: should I wrap the whole thing, starting with the HttpWebRequest , or should I create the WebRequest outside of the "using"

using statement with multiple variables [duplicate]

给你一囗甜甜゛ 提交于 2019-11-27 17:05:22
This question already has an answer here: Nested using statements in C# 16 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; } } The accepted way is just to chain the statements: using (var sr = new StringReader(content)) using (var xtr = new XmlTextReader(sr)) { obj = XmlSerializer.Deserialize(xtr) as TModel; } Note that the IDE will also support this indentation, i.e. it

What is the Managed C++ equivalent to the C# using statement

心已入冬 提交于 2019-11-27 15:44:37
问题 How would one code the following C# code in Managed C++ void Foo() { using (SqlConnection con = new SqlConnection("connectionStringGoesHere")) { //do stuff } } Clarificaton: For managed objects. 回答1: Assuming you mean C++/CLI (not the old Managed C++), the following are your options: (1) Mimic a using-Block with using automatic / stackbased objects: { SqlConnection conn(connectionString); } This will call the Destructor of the "conn" Object when the next enclosing block ends. Whether this is

The C# using statement, SQL, and SqlConnection

笑着哭i 提交于 2019-11-27 14:46:33
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 connection opening error? If not, how to implement this with using the using statement a shown above? It's

Why use a using statement with a SqlTransaction?

核能气质少年 提交于 2019-11-27 11:18:49
I've been running into some problems concerning a SqlTransaction I'm using in my code. During my Googling I see many people using a using statement with a SqlTransaction. What is the benefit and/or difference of using this type of statement with a SqlTransaction? using (SqlConnection cn = new SqlConnection()) { using (SqlTransaction tr = cn.BeginTransaction()) { //some code tr.Commit(); } } Currently my code looks like this: SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["T3"]); cn.Open(); SqlTransaction tr = cn.BeginTransaction(); try { //some code tr.Commit(); cn.Close

Does Java have a using statement?

瘦欲@ 提交于 2019-11-27 10:46:15
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,

How to handle WCF exceptions (consolidated list with code)

左心房为你撑大大i 提交于 2019-11-27 10:08:33
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 and rethrow this error Retry N times, then throw Since it's unlikely one person knows all the exceptions