I keep wondering what C # can do with the new features of C # 3.0. I was about to throw something there that I thought would be so, but it turned out to be better than I had hoped. Here.
( " "... - ). , #. , . "SynchronicityHandler" (sucky term, ).
SynchronicityHandler . . , :
public class PersonValueHolder
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool HasCollegeDegree { get; set; }
}
public class SyncronicityHandler<T>
{
private object locker = new object();
private T valueHolder;
public SynchronicityHandler(T theValueHolder)
{
this.valueHolder = theValueHolder;
}
public void WorkWithValueHolderSafely(Action<T> yourAction)
{
lock(locker)
{
yourAction(valueHolder);
}
}
}
, :
var myPerson = new SynchronicityHandler(new PersonValueHolder());
myPerson.WorkWithValueHolderSafely( p =>
{
p.FirstName = "Douglas";
p.LastName = "Adams";
p.HasCollegeDegree = true;
});
string theFirstName = null;
myPerson.WorkWithValueHolderSafely( p=> theFirstName = p.FirstName);
Console.Writeline("Name is: " + theFirstName);
, , , " " . .