idisposable

Dispose MemoryStream when using with .Net Mail Attachment

这一生的挚爱 提交于 2019-12-23 10:16:12
问题 I am using a MemoryStream to add attachments from binary that is stored in a DB. My problem is that I want to properly dispose of the MemoryStream. This is easily done using a "using" statement, but when I have more than one attachment I don't know how to properly dispose of the multiple MemoryStreams. Is there a good way to iterate over and attach the files, but yet at the same time properly dispose of the MemoryStreams that I am using to attach? When I tried to flush/close prior to using

Will a IDisposable be disposed if the using block returns?

时光总嘲笑我的痴心妄想 提交于 2019-12-23 09:27:01
问题 For example using(var something = GetSomething()) { something.DoSomething(); if(something.IsX()) return true; } return false; 回答1: Yes, absolutely. The Dispose method is called however the using statement is executed, unless it was an abrupt whole-process termination. The most common cases are: A return within the block An exception being thrown (and not caught) within the block Reaching the end of the block naturally Basically a using statement is mostly syntactic sugar for a try / finally

Transaction scope similar functionality

我怕爱的太早我们不能终老 提交于 2019-12-23 09:15:51
问题 I am looking to setup something very similar to transaction scope which creates a version on a service and will delete/commit at the end of scope. Every SQL statement ran inside the transaction scope internally looks at some connection pool / transaction storage to determine if its in the scope and reacts appropriately. The caller doesn't need to pass in the transaction to every call. I am looking for this functionality. Here is a little more about it: https://blogs.msdn.microsoft.com

Passing IDisposable objects through constructor chains

寵の児 提交于 2019-12-23 09:12:35
问题 I've got a small hierarchy of objects that in general gets constructed from data in a Stream , but for some particular subclasses, can be synthesized from a simpler argument list. In chaining the constructors from the subclasses, I'm running into an issue with ensuring the disposal of the synthesized stream that the base class constructor needs. Its not escaped me that the use of IDisposable objects this way is possibly just dirty pool (plz advise?) for reasons I've not considered, but, this

Is there a FxCop rule for local used IDisposable's?

China☆狼群 提交于 2019-12-23 09:07:43
问题 ... if I use an IDisposable in a local variable, but do not call Dispose() or use the using() pattern. public void BadMethod() { var fs = new FileStream("file.txt", FileMode.Create); fs.WriteByte(0x55); // no dispose, no using() } Just like the "Types that own disposable fields should be disposable" rule for fields. EDIT: Replaced MemoryStream by FileStream, because MemoryStream just allocates memory and doesn't use (unmanaged) resources, so someone could discuss about a mandatory Dispose()

Do we need to close a C# BinaryWriter or BinaryReader in a using block?

社会主义新天地 提交于 2019-12-23 07:38:59
问题 Having this code: using (BinaryWriter writer = new BinaryWriter(File.Open(ProjectPath, FileMode.Create))) { //save something here } Do we need to close the BinaryWriter? If not, why? 回答1: So long as it's all wrapped up in a using block then you don't need to explicitly call Close . The using block will ensure that the object is disposed, and the Close and Dispose methods are interchangeable on BinaryWriter . (The Close method just calls Dispose behind the scenes.) 回答2: Putting it in a using

Best practice for reusing SqlConnection

橙三吉。 提交于 2019-12-23 07:15:52
问题 I've come from Java experience and am trying to start with C#. I've read SqlConnection SqlCommand SqlDataReader IDisposable and I can understand that the best practice to connecting to a DB is wrapping SqlConnection , SqlCommand and SqlDataReader in their own using block. But in Java we use to encapsulate the connection into a factory method, create it only once, and reuse it for all queries, even multithreaded ones. Only statements and result sets are created for each query and closed ASAP.

Best practice for reusing SqlConnection

风流意气都作罢 提交于 2019-12-23 07:14:35
问题 I've come from Java experience and am trying to start with C#. I've read SqlConnection SqlCommand SqlDataReader IDisposable and I can understand that the best practice to connecting to a DB is wrapping SqlConnection , SqlCommand and SqlDataReader in their own using block. But in Java we use to encapsulate the connection into a factory method, create it only once, and reuse it for all queries, even multithreaded ones. Only statements and result sets are created for each query and closed ASAP.

Will using work on null?

我是研究僧i 提交于 2019-12-23 07:02:48
问题 Will the following code work if resource doesn't implement IDisposable? T resource = new T(); using (resource as IDisposable) { ... } 回答1: Yes, it will work, check this running test: [TestMethod] public void TestMethod8() { using (new MyClass() as IDisposable) { } } public class MyClass { } It just runs without any issue. If the corresponding class implements IDisposable it will call it, if not it will still work/run :). Update: As others have said, I also wonder what's the use you want to

Will using work on null?

狂风中的少年 提交于 2019-12-23 07:02:35
问题 Will the following code work if resource doesn't implement IDisposable? T resource = new T(); using (resource as IDisposable) { ... } 回答1: Yes, it will work, check this running test: [TestMethod] public void TestMethod8() { using (new MyClass() as IDisposable) { } } public class MyClass { } It just runs without any issue. If the corresponding class implements IDisposable it will call it, if not it will still work/run :). Update: As others have said, I also wonder what's the use you want to