using-statement

Does “using” also dispose objects created in the constructor? [duplicate]

℡╲_俬逩灬. 提交于 2021-01-29 05:23:59
问题 This question already has answers here : Nesting 'IDisposable's in a single 'using' statement (3 answers) Closed 4 years ago . When creating an element that implements IDisposable , Dispose() is called at the end of the using block also if an exception is thrown, if I'm correct. However, when creating a new element of ClassB within the constructor of a disposable element, will the object of ClassB also be disposed if IDisposable is implemented? using (ClassA a = new ClassA(new ClassB())) { }

Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? [duplicate]

浪尽此生 提交于 2020-03-21 07:17:49
问题 This question already has answers here : Why Dispose is not called even with using-statement? (1 answer) If I return out of a try/finally block in C# does the code in the finally always run? (4 answers) Closed last month . Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? The question naturally follows the discussion from other here. According to the documentation: using (var font1 = new Font("Arial", 10.0f)) { byte charset = font1

Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? [duplicate]

不打扰是莪最后的温柔 提交于 2020-03-21 07:17:18
问题 This question already has answers here : Why Dispose is not called even with using-statement? (1 answer) If I return out of a try/finally block in C# does the code in the finally always run? (4 answers) Closed last month . Are there guarantees in C# that the using statement won`t inherit the try + finally combinations issues? The question naturally follows the discussion from other here. According to the documentation: using (var font1 = new Font("Arial", 10.0f)) { byte charset = font1

Catching exceptions thrown in the constructor of the target object of a Using block

为君一笑 提交于 2020-01-24 04:17:09
问题 using(SomeClass x = new SomeClass("c:/temp/test.txt")) { ... } Inside the using block, all is fine with treating exceptions as normal. But what if the constructor of SomeClass can throw an exception? 回答1: Yes , this will be a problem when the constructor throws an exception. All you can do is wrap the using block within a try/catch block. Here's why you must do it that way. using blocks are just syntactic sugar and compiler replaces each using block with equivalent try/finall block. The only

Catching exceptions thrown in the constructor of the target object of a Using block

て烟熏妆下的殇ゞ 提交于 2020-01-24 04:17:05
问题 using(SomeClass x = new SomeClass("c:/temp/test.txt")) { ... } Inside the using block, all is fine with treating exceptions as normal. But what if the constructor of SomeClass can throw an exception? 回答1: Yes , this will be a problem when the constructor throws an exception. All you can do is wrap the using block within a try/catch block. Here's why you must do it that way. using blocks are just syntactic sugar and compiler replaces each using block with equivalent try/finall block. The only

Is it better to use the [using] statement in C# or the [dispose] method? Does this apply to external (COM) objects?

a 夏天 提交于 2020-01-23 12:29:13
问题 What is better, the using directive, or the dispose directive when finished with an object? using(FileStream fileStream = new FileStream( "logs/myapp.log", FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { using(StreamReader streamReader = new StreamReader(fileStream)) { this.textBoxLogs.Text = streamReader.ReadToEnd(); } } On the other hand, when I'm dealing with System.Net.Mail, I'm told I need to Dispose() of the object to release any stray locks. Is there any consistent guidance?

using(IDisposable obj = new …) in C# to write code blocks in stream (e.g. XML)

亡梦爱人 提交于 2020-01-13 09:07:10
问题 I have started to use classes implementing IDisposable to write blocks in streams, with the using statement. This is helpful to keep a correct nesting and avoid missing or wrongly placed start/end parts. Basically, the constructor writes the start of a block (e.g. opening XML tag), Dispose() the end (e.g. closing XML tag). Example is the UsableXmlElement below (it's for large XMLs, so LINQ to XML or XmlDocument in memory are no options). However, these IDisposable do not implement the

Disposables, Using & Try/Catch Blocks

有些话、适合烂在心里 提交于 2020-01-10 03:52:05
问题 Having a mental block today, need a hand verifying my logic isn't fubar'ed. Traditionally I would do file i/o similar to this: FileStream fs = null; // So it's visible in the finally block try { fs = File.Open("Foo.txt", FileMode.Open); /// Do Stuff } catch(IOException) { /// Handle Stuff } finally { if (fs != null) fs.Close(); } However, this isn't very elegant. Ideally I'd like to use the using block to dispose of the filestream when I'm done, however I am unsure about the synergy between

IDisposable - automated check for using construct

时光总嘲笑我的痴心妄想 提交于 2020-01-04 01:19:26
问题 Does anybody know of a way to automatically find any variable, where the type implements IDisposable but the using construct is not used? ie. a way to check for potentially unreleased unmanaged resources? Also, is it possible to see the number and types of resource held by a running application? 回答1: There's a code analysis rule for this: http://msdn.microsoft.com/en-us/library/ms182289%28VS.100%29.aspx This can be run from VS 2010 Premium or Ultimate or separately with FxCop: http://www

Am I closing this SQL connection in a C# function appropriately?

烈酒焚心 提交于 2020-01-03 18:37:08
问题 In an attempt to close my question on connections remaining open and exceeding the maximum pool, I'm trying tor rewrite the function that is used to connect to our database. The function exists within a homegrown compiled library. using reflector I can see the code looks like this: public SqlProvider([Optional, DefaultParameterValue("")] string StrConnection) { string str; if (StrConnection == "") { str = ConfigurationSettings.AppSettings["ConStr"]; } else { str = StrConnection; }