I've got a third-party component that does PDF file manipulation. Whenever I need to perform operations I retrieve the PDF documents from a document store (database, SharePoint, filesystem, etc.). To make things a little consistent I pass the PDF documents around as a byte[].
This 3rd party component expects a MemoryStream[] (MemorySt...
Start with these simple classes...
Let's say I have a simple set of classes like this:
class Bus
{
Driver busDriver = new Driver();
}
class Driver
{
Shoe[] shoes = { new Shoe(), new Shoe() };
}
class Shoe
{
Shoelace lace = new Shoelace();
}
class Shoelace
{
bool tied = false;
}
A Bus has a Driver, the Driver has t...
Something like:
using (IDisposable disposable = GetSomeDisposable())
{
//.....
//......
return Stg();
}
I believe it is not a proper place for a return statement, is it?
...
When implementing IDisposable correctly, most implementations, including the framework guidelines, suggest including a private bool disposed; member in order to safely allow multiple calls to Dispose(), Dispose(bool) as well as to throw ObjectDisposedException when appropriate.
This works fine for a single class. However, when you subc...
For a user control with internal data structures that must be disposed, is the correct place to add that code to the Dispose method in the .designer.cs file, or is there an event or something we're meant to use instead?
Edit: This is a winforms user control.
...
If I have a SomeDisposableObject class which implements IDisposable:
class SomeDisposableObject : IDisposable
{
public void Dispose()
{
// Do some important disposal work.
}
}
And I have another class called AContainer, which has an instance of SomeDisposableObject as a public property:
class AContainer
{
Som...
Dupe of http://stackoverflow.com/questions/662773/returning-in-the-middle-of-a-using-block
Question title is fairly obvious I think, so given the following code is the SecurityDisabler disposed if true is returned?
public bool CreateProxyItem(string name, Sitecore.Data.ID sourceID, Sitecore.Data.ID targetID)
{
// create an instance...
What do you think of the following IDisposable pattern implementation?
public class Connection : IDisposable
{
private Socket _socket;
public bool IsConnected()
{
if (_socket.Poll(1, SelectMode.SelectRead) && _socket.Available == 0)
return false;
return true;
}
public void Disconnect()
...
Will the following code work if resource doesn't implement IDisposable?
T resource = new T();
using (resource as IDisposable)
{
...
}
...
I am trying to dispose XmlWriter object:
try
{
[System.Xml.XmlWriter] $writer = [System.Xml.XmlWriter]::Create('c:\some.xml')
}
finally
{
$writer.Dispose()
}
Error:
Method invocation failed because
[System.Xml.XmlWellFormedWriter]
doesn't contain a method named
'Dispose'.
On the other side:
$writer -is [IDisposab...
Recently I needed to compare a suggested pattern for IDisposable and object finalization with the auto-generated one we which VS2005/VB.NET provide. We have used the auto-generated one a fair bit, but after looking it the two side by side I had a number of questions about the VB.NET implementation...
For reference, here is the IDE's im...
I remember i was loading in images by streaming it from the net straight into a bitmap. close the stream, return the bitmap and held it in an image control.
I excepted when i did = loadPicture() the first bitmap would be freed like a smart pointer would do in C++. But it didnt and i consumed a lot of ram until i called dispose. So my qu...
I have an IEnumerable<IDisposable> collection that I need to dispose of at a given point. The issue is that for some cases all the object will be of one type and disposing of the objects needs to be done by collecting some data from them and making a single RPC call. In other cases, the objects will be of another type and I just need to ...
I am trying to create a general method for disposing an object that implements IDisposable, called DisposeObject()
To make sure I am disposing an object pointed by original reference, I am trying to pass an object by reference.
But I am getting a compilation error that says
The 'ref' argument type doesn't match parameter type
In ...
For example,
int myResult= (new UnmanagedResourceUsingMemorySuckingPig()).GetThingsDone(id);
There is no using block, no obvious way to use a using block, no obvious way to call Dispose(). And of course UnmanagedResourceUsingMemorySuckingPig does implement IDisposable.
...
I am wanting to create an internal messaging system that can tell me the duration of some code being called. I was thinking for ease of use, to make the SystemMessage class implement IDisposable.
I would set a time stamp during the SystemMessage's constructor and if the Dispose was called, I could figure out the duration.
The prob...
I have several methods that deal with DB and all of them start by calling
FaierDbDataContext db = new FaierDbDataContext();
Since the Linq2Sql DataContext object implements IDisposable, should this be used with "using"?
using (FaierDbDataContext db = new FaierDbDataContext()) {
// use db here
}
What are the implications of usin...
I can never remember all the rules for implementing the IDisposable interface, so I tried to come up with a base class that takes care of all of this and makes IDisposable easy to implement. I just wanted to hear your opinion if this implementation is ok as it is or whether you see something I could improve. The user of this base class i...
using (var file_stream = File.Create("users.xml"))
{
var serializer = new XmlSerializer(typeof(PasswordManager));
serializer.Serialize(file_stream, this);
file_stream.Close();
}
Using the above code works perfectly. However, when I shorten it to:
var serializer = new...
I have a class that implements the IDisposable interface. I am using a webclient to download some data using the AsyncDownloadString.
I am wondering have I correctly declared my event handlers in the constructor and within the using statement of the web client? And is this correct way to remove the event handlers in the Dispose method?...