How do I bind a setter property to a delegate?

I would like to provide the delegate with a setter property. How it's done?

class A {
  private int count;
  public int Count {
    get { return count; }
    set { count = value; }
  }
}
A a = new A();   
delegate void ChangeCountDelegate(int x);
ChangeCountDelegate dlg = ... ? // should call a.Count = x

      

+2


a source to share


3 answers


ChangeCountDelegate dlg = (int x) => a.Count = x;

// or
ChangeCountDelegate dlg = x => a.Count = x;

// or 
ChangeCountDelegate dlg = new ChangeCountDelegate(delegate(int x) { a.Count = x; } );

// or 
ChangeCountDelegate dlg = new ChangeCountDelegate(int x => a.Count = x);

      

Or I think it's easy? :)



I'm sure you get the idea.

The third works in .NET 2.0, others need at least 3.5 :)

+7


a source


Try the following:



ChangeCountDelegate dlg = v => a.Count = v;

      

+1


a source


C # does not support Property-Delegates.

You can work with anonymous methods in the way Snake is mentioned if you need to.

+1


a source







All Articles