C# Using block usage with static objects - c#

I'm adding some code into a using block in a C# program. I'm sort of stuffing my app, which previously was a standalone into an existing body of code, so I need to do a bit of messing around to get it to fit properly. What it's ending up looking like is the following:
public class WrapperForMyOldApp
{
public static ItemThatINeed item1;
public static ItemThatINeed item2;
public WrapperForMyOldApp ()
{
item1 = new ItemThatINeed();
item2 = new ItemThatINeed();
}
public static go()
{
// some stuff that i need to do with items 1 and 2
}
}
public class MainProgram
{
.
.
.
public void MethodThatNeedsToMakeUseOfMyApp ()
{
....
using (WrapperForMyOldApp oldAPp = new WrapperForMyOldApp())
{
WrapperForMyOldApp.go();
}
}
}
Alright, so the question here is: Have I now crippled the effects of the using block and/or created any peculiar side effects that might adversely effect the class MainProgram? I believe that the Wrapper object and it's contents will be Disposed and execution will continue as expected but is there anything I need to be aware of that I'm overlooking?
thanks!

Does your wrapper class implement IDisposable and you're just not showing it? If it's not disposable, then you don't need the using statement at all.

In order for this to work, you'll need to have WrapperForMyOldApp implement IDisposable.
The Dispose() call in WrapperForMyOldApp would then do your cleanup.
However, static objects are typically used for objects that have a lifetime beyond the single object. In general, for this type of usage, you'd make the ItemThatINeed instances non-static, generate them in your WrapperForMyOldApp constructor, then clean them up in WrapperForMyOldApp.Dispose().
With static objects, you're potentially creating a nightmare - you're constructing the object, and then saying you want to perform the cleanup (at the end of the using block), so you Dispose() method would be cleaning up the static objects. However, if they get used again, what should happen? What is the correct behavior if you create two WrapperForMyOldApp instances in 2 threads? I would consider these issues if you want deterministic cleanup.

Well, if WrapperForMyOldApp implements IDisposable, and your Dispose() implementation can be sure to get rid of any resources, then it should work... but there can be other side effects. The code could change global (static) state, such as culture etc. It could spawn threads. All sorts of things.
It isn't a bad approach, but you need to know what the code you are encapsulating does to know whether Dispose() is going to do anything useful.

Related

How can I do an action when a variable goes out of scope?

EDIT: So it looks like the only way to do this is with IDisposable. I suspected as much but it's still a bit of a bummer. I may make a class for this that takes an Action as #mjwills suggests but I still personally wish there was a way to do this without the using block. Alternative syntax like "using (var foo = new Bar([action]);" that makes the using statement apply to the block it's called from would be great.
In C# is it possible to perform an action when a variable goes out of scope? In C++ I would use an object's destructor to do something when it goes out of scope, but the garbage collector means I can't (cleanly) get the same behavior in C#. This is what I would write in C++:
class SetOnDestruction
{
bool& m_thingToSet;
bool m_newValueForThing;
public:
SetOnDestruction(bool& thingToSet, bool newValueForThing)
: m_thingToSet(thingToSet)
, m_newValueForThing(newValueForThing)
{
}
~SetOnDestruction()
{
m_thingToSet = m_newValueForThing;
}
};
void ExampleUsage()
{
bool thingToSet = true;
{
SetOnDestruction setter(thingToSet, false);
// do some stuff here
}
// thingToSet should now be false
}
I don't want to use IDisposable because then I'm wrapping my functions in big using blocks for what is really just a bit of syntactic sugar.
Does anyone know of anything relevant?
If you're trying to destroy unmanaged resources we can in fact use a destructor such as
~SetOnDestruction()
{
/* destruction things */
}
However, this gets called when the garbage collector destroys the object and thus is non-deterministic.
The only other way is using IDisposable
More info on IDisposable from MSDN here
Edit: In honor of the down votes (And pure interest) Let's see not if we should do this but if we can do this
We could hypothetically enforce local scope by calling something like this
class Program
{
static void Main(string[] args)
{
LocalScopeWrapper();
}
private static void LocalScopeWrapper() // maybe I could have a better name?
{
var testObj = new TestObj();
testObj.Print();
Console.WriteLine($"Total memory used before forced collect {GC.GetTotalMemory(false)}");
testObj = null;
GC.Collect(0); // Generation 0, lets only clear the smallest Small Object Heap
Console.WriteLine($"Total memory used after forced collect {GC.GetTotalMemory(true)}");
}
private class TestObj
{
public void Print()
{
Console.WriteLine("HelloWorld");
}
}
}
Here we're forcing the object to be collected after we set it to null. Makes sense, but not exactly what we want, and as Dire Whale asked for not clean.
But, I wanted to see if we emulate C++ behavior, and it is possible.
We could use Pinvoke and handles to pass our object to C/C++ libraries and call free as shown in this great answer by #xanatos
I was curious how bad we could get in C#, and this seemed like the ultimate of getting around the GC by letting a Pinvoke delete things under our nose. It isn't useful but interesting none the less.

Is a struct to perform cleanup at the end of scope a good C# pattern?

RAII is nice for ensuring you don't fail to call cleanup. Normally, I'd implement with a class. I'm currently using Unity and am conscious of generating garbage in Update (even in editor scripting). I was thinking, is creating a struct wrapper for Begin/End actions a good pattern for avoiding allocation? (Since value types don't allocate on the heap.)
Here's an editor scripting example:
public struct ActionOnDispose : IDisposable
{
Action m_OnDispose;
public ActionOnDispose(Action on_dispose)
{
m_OnDispose = on_dispose;
}
public void Dispose()
{
m_OnDispose();
}
}
EditorGUILayout is a Unity type that exposes several functions that need to be called before and after your code. I'd use ActionOnDispose like this:
public static ActionOnDispose ScrollViewScope(ref Vector2 scroll)
{
scroll = EditorGUILayout.BeginScrollView(scroll);
return new ActionOnDispose(EditorGUILayout.EndScrollView);
}
private Vector2 scroll;
public void OnGUI() // called every frame
{
using (EditorHelper.ScrollViewScope(ref scroll))
{
for (int i = 0; i < 1000; ++i)
{
GUILayout.Label("Item #"+ i);
}
}
}
The above simple example works as expected and Dispose is called once for each call to ScrollViewScope, so it appears correct. Unity even provides a scoped struct: EditorGUI.DisabledScope, but not in many cases. So I wonder if there's a downside to this pattern with structs? (I'd assume if the struct is somehow copied the old copy would be disposed and my end action called twice? I don't see such a scenario, but I'm not very familiar with C# value types.)
Edit: I'm specifically asking if whether it matters that I'm using a struct (a value type). Is abusing IDisposable to benefit from “using” statements considered harmful covers whether this is a good use of IDisposable.
Jeroen Mostert's comments as an answer:
Assigning does not generate the garbage, writing new ActionOnDispose(EditorGUILayout.EndScrollView) does. (Yes, this too allocates a new Action under water, it's just syntactic shorthand.) In general, it's very hard to avoid allocation if you're using delegates, but it usually doesn't pay off to do so either. Short-lived objects are collected in gen 0, which is very efficient. My uneducated guess would be that methods like EditorGUILayout.BeginScrollView are going to allocate stuff themselves, to the point where one more allocation on your end won't matter much.
To answer your other question, no, copying the struct would not cause the method to be called twice. The only way to do that is to actually have it wrapped in two different Dispose scopes. C# does not have RAII; things going out of scope does nothing. It's the using statement that does the actual magic.
My conclusion:
I could make versions of ActionOnDispose for each of my use cases to avoid use of garbage-generating Action.

Using 'Using' for things other than resource disposal [duplicate]

This question already has answers here:
Is it abusive to use IDisposable and "using" as a means for getting "scoped behavior" for exception safety?
(12 answers)
Closed 9 years ago.
We all know that the using statement is really good for resources you want to cleanup in a timely manner, such as an open file or database connection.
I was wondering if it would be considered a good thing to use the statement in cases where resources cleanup isn't the goal of the Dispose() method, but rather resetting to a previous state.
For example, a class that allows a using statement to wrap a procedure that takes a noticeable amount of time and changes the Cursor to a waiting state.
class CursorHelper : IDisposable
{
readonly Cursor _previousState;
public CursorHelper(Cursor newState)
{
_previousState = Cursor.Current;
Cursor.Current = newState;
}
public void Dispose()
{
Cursor.Current = _previousState;
}
}
Then the class can be used as such, without worrying about reverting the Cursor when your are done.
public void TimeIntensiveMethod()
{
using (CursorHelper ch = new CursorHelper(Cursors.WaitCursor))
{
// something that takes a long time to complete
}
}
Is this an appropriate use of the using statement?
There are certainly precedents for (ab)using the using statement in this way, for example FormExtensions.BeginForm in the ASP.NET MVC framework. This renders a <form> closing tag when it's disposed, and it's main purpose is to enable a more terse syntax in MVC views. The Dispose method attempts to render the closing tag even if an exception is thrown, which is slightly odd: if an exception is thrown while rendering a form, you probably don't want to attempt to render the end tag.
Another example is the (now deprecated) NDC.Push method in the log4net framework, which returns an IDisposable whose purpose is to pop the context.
Some purists would say it's an abuse, I suggest you form your own judgement on a case-by-case basis.
Personally I don't see anything wrong with your example for rendering an hourglass cursor.
The discussion linked in a comment by #I4V has some interesting opinions - including arguments against this type of "abuse" from the ubiquitous Jon Skeet.
In reality using is simply syntactical sugar for try/finally, so why don't you just do plain old try/finally like below...
try
{
// do some work
}
finally
{
// reset to some previous state
}
imho implementing the Dispose method to reset to some state would be very misleading, especially if you have consumers for your code.
I am opposed to this and believe it to be an abuse. I also think that RAII in C++ is a terrible idea. I am aware that I am in a minority on both positions.
This question is a duplicate. For detailed reasons on why I think this is an unwarranted abuse of the using statement, see: https://stackoverflow.com/a/2103158/88656
No, it isn't appropriate use of using and/or Dispose. The pattern has a very clear use ("Defines a method to release allocated resources."), and this isn't it. Any future developers using this code would look upon it with the contempt warranted for such evil.
If you want this kind of behaviour then implement events and expose them, calling code can subscribe to them and manage the cursor, if needs be, otherwise the cursor should be manageable by general parameters and maybe using Begin and End methods (although such naming conventions are generally reserved for asynchronous implementations of methods, you get the picture) - hacking this way doesn't actually buy you anything.
I my opinion it makes sense to use using-Disposable thing in a way other than just disposing objects. Of course it depends on the context and usage. If leads to a more readable code then it is ok.
I have had used it in a unit of work & repository pattern implementation like:
public class UnitOfWork: IDisposable {
// this is thread-safe in actual implementation
private static Stack<UnitOfWork> _uowStack = new Stack<UnitOfWork>();
public static UnitOfWork Current {get { return _uowStack.Peek(); }}
public UnitOfWork() {
_uowStack.Push(this);
}
public void Dispose() {
_ouwStack.Pop();
}
public void SaveChanges() {
// do some db operations
}
}
public class Repository {
public void DoSomething(Entity entity) {
// Do Some db operations
UnitOfWork.Current.SaveChanges();
}
}
With this implementation it is guaranteed that nested operations will use their corresponding UnitOfWork without passing parameter. Usage is like.
using (new UnitOfWork())
{
var repo1 = new UserRepository();
// Do some user operation
using (new UnitOfWork())
{
var repo2 = new AccountingRepository();
// do some accounting
}
var repo3 = new CrmRepository();
// do some crm operations
}
In this sample repo1 and repo3 use the same UnitOfWork while repo2 uses a different repository. What reader reads is "using new unit of work" and it makes a lot of sense.

Why not static destructors in C#

I am interested in knowing why static destructors are not allowed in C#. Note that I am not supposing that they exist in any other language.
I could have a class like this one.
class A
{
static A()
{
// here I can load a resource that will be avaliable for all instances
// of this class.
}
}
When the application ends I may need to release the resource.
So, the semantic of a static destructor could be the following: called when the application ends, for classes that contain it and were initialized in the app.
Your semantic is one possible one, but I guess you have not checked all effects it would have on the language. In most (more or less) dynamic languages I know, destruction is a much more complicated topic that it looks like. Why not call the destructor when the class is not referenced anymore? Or if the assembly is unloaded? In what order should destructor be called? ...?
If you just want to execute some code when the application ends, have a look at the .Net documentation. There are easier and more reliable ways to do so.
So, the semantic of a static destructor could be the following: - be
called when the application ends, on class that contains it and was
charged in the app.
Your semantic relies on your program doing an specific action at the end of execution and this is far more difficult to correctly handle than just a piece of code that runs at the end of normal execution of the process.
Think about transactions or file management operations. You have to manually handle crashes and unexpected termination of the process and try recovering at next run anyway, so then having an static destructor wouldn't help that much. .NET managed world favors upon using patterns instead of that. Now, if you're having serious problems with this, try to attach an event handler to the DomainUnloaded event on the AppDomain and perform your cleanup there.
You can, also, give a try to the singleton dispose way:
class A : IDisposable
{
public static A Instance {get; private set;}
public static A()
{
Instance=new A();
}
public void MethodA() {...}
public void Dispose()
{
//...
}
~A()
{
// Release your hard resources here
}
}
And then, use the safe method:
A.Instance.MethodA();

Is there any benefit to implementing IDisposable on classes which do not have resources?

In C#, if a class, such as a manager class, does not have resources, is there any benefit to having it : IDisposable?
Simple example:
public interface IBoxManager
{
int addBox(Box b);
}
public class BoxManager : IBoxManager
{
public int addBox(Box b)
{
using(dataContext db = new dataContext()){
db.Boxes.add(b);
db.SaveChanges();
}
return b.id;
}
}
Will there be any benefit in memory use when using BoxManager if it also implements IDisposable? public class BoxManager : IBoxManager , IDisposable
For example:
BoxManager bm = new BoxManager();
bm.add(myBox);
bm.dispose();//is there benefit to doing this?
There are only 2 reasons for implementing IDisposable on a type
The type contains native resources which must be freed when the type is no longer used
The type contains fields of type IDisposable
If neither of these are true then don't implement IDisposable
EDIT
Several people have mentioned that IDisposable is a nice way to implement begin / end or bookended operations. While that's not the original intent of IDisposable it does provide for a very nice pattern.
class Operation {
class Helper : IDisposable {
internal Operation Operation;
public void Dispose() {
Operation.EndOperation();
}
}
public IDisposable BeginOperation() {
...
return new Helper() { Operation = this };
}
private void EndOperation() {
...
}
}
Note: Another interesting way to implement this pattern is with lambdas. Instead of giving an IDisposable back to the user and hoping they don't forget to call Dispose have them give you a lambda in which they can execute the operation and you close out the operation
public void BeginOperation(Action action) {
BeginOperationCore();
try {
action();
} finally {
EndOperation();
}
}
There won't be a scrap of difference between the disposable and non-disposable version if you don't explicitly make use of the Dispose() method.
While your code wouldn't benefit from implementing IDisposable, I can't agree with other opinions here that state that IDisposable is only meant to (directly or indirectly) free native resources. IDisposable can be used whenever the object needs to perform clean up task at the end of it's lifetime span. It's extremely useful together with using.
A very popular example: in ASP.NET MVC Html.BeginForm returns an IDisposable. On creation, the object opens the tag, when Dispose is called it closes it. No native resources involved, yet still a good use of IDisposable.
No, there will be no benefit if you don't do something useful like releasing unmanaged resources that your class might hold in the Dispose method.
One major point of confusion, which may not be applicable in your case but arises often, is what exactly constitutes a "resource". From the perspective of an object, an unmanaged resource is something which an outside entity () is "doing"(*) on its behalf, which that outside entity will keep doing--to the detriment of other entitites--until told to stop. For example, if an object opens a file, the machine which hosts the file may grant that object exclusive access, denying everyone else in the universe a chance to use it unless or until it gets notified that the exclusive access isn't needed anymore.
(*) which could be anything, anywhere; possibly not even on the same computer.
(**) or some way in which the the behavior or state of an outside entity is altered
If an outside entity is doing something on behalf of an object which is abandoned and disappears without first letting the entity know its services are no longer required, the outside entity will have no way of knowing that it should stop acting on behalf of the object which no longer exists. IDisposable provides one way of avoiding this problem by providing a standard means of notifying objects when their services are not required. An object whose services are no longer required will generally not need to ask any further favors from any other entities, and will thus be able to request that any entities that had been acting on its behalf should stop doing so.
To allow for the case where an object gets abandoned without IDisposable.Dispose() having been called first, the system allows objects to register a "failsafe" cleanup method called Finalize(). Because for whatever reason, the creators of C# don't like the term Finalize(), the language requires the use of a construct called a "destructor" which does much the same thing. Note that in general, Finalize() will mask rather than solve problems, and can create problems of its own, so it should be used with extreme caution if at all.
A "managed resource" is typically a name given to an object which implements IDisposable and usually, though not always, implements a finalizer.
No, if there are no (managed or unmanaged) resources there is no need for IDisposable either.
Small caveat: some people use IDisposable to clean up eventhandlers or large memory buffers but
you don't seem to use those
it's a questionable pattern anyway.
From my personal experience (confirmed with discussion and other posts here) I would say, that there could be a situations where your object use massive amount of events, or not massive amount but frequent subscriptions and unsubscription from the event which sometimes leads to that the object is not garbage collected. In this case I in Dispose unsubscribe from all events my object subscribed before.
Hope this helps.
IDisposable is also great if you want to benefit the using () {} syntax.
In a WPF project with ViewModels, I wanted to be able to temporarily disable NotifyPropertyChange events from raising. To be sure other developers will re-enable notifications, I wrote a bit of code to be able to write something like:
using (this.PreventNotifyPropertyChanges()) {
// in this block NotifyPropertyChanged won't be called when changing a property value
}
The syntax looks okay and is easily readable. For it to work, there's a bit of code to write. You will need a simple Disposable object and counter.
public class MyViewModel {
private volatile int notifyPropertylocks = 0; // number of locks
protected void NotifyPropertyChanged(string propertyName) {
if (this.notifyPropertylocks == 0) { // check the counter
this.NotifyPropertyChanged(...);
}
}
protected IDisposable PreventNotifyPropertyChanges() {
return new PropertyChangeLock(this);
}
public class PropertyChangeLock : IDisposable {
MyViewModel vm;
// creating this object will increment the lock counter
public PropertyChangeLock(MyViewModel vm) {
this.vm = vm;
this.vm.notifyPropertylocks += 1;
}
// disposing this object will decrement the lock counter
public void Dispose() {
if (this.vm != null) {
this.vm.notifyPropertylocks -= 1;
this.vm = null;
}
}
}
}
There are no resources to dispose here. I wanted a clean code with a kind of try/finally syntax. The using keyword looks better.
is there any benefit to having it : IDisposable?
It doesn't look so in your specific example, however: there is one good reason to implement IDisposable even if you don’t have any IDisposable fields: your descendants might.
This is one of the big architectural problems of IDisposable highlighted in IDisposable: What your mother never told you about resource deallocation. Basically, unless your class is sealed you need to decide whether your descendants are likely to have IDisposable members. And this isn't something you can realistically predict.
So, if your descendants are likely to have IDisposable members, that might be a good reason to implement IDisposable in the base class.
Short answer would be no. However, you can smartly use the nature of the Dispose() executement at the end of the object lifecycle. One have already gave a nice MVC example (Html.BeginForm)
I would like to point out one important aspect of IDisposable and using() {} statement. At the end of the Using statement Dispose() method is automatically called on the using context object (it must have implemented IDisposable interface, of course).
There one more reason that no one mentioned (though it's debateful if it really worth it):
The convension says that if someone uses a class that implement IDisposable, it must call its Dispose method (either explicitly or via the 'using' statement).
But what happens if V1 of a class (in your public API) didn't need IDisposable, but V2 does? Technically it doesn't break backward compatibility to add an interface to a class, but because of that convension, it is! Because old clients of your code won't call its Dispose method and may cause resources to not get freed.
The (almost) only way to avoid it is to implement IDisposable in any case you suspect that you'll need it in the future, to make sure that your clients always call your Dispose method, that some day may be really needed.
The other (and probably better) way is to implemet the lambda pattern mentioned by JaredPar above in the V2 of the API.

Categories