using-statement

Calling Dispose() vs when an object goes out scope/method finishes

情到浓时终转凉″ 提交于 2019-11-29 08:10:43
问题 I have a method, which has a try/catch/finaly block inside. Within the try block, I declare SqlDataReader as follows: SqlDataReader aReader = null; aReader = aCommand.ExecuteReader(); In the finally block, the objects which are manually disposed of are those which are set at the class level. So objects in the method which implement IDisposable , such as SqlDataReader above, do they get automatically disposed of? Close() is called on aReader after a while loop executes to get the contents of

Is there a list of common object that implement IDisposable for the using statement?

六眼飞鱼酱① 提交于 2019-11-29 05:43:22
I was wondering if there was some sort of cheat sheet for which objects go well with the using statement... SQLConnection , MemoryStream , etc. Taking it one step further, it would be great to even show the other "pieces of the puzzle", like how you should actually call connection.Close() before the closing using statement bracket. Anything like that exist? If not, maybe we should make one. Microsoft FxCop has a rule checking that you use an IDisposbale in a using block. Perhaps glance at my post on this at http://www.lancemay.com/2010/01/idisposable-cheat-sheet/ . Not sure if that's what you

Does “using” statement always dispose the object?

那年仲夏 提交于 2019-11-29 05:29:27
Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.: using (var myClassInstance = new MyClass()) { // ... return; } or using (var myClassInstance = new MyClass()) { // ... throw new UnexplainedAndAnnoyingException(); } Yes, that's the whole point. It compiles down to: SomeDisposableType obj = new SomeDisposableType(); try { // use obj } finally { if (obj != null) ((IDisposable)obj).Dispose(); } Be careful about your terminology here; the object itself is not deallocated. The Dispose() method is called and, typically, unmanaged

C#: “Using” Statements with HttpWebRequests/HttpWebResponses

限于喜欢 提交于 2019-11-29 03:05:32
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" statement and then wrap the Response inside ? I have a feeling that the difference is that, in the former,

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

风格不统一 提交于 2019-11-29 01:13:45
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. 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 enclosing function, or a block you manually add to limit scope doesn't matter. (2) Explicitly call

C# - Location of using statements

梦想的初衷 提交于 2019-11-28 11:10:14
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 seems/(is?) useless. If one were placing multiple types and multiple namespaces in the

Why is a variable declared in a using statement treated as readonly?

爷,独闯天下 提交于 2019-11-28 07:59:20
问题 Why using variable treated as readonly? It is c# language specification or managed languages specification? It is because c# is a .net language? Thanks in advance. Note : using variable is variable that appears in using statement sample code: using (Form s = new Form) { myfunc(ref s); } we cant change value of a using variable in using block. the code will raise an error. Note : i dont want you discuss about readonly keyword. 回答1: I'm looking at an (outdated?) spec [1] right now. 15.13 says

Does “using” statement always dispose the object?

孤街醉人 提交于 2019-11-27 23:03:54
问题 Does the using statement always dispose the object, even if there is a return or an exception is thrown inside it? I.E.: using (var myClassInstance = new MyClass()) { // ... return; } or using (var myClassInstance = new MyClass()) { // ... throw new UnexplainedAndAnnoyingException(); } 回答1: Yes, that's the whole point. It compiles down to: SomeDisposableType obj = new SomeDisposableType(); try { // use obj } finally { if (obj != null) ((IDisposable)obj).Dispose(); } Be careful about your

what does a using statement without variable do when disposing?

不问归期 提交于 2019-11-27 23:03:43
问题 I've always used using with variable and assignment. Now i have like this a class DbProviderConnection: public class DbProviderConnection : IDisposable { public DbConnection Connection { get; set; } public DbTransaction Transaction { get; set; } public DbTransaction BeginTransaction() { Transaction = Connection.BeginTransaction(); return Transaction; } //... and so on } Now i was thinkin to use it like this: using (DbProviderConnection cnctn = _planDb.CreateOpenConnection()) { using (cnctn

MemoryStream in Using Statement - Do I need to call close()

坚强是说给别人听的谎言 提交于 2019-11-27 22:55:48
问题 When using a memory stream in a using statement do I need to call close? For instance is ms.Close() needed here? using (MemoryStream ms = new MemoryStream(byteArray)) { // stuff ms.Close(); } 回答1: No, it's not. using ensures that Dispose() will be called, which in turn calls the Close() method. You can assume that all kinds of Streams are getting closed by the using statement. From MSDN: When you use an object that accesses unmanaged resources, such as a StreamWriter, a good practice is to