<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8486822413101972100</id><updated>2019-12-05T17:52:49.605+05:00</updated><category term="performance"/><category term="unknown features"/><category term="garbage collection"/><category term="extension methods"/><category term="finalization"/><category term="memory leaks"/><category term="thread safety"/><category term="LINQ"/><category term="attributes"/><category term="expressions"/><category term="functional programming"/><category term="generics"/><category term="internal"/><category term="unit testing"/><category term="usability"/><category term="visual studio"/><title type='text'>.NET tips, tricks and patterns</title><subtitle type='html'>From well-known to unknown.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>13</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-4915200485981210313</id><published>2009-11-06T09:45:00.001+05:00</published><updated>2009-11-06T09:45:26.799+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="functional programming"/><category scheme="http://www.blogger.com/atom/ns#" term="usability"/><title type='text'>Currying in C#</title><content type='html'>A &lt;a href=&quot;http://blog.alexyakunin.com/2009/09/currying-in-c.html&quot;&gt;link to my post about currying in C#&lt;/a&gt; perfectly fits this blog.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/4915200485981210313/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2009/11/currying-in-c.html#comment-form' title='39 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/4915200485981210313'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/4915200485981210313'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2009/11/currying-in-c.html' title='Currying in C#'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>39</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-5747249071466843072</id><published>2009-08-08T15:40:00.002+06:00</published><updated>2010-11-03T12:14:03.450+05:00</updated><title type='text'>Making IDisposable more usable, part 1</title><content type='html'>&lt;b&gt;IDisposable&lt;/b&gt; is used quite frequently, so here I’m going to provide few hacks allowing to improve its usability. First of all, let&#39;s create &lt;b&gt;IDisposableExtensions&lt;/b&gt; with a single method:&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Safely disposes an &amp;lt;see cref=&quot;IDisposable&quot;/&amp;gt; object.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;disposable&quot;&amp;gt;Object to dispose (can be &amp;lt;see langword=&quot;null&quot;/&amp;gt;).&amp;lt;/param&amp;gt;&lt;br /&gt;public static void DisposeSafely(this IDisposable disposable)&lt;br /&gt;{&lt;br /&gt;if (disposable!=null)&lt;br /&gt;disposable.Dispose();&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;This gives us an ability to dispose the objects without repeating null-check:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;try {&lt;br /&gt;writer.DisposeSafely();&lt;br /&gt;}&lt;br /&gt;finally {&lt;br /&gt;writer = null;&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Let&#39;s go further now. Frequently you must &lt;b&gt;safely&lt;/b&gt; dispose two or more &lt;b&gt;IDisposable&lt;/b&gt; objects. Note that &quot;safely&quot; is an essential word here. You can&#39;t write something like:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;disposable1.DisposeSafely();&lt;br /&gt;disposable2.DisposeSafely();&lt;/pre&gt;&lt;br /&gt;This code may fail: an exception can be thrown on execution of its first line, and if this happens, its second line won&#39;t be executed at all. So &lt;b&gt;disposable2&lt;/b&gt; has a chance of not being disposed. That&#39;s why it&#39;s a good idea to implement few helpers allowing us to deal with such issues safely.&lt;br /&gt;&lt;br /&gt;Let&#39;s add &lt;b&gt;JoiningDisposable&lt;/b&gt; type:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Disposes two &amp;lt;see cref=&quot;IDisposable&quot;/&amp;gt; objects.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;[Serializable]&lt;br /&gt;public sealed class JoiningDisposable : IDisposable&lt;br /&gt;{&lt;br /&gt;private IDisposable first;&lt;br /&gt;private IDisposable second;&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Gets the first object to dispose.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public IDisposable First {&lt;br /&gt;get { return first; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Gets the second object to dispose.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public IDisposable Second {&lt;br /&gt;get { return second; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Joins the &amp;lt;see cref=&quot;JoiningDisposable&quot;/&amp;gt; and &amp;lt;see cref=&quot;IDisposable&quot;/&amp;gt;.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;first&quot;&amp;gt;The first disposable to join.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;second&quot;&amp;gt;The second disposable to join.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;New &amp;lt;see cref=&quot;JoiningDisposable&quot;/&amp;gt; that will&lt;br /&gt;/// dispose both of them on its disposal&amp;lt;/returns&amp;gt;&lt;br /&gt;public static JoiningDisposable operator &amp;amp;(JoiningDisposable first, IDisposable second)&lt;br /&gt;{&lt;br /&gt;if (second==null)&lt;br /&gt;return first;&lt;br /&gt;return new JoiningDisposable(first, second);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Constructors&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;///   &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot;/&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;disposable1&quot;&amp;gt;The first disposable.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;disposable2&quot;&amp;gt;The second disposable.&amp;lt;/param&amp;gt;&lt;br /&gt;public JoiningDisposable(IDisposable disposable1, IDisposable disposable2)&lt;br /&gt;{&lt;br /&gt;this.first = disposable1;&lt;br /&gt;this.second = disposable2;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;public void Dispose()&lt;br /&gt;{&lt;br /&gt;var d1 = first;&lt;br /&gt;first = null;&lt;br /&gt;try {&lt;br /&gt;d1.DisposeSafely();&lt;br /&gt;}&lt;br /&gt;catch (Exception ex) {&lt;br /&gt;using (var ea = new ExceptionAggregator()) {&lt;br /&gt;ea.Execute(_this =&amp;gt; {&lt;br /&gt;var d2 = _this.second;&lt;br /&gt;_this.second = null;&lt;br /&gt;d2.DisposeSafely();&lt;br /&gt;}, this);&lt;br /&gt;ea.Execute(e =&amp;gt; { throw e; }, ex);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;d1 = second;&lt;br /&gt;second = null;&lt;br /&gt;d1.DisposeSafely();&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;As you see, the code in its &lt;b&gt;Dispose&lt;/b&gt; method relies on &lt;b&gt;ExceptionAggregator&lt;/b&gt;. Here it is:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Provides exception aggregation support.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;[Serializable]&lt;br /&gt;public class ExceptionAggregator : &lt;br /&gt;IDisposable, &lt;br /&gt;ICountable&amp;lt;Exception&amp;gt;&lt;br /&gt;{&lt;br /&gt;private Action&amp;lt;Exception&amp;gt; exceptionHandler;&lt;br /&gt;private List&amp;lt;Exception&amp;gt; exceptions;&lt;br /&gt;private string exceptionMessage;&lt;br /&gt;&lt;br /&gt;private bool isDisposed = false;&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Gets or sets the exception handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public Action&amp;lt;Exception&amp;gt; ExceptionHandler&lt;br /&gt;{&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;get { return exceptionHandler; }&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;set { exceptionHandler = value; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Gets the number of caught exceptions.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public long Count&lt;br /&gt;{&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;get { return exceptions!=null ? exceptions.Count : 0; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#region Execute(...) methods&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified action catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;action&quot;&amp;gt;The action to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public void Execute(Action action)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;action();&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified action catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T&quot;&amp;gt;The type of action argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;action&quot;&amp;gt;The action to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument&quot;&amp;gt;The action argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public void Execute&amp;lt;T&amp;gt;(Action&amp;lt;T&amp;gt; action, T argument)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;action(argument);&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified action catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T1&quot;&amp;gt;The type of the 1st action argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T2&quot;&amp;gt;The type of the 2nd action argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;action&quot;&amp;gt;The action to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument1&quot;&amp;gt;The 1st action argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument2&quot;&amp;gt;The 2nd action argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public void Execute&amp;lt;T1, T2&amp;gt;(Action&amp;lt;T1, T2&amp;gt; action, T1 argument1, T2 argument2)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;action(argument1, argument2);&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified action catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T1&quot;&amp;gt;The type of the 1st action argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T2&quot;&amp;gt;The type of the 2nd action argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T3&quot;&amp;gt;The type of the 3rd action argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;action&quot;&amp;gt;The action to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument1&quot;&amp;gt;The 1st action argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument2&quot;&amp;gt;The 2nd action argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument3&quot;&amp;gt;The 3rd action argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public void Execute&amp;lt;T1, T2, T3&amp;gt;(Action&amp;lt;T1, T2, T3&amp;gt; action, T1 argument1, T2 argument2, T3 argument3)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;action(argument1, argument2, argument3);&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified function catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;TResult&quot;&amp;gt;The type of function result.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;function&quot;&amp;gt;The function to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;Function execution result, if no exception was caught;&lt;br /&gt;/// otherwise, &amp;lt;see langword=&quot;default(TResult)&quot;/&amp;gt;.&amp;lt;/returns&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public TResult Execute&amp;lt;TResult&amp;gt;(Func&amp;lt;TResult&amp;gt; function)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;return function();&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;return default(TResult);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified function catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T&quot;&amp;gt;The type of the function argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;TResult&quot;&amp;gt;The type of function result.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;function&quot;&amp;gt;The function to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument&quot;&amp;gt;The function argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;Function execution result, if no exception was caught;&lt;br /&gt;/// otherwise, &amp;lt;see langword=&quot;default(TResult)&quot;/&amp;gt;.&amp;lt;/returns&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public TResult Execute&amp;lt;T, TResult&amp;gt;(Func&amp;lt;T, TResult&amp;gt; function, T argument)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;return function(argument);&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;return default(TResult);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified function catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T1&quot;&amp;gt;The type of the 1st function argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T2&quot;&amp;gt;The type of the 2nd function argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;TResult&quot;&amp;gt;The type of function result.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;function&quot;&amp;gt;The function to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument1&quot;&amp;gt;The 1st function argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument2&quot;&amp;gt;The 2nd function argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;Function execution result, if no exception was caught;&lt;br /&gt;/// otherwise, &amp;lt;see langword=&quot;default(TResult)&quot;/&amp;gt;.&amp;lt;/returns&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public TResult Execute&amp;lt;T1, T2, TResult&amp;gt;(Func&amp;lt;T1, T2, TResult&amp;gt; function, T1 argument1, T2 argument2)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;return function(argument1, argument2);&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;return default(TResult);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Executes the specified function catching all the exceptions from it,&lt;br /&gt;/// adding it to internal list of caught exceptions and&lt;br /&gt;/// and passing it to &amp;lt;see cref=&quot;ExceptionHandler&quot;/&amp;gt; handler.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T1&quot;&amp;gt;The type of the 1st function argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T2&quot;&amp;gt;The type of the 2nd function argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;T3&quot;&amp;gt;The type of the 3rd function argument.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;typeparam name=&quot;TResult&quot;&amp;gt;The type of function result.&amp;lt;/typeparam&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;function&quot;&amp;gt;The function to execute.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument1&quot;&amp;gt;The 1st function argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument2&quot;&amp;gt;The 2nd function argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;argument3&quot;&amp;gt;The 3rd function argument value.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;Function execution result, if no exception was caught;&lt;br /&gt;/// otherwise, &amp;lt;see langword=&quot;default(TResult)&quot;/&amp;gt;.&amp;lt;/returns&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;ObjectDisposedException&quot;&amp;gt;Aggregator is already disposed.&amp;lt;/exception&amp;gt;&lt;br /&gt;public TResult Execute&amp;lt;T1, T2, T3, TResult&amp;gt;(Func&amp;lt;T1, T2, T3, TResult&amp;gt; function, T1 argument1, T2 argument2, T3 argument3)&lt;br /&gt;{&lt;br /&gt;if (isDisposed)&lt;br /&gt;throw Exceptions.AlreadyDisposed(null);&lt;br /&gt;try {&lt;br /&gt;return function(argument1, argument2, argument3);&lt;br /&gt;}&lt;br /&gt;catch (Exception e) {&lt;br /&gt;HandleException(e);&lt;br /&gt;}&lt;br /&gt;return default(TResult);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endregion&lt;br /&gt;&lt;br /&gt;#region IEnumerable&amp;lt;...&amp;gt; methods&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;IEnumerator IEnumerable.GetEnumerator()&lt;br /&gt;{&lt;br /&gt;return GetEnumerator();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;public IEnumerator&amp;lt;Exception&amp;gt; GetEnumerator()&lt;br /&gt;{&lt;br /&gt;if (exceptions==null)&lt;br /&gt;return EnumerableUtils&amp;lt;Exception&amp;gt;.EmptyEnumerator;&lt;br /&gt;else &lt;br /&gt;return exceptions.GetEnumerator();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endregion&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Invoked on any exception caught by &amp;lt;see cref=&quot;Execute&quot;/&amp;gt; methods.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;exception&quot;&amp;gt;The caught exception.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;remarks&amp;gt;&lt;br /&gt;/// If this method throws an exception, it won&#39;t be caught.&lt;br /&gt;/// I.e. it will throw &quot;through&quot; any of &amp;lt;see cref=&quot;Execute&quot;/&amp;gt; methods.&lt;br /&gt;/// &amp;lt;/remarks&amp;gt;&lt;br /&gt;protected virtual void HandleException(Exception exception)&lt;br /&gt;{&lt;br /&gt;if (exceptionHandler!=null)&lt;br /&gt;exceptionHandler(exception);&lt;br /&gt;if (exceptions==null)&lt;br /&gt;exceptions = new List&amp;lt;Exception&amp;gt;();&lt;br /&gt;exceptions.Add(exception);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Constructors&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot;/&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public ExceptionAggregator()&lt;br /&gt;: this(null, null)&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot;/&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;exceptionMessage&quot;&amp;gt;The message of &amp;lt;see cref=&quot;AggregateException&quot;/&amp;gt;.&amp;lt;/param&amp;gt;&lt;br /&gt;public ExceptionAggregator(string exceptionMessage)&lt;br /&gt;: this (null, exceptionMessage)&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot;/&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;exceptionHandler&quot;&amp;gt;The exception handler.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;exceptionMessage&quot;&amp;gt;The message of &amp;lt;see cref=&quot;AggregateException&quot;/&amp;gt;.&amp;lt;/param&amp;gt;&lt;br /&gt;public ExceptionAggregator(Action&amp;lt;Exception&amp;gt; exceptionHandler, string exceptionMessage)&lt;br /&gt;{&lt;br /&gt;this.exceptionHandler = exceptionHandler;&lt;br /&gt;this.exceptionMessage = exceptionMessage;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// Descructor&lt;br /&gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Dispose&quot; copy=&quot;true&quot;/&amp;gt;&lt;br /&gt;/// &amp;lt;exception cref=&quot;AggregateException&quot;&amp;gt;Thrown if at least one exception was caught &lt;br /&gt;/// by &amp;lt;see cref=&quot;Execute&quot;/&amp;gt; methods.&amp;lt;/exception&amp;gt;&lt;br /&gt;public void Dispose()&lt;br /&gt;{&lt;br /&gt;if (exceptions!=null &amp;amp;&amp;amp; exceptions.Count&amp;gt;0) {&lt;br /&gt;Exception exception = string.IsNullOrEmpty(exceptionMessage) ? &lt;br /&gt;new AggregateException(exceptions) : &lt;br /&gt;new AggregateException(exceptionMessage, exceptions);        &lt;br /&gt;exceptions = null;&lt;br /&gt;isDisposed = true;&lt;br /&gt;throw exception;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;Finally, both these types use &lt;b&gt;AggregateException&lt;/b&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Aggregates a set of caught exceptions.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;[Serializable]&lt;br /&gt;public class AggregateException : Exception,&lt;br /&gt;IHasExceptions&amp;lt;Exception&amp;gt;&lt;br /&gt;{&lt;br /&gt;private ReadOnlyList&amp;lt;Exception&amp;gt; exceptions;&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Gets the list of caught exceptions.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public ReadOnlyList&amp;lt;Exception&amp;gt; Exceptions&lt;br /&gt;{&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;get { return exceptions; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;IEnumerable&amp;lt;Exception&amp;gt; IHasExceptions.Exceptions&lt;br /&gt;{&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;get { return Exceptions; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;IEnumerable&amp;lt;Exception&amp;gt; IHasExceptions&amp;lt;Exception&amp;gt;.Exceptions&lt;br /&gt;{&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;get { return Exceptions; }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Gets the &quot;flat&quot; list with all aggregated exceptions. &lt;br /&gt;/// If other &amp;lt;see cref=&quot; AggregateException&quot;/&amp;gt;s were aggregated, &lt;br /&gt;/// their inner exceptions are included instead of them.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;Flat list of aggregated exceptions.&amp;lt;/returns&amp;gt;&lt;br /&gt;public List&amp;lt;Exception&amp;gt; GetFlatExceptions()&lt;br /&gt;{&lt;br /&gt;var result = new List&amp;lt;Exception&amp;gt;();&lt;br /&gt;&lt;br /&gt;foreach (var exception in exceptions) {&lt;br /&gt;var ae = exception as AggregateException;&lt;br /&gt;if (ae!=null)&lt;br /&gt;result.AddRange(ae.GetFlatExceptions());&lt;br /&gt;else&lt;br /&gt;result.Add(exception);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return result;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;public override string ToString()&lt;br /&gt;{&lt;br /&gt;StringBuilder sb = new StringBuilder(64);&lt;br /&gt;sb.Append(base.ToString());&lt;br /&gt;sb.AppendFormat(&quot;\r\n{0}:&quot;, Strings.OriginalExceptions);&lt;br /&gt;int i = 1;&lt;br /&gt;foreach (Exception exception in exceptions)&lt;br /&gt;sb.AppendFormat(&quot;\r\n{0}: {1}&quot;, i++, exception);&lt;br /&gt;return sb.ToString();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#region Private \ internal methods&lt;br /&gt;&lt;br /&gt;private void SetExceptions(IEnumerable&amp;lt;Exception&amp;gt; exceptions)&lt;br /&gt;{&lt;br /&gt;var list = exceptions as IList&amp;lt;Exception&amp;gt; ?? exceptions.ToList();&lt;br /&gt;this.exceptions = new ReadOnlyList&amp;lt;Exception&amp;gt;(list);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;private void SetExceptions(Exception exception)&lt;br /&gt;{&lt;br /&gt;var list = new List&amp;lt;Exception&amp;gt;();&lt;br /&gt;list.Add(exception);&lt;br /&gt;exceptions = new ReadOnlyList&amp;lt;Exception&amp;gt;(list);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;#endregion&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Constructors&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;public AggregateException()&lt;br /&gt;: base(Strings.ExASetOfExceptionsIsCaught)&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;text&quot;&amp;gt;Text of message.&amp;lt;/param&amp;gt;&lt;br /&gt;public AggregateException(string text)&lt;br /&gt;: base(text)&lt;br /&gt;{&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;message&quot;&amp;gt;Text of message.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;innerException&quot;&amp;gt;Inner exception.&amp;lt;/param&amp;gt;&lt;br /&gt;public AggregateException(string message, Exception innerException) &lt;br /&gt;: base(message, innerException)&lt;br /&gt;{&lt;br /&gt;SetExceptions(innerException);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;exceptions&quot;&amp;gt;Inner exceptions.&amp;lt;/param&amp;gt;&lt;br /&gt;public AggregateException(IEnumerable&amp;lt;Exception&amp;gt; exceptions) &lt;br /&gt;: base(Strings.ExASetOfExceptionsIsCaught, exceptions.First())&lt;br /&gt;{&lt;br /&gt;SetExceptions(exceptions);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;ClassDocTemplate.Ctor&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;message&quot;&amp;gt;Text of message.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;exceptions&quot;&amp;gt;Inner exceptions.&amp;lt;/param&amp;gt;&lt;br /&gt;public AggregateException(string message, IEnumerable&amp;lt;Exception&amp;gt; exceptions) &lt;br /&gt;: base(message, exceptions.First())&lt;br /&gt;{&lt;br /&gt;SetExceptions(exceptions);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;// Serialization&lt;br /&gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;SerializableDocTemplate.Ctor&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;protected AggregateException(SerializationInfo info, StreamingContext context)&lt;br /&gt;: base(info, context)&lt;br /&gt;{&lt;br /&gt;exceptions = (ReadOnlyList&amp;lt;Exception&amp;gt;)info.GetValue(&quot;Exceptions&quot;, typeof (ReadOnlyList&amp;lt;Exception&amp;gt;));&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;/// &amp;lt;see cref=&quot;SerializableDocTemplate.GetObjectData&quot; copy=&quot;true&quot; /&amp;gt;&lt;br /&gt;public override void GetObjectData(SerializationInfo info, StreamingContext context)&lt;br /&gt;{&lt;br /&gt;base.GetObjectData(info, context);&lt;br /&gt;info.AddValue(&quot;Exceptions&quot;, exceptions);&lt;br /&gt;}&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;And now we&#39;re going to the final step. Let&#39;s add one more extension method to our &lt;b&gt;DisposableExtensions&lt;/b&gt;:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;/// &amp;lt;summary&amp;gt;&lt;br /&gt;/// Joins the specified disposable objects by returning&lt;br /&gt;/// a single &amp;lt;see cref=&quot;JoiningDisposable&quot;/&amp;gt; that will&lt;br /&gt;/// dispose both of them on its disposal.&lt;br /&gt;/// &amp;lt;/summary&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;disposable&quot;&amp;gt;The first disposable.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;param name=&quot;joinWith&quot;&amp;gt;The second disposable.&amp;lt;/param&amp;gt;&lt;br /&gt;/// &amp;lt;returns&amp;gt;New &amp;lt;see cref=&quot;JoiningDisposable&quot;/&amp;gt; that will&lt;br /&gt;/// dispose both of them on its disposal&amp;lt;/returns&amp;gt;&lt;br /&gt;public static JoiningDisposable Join(this IDisposable disposable, IDisposable joinWith)&lt;br /&gt;{&lt;br /&gt;return new JoiningDisposable(disposable, joinWith);&lt;br /&gt;}&lt;/pre&gt;&lt;br /&gt;When this is done, you can use the following code with &lt;b&gt;Join&lt;/b&gt; to safely dispose two or more &lt;b&gt;IDisposable&lt;/b&gt;s:&lt;br /&gt;&lt;br /&gt;&lt;pre class=&quot;code&quot;&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;public override object OnEntry(object instance)&lt;br /&gt;{&lt;br /&gt;// ...&lt;br /&gt;var sessionScope = sessionBound.ActivateContext();&lt;br /&gt;var transactionScope = Transaction.Open(sessionBound.Session, true)&lt;br /&gt;return transactionScope.Join(sessionScope); // Joins 2 disposables into one&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// ...&lt;br /&gt;&lt;br /&gt;/// &amp;lt;inheritdoc/&amp;gt;&lt;br /&gt;[DebuggerStepThrough]&lt;br /&gt;public override void OnExit(object instance, object onEntryResult)&lt;br /&gt;{&lt;br /&gt;var d = (IDisposable) onEntryResult;&lt;br /&gt;d.DisposeSafely(); // Safely disposes 2 disposables&lt;br /&gt;}  &lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;b&gt;P.S.&lt;/b&gt; All the code provided here was taken from our &lt;a href=&quot;http://help.dataobjects.net/Default.aspx##DataObjects.Net_4-Essentials.chm/html/N_Xtensive_Core.htm&quot;&gt;Xtensive.Core&lt;/a&gt; assembly. There are &lt;a href=&quot;http://help.dataobjects.net/Default.aspx##DataObjects.Net_4-Essentials.chm/html/T_Xtensive_Core_Disposing_JoiningDisposable.htm&quot;&gt;JoiningDisposable&lt;/a&gt;, &lt;a href=&quot;http://help.dataobjects.net/Default.aspx##DataObjects.Net_4-Essentials.chm/html/T_Xtensive_Core_Disposing_DisposableExtensions.htm&quot;&gt;DisposableExtensions&lt;/a&gt; and everything else I just described.&lt;br /&gt;&lt;br /&gt;Next time I&#39;ll tell you how to simplify safe dealing with &lt;b&gt;IDisposable&lt;/b&gt; further.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/5747249071466843072/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2009/08/making-idisposable-more-usable.html#comment-form' title='76 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5747249071466843072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5747249071466843072'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2009/08/making-idisposable-more-usable.html' title='Making IDisposable more usable, part 1'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>76</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-9019426660212648770</id><published>2009-07-20T11:04:00.004+06:00</published><updated>2009-07-20T11:39:54.120+06:00</updated><title type='text'>Separate Microsoft Help 2.0 SDK installation</title><content type='html'>It&#39;s well-known that Microsoft Help 2.0 SDK can be installed only as part of Visual Studio 2005/2008 SDK - there is simply no separate installer for it. Thanks to us, there is such an installer now, moreover, it contains both Microsoft Help 1.0 and Microsoft Help 2.0 SDKs.&lt;br /&gt;&lt;br /&gt;You can &lt;a href=&quot;http://www.x-tensive.com/Downloads/?Path=Freeware&quot;&gt;download it here&lt;/a&gt; (as .rar archive) - there are just 2.8 MB instead of more than 100 MB you should normally install (Visual Studio 2008 SDK).&lt;br /&gt;&lt;br /&gt;Installation instructions:&lt;br /&gt;- Extract appropriate SDK folder(s) to &quot;C:\Program Files\&quot;&lt;br /&gt;- Use Install.bat and Uninstall.bat inside SDK folders to install or uninstall a particular one.&lt;br /&gt;- If you can&#39;t use &quot;C:\Program Files\&quot; as installation location, update paths in .reg files.&lt;br /&gt;&lt;br /&gt;Happy documenting ;)&lt;br /&gt;&lt;br /&gt;P.S. Check out &lt;a href=&quot;http://wiki.dataobjects.net/index.php?title=FiXml&quot;&gt;FiXml&lt;/a&gt; - quite likely it will be helpful for you as well.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/9019426660212648770/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2009/07/separate-microsoft-help-20-sdk.html#comment-form' title='116 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/9019426660212648770'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/9019426660212648770'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2009/07/separate-microsoft-help-20-sdk.html' title='Separate Microsoft Help 2.0 SDK installation'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>116</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-4566311586192576602</id><published>2009-05-14T09:49:00.036+06:00</published><updated>2009-05-17T21:03:29.536+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="expressions"/><category scheme="http://www.blogger.com/atom/ns#" term="extension methods"/><category scheme="http://www.blogger.com/atom/ns#" term="LINQ"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><title type='text'>Fast expression compilation</title><content type='html'>Recently we&#39;ve faced a well-known problem: &lt;a href=&quot;http://www.google.ru/search?q=LambdaExpression.Compile+is+slow&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;LambdaExpression.Compile&lt;/span&gt; is quite slow&lt;/a&gt;. Mainly, because of:&lt;br /&gt;- Huge amount of reflection&lt;br /&gt;- Excessive security checks&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;Absence of any caching!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;We&#39;ve faced the issue while implementing compilation to executable providers for our &lt;a href=&quot;http://wiki.dataobjects.net/index.php?title=RSE&quot;&gt;RecordSet engine&lt;/a&gt; for &lt;a href=&quot;http://wiki.dataobjects.net/&quot;&gt;DO4&lt;/a&gt;. We should compile generally any expression, such as predicate passed to FilterProvider here. Event quite simple LINQ queries using just a single &lt;span style=&quot;font-weight: bold;&quot;&gt;.Where&lt;/span&gt;  invocation normally require compilation of 2 expressions: one for original &lt;span style=&quot;font-weight: bold;&quot;&gt;.Where&lt;/span&gt; criteria, and one - for index range definition for &lt;span style=&quot;font-weight: bold;&quot;&gt;RangeSetProvider&lt;/span&gt; (we build it as expression).&lt;br /&gt;&lt;br /&gt;Obviously, we recommend everyone to use our cached queries in DO4 - they eliminate the problem. But what if you can&#39;t cache the query - e.g. if it is built dynamically, and actual instances almost always differ?&lt;br /&gt;&lt;br /&gt;So we&#39;ve implemented &lt;span style=&quot;font-weight: bold;&quot;&gt;LambdaExpression&lt;/span&gt; compilation cache. Being implemented well, it solves all the above problems.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;Raw results&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Simple expression compilation test:&lt;/span&gt;&lt;br /&gt;- Expression: &lt;span style=&quot;font-weight: bold;&quot;&gt;(int a, int b) =&gt; a + b&lt;/span&gt;;&lt;br /&gt;- Without caching: &lt;span style=&quot;font-weight: bold;&quot;&gt;15,884&lt;/span&gt; K compilations/s.&lt;br /&gt;- With caching: &lt;span style=&quot;font-weight: bold;&quot;&gt;45,158&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt; &lt;/span&gt;K compilations/s.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Complex expression compilation test:&lt;/span&gt;&lt;br /&gt;- Expression: &lt;span style=&quot;font-weight: bold;&quot;&gt;(int a, int b) =&gt; new {Result = a + b * 2 / a}.Result + DateTime.Now.Day * a * b - a + b&lt;/span&gt;&lt;br /&gt;- Without caching: &lt;span style=&quot;font-weight: bold;&quot;&gt;0,&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;945&lt;/span&gt; K compilations/s.&lt;br /&gt;- With caching: &lt;span style=&quot;font-weight: bold;&quot;&gt;25,747&lt;/span&gt; K compilations/s.&lt;br /&gt;&lt;br /&gt;As you see, &lt;span style=&quot;font-weight: bold;&quot;&gt;the acceleration factor varies from ~ &lt;span style=&quot;color: rgb(204, 0, 0);&quot;&gt;3x to 26x&lt;/span&gt;!&lt;/span&gt; Even this result seems very good. But we decided to investigate why results of standard .NET expression compilation differs so much here and implemented one more test:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Always new expression compilation test:&lt;/span&gt;&lt;br /&gt;- Expression: &lt;span style=&quot;font-weight: bold;&quot;&gt;(int a, int b) =&gt; &lt;/span&gt;&lt;span style=&quot;font-weight: bold; font-style: italic;&quot;&gt;i&lt;/span&gt;;&lt;br /&gt;- Without caching: &lt;span style=&quot;font-weight: bold;&quot;&gt;1,707&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;/span&gt; K compilations/s.&lt;br /&gt;- With caching: &lt;span style=&quot;font-weight: bold;&quot;&gt;27,960&lt;/span&gt; K compilations/s.&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;i&lt;/span&gt;&lt;/span&gt; is an integer increasing its value by one on each compilation attempt. &lt;span style=&quot;font-style: italic;&quot;&gt;So it looks like there is already some kind of caching in .NET expression compilation logic.&lt;/span&gt; But IT FAILS even on such a simple case (difference in constant value)! Probably, they simply cache the compilation result using expression instance as key.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;So &quot;true&quot; acceleration factor is  at least &lt;span style=&quot;color: rgb(204, 0, 0);&quot;&gt;16x&lt;/span&gt;.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;Implementation&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold; color: rgb(204, 0, 0);&quot;&gt;Note:&lt;/span&gt;&lt;span style=&quot;color: rgb(204, 0, 0);&quot;&gt; Further I&#39;ll use &lt;/span&gt;&lt;span style=&quot;font-weight: bold; color: rgb(204, 0, 0);&quot;&gt;GenericType(of T)&lt;/span&gt;&lt;span style=&quot;color: rgb(204, 0, 0);&quot;&gt; instead of standard C# notation with square brackets, because Blogger hates them - it simply cuts them out.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;There is &lt;span style=&quot;font-weight: bold;&quot;&gt;Xtensive.Core.Linq.ExpressionCompileExtensions&lt;/span&gt; class providing a set of additional &lt;span style=&quot;font-weight: bold;&quot;&gt;.CachingCompile()&lt;/span&gt; methods to &lt;span style=&quot;font-weight: bold;&quot;&gt;Expression&lt;/span&gt; type. Its usage is almost the same as of original &lt;span style=&quot;font-weight: bold;&quot;&gt;.Compile&lt;/span&gt; method:&lt;br /&gt;- Original code: &lt;span style=&quot;font-weight: bold;&quot;&gt;var compiledLambda = lambda.Compile()&lt;/span&gt;;&lt;br /&gt;- New code: &lt;span style=&quot;font-weight: bold;&quot;&gt;var compiledLambda  = lambda.&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Caching&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Compile()&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;How it &lt;span style=&quot;font-style: italic;&quot;&gt;could&lt;/span&gt; work:&lt;br /&gt;- Find cached version of provided expression tree in some dictionary&lt;br /&gt;- Return its compiled version, if it exists; otherwise, compile &amp;amp; cache it.&lt;br /&gt;&lt;br /&gt;Actually everything is much more complex:&lt;br /&gt;- Expressions aren&#39;t comparable for equality. They neither override &lt;span style=&quot;font-weight: bold;&quot;&gt;Equals&lt;/span&gt;, nor &lt;span style=&quot;font-weight: bold;&quot;&gt;GetHashCode&lt;/span&gt;. But comparison for equalitry is required to use them as key in dictionary. We can&#39;t use default implmentation as well - &lt;span style=&quot;font-weight: bold;&quot;&gt;Expression&lt;/span&gt; instances are almost always built anew rather than cached.&lt;br /&gt;- Even if they would be comparable for equality, they won&#39;t really be equal because of closures: a new instance of closure is referenced by corresponding &lt;span style=&quot;font-weight: bold;&quot;&gt;ConstantExpression&lt;/span&gt; on the subsequent creation of expression tree. Even if we could be able to compare such constants for equality, they would almost always differ.&lt;br /&gt;&lt;br /&gt;So we&#39;ve implemented:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;ExpressionTree&lt;/span&gt; class implementing &lt;span style=&quot;font-weight: bold;&quot;&gt;IEquatable(of ExpressionTree)&lt;/span&gt;, and properly overriding default &lt;span style=&quot;font-weight: bold;&quot;&gt;GetHashCode&lt;/span&gt; &amp;amp; &lt;span style=&quot;font-weight: bold;&quot;&gt;Equals&lt;/span&gt;. This allows us to compare expressions.&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;ConstantExtractor&lt;/span&gt; - a visitor rewriting the original expression and removing any dependencies on constants from it. In fact, we rewrite original expression replacing each &lt;span style=&quot;font-weight: bold;&quot;&gt;const&lt;/span&gt; of type &lt;span style=&quot;font-weight: bold;&quot;&gt;T&lt;/span&gt; there to &lt;span style=&quot;font-weight: bold;&quot;&gt;((T) additionalLambdaParameter[constNumber])&lt;/span&gt; there. E.g. &lt;span style=&quot;font-weight: bold;&quot;&gt;() =&gt; 1&lt;/span&gt; would be rewritten to &lt;span style=&quot;font-weight: bold;&quot;&gt;(consts) =&gt; ((int) consts&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;[0])&lt;/span&gt;, and we would cache this expression. &lt;span style=&quot;font-weight: bold;&quot;&gt;ConstantExtractor&lt;/span&gt; builds the array of constant values (actual &lt;span style=&quot;font-weight: bold;&quot;&gt;consts&lt;/span&gt; value) during processing of the original expression. Since this happens on any attempt to compile the expression by our caching compiler (because first of all we must build caching key - an &lt;span style=&quot;font-weight: bold;&quot;&gt;ExpressionTree&lt;/span&gt; containing no constants), we always have this array of constants.&lt;br /&gt;- So in the end we always have both compiled expression (processed by &lt;span style=&quot;font-weight: bold;&quot;&gt;ConstantExtractor&lt;/span&gt;) and the array of constants. So we should just &quot;bind&quot; this array to the comiled expression. &quot;Bind&quot; means converting a pair of &lt;span style=&quot;font-weight: bold;&quot;&gt;f(consts,a,b,c, ...)&lt;/span&gt; and &lt;span style=&quot;font-weight: bold;&quot;&gt;extractedConsts&lt;/span&gt; to &lt;span style=&quot;font-weight: bold;&quot;&gt;g(a,b,c, ...) = f(&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;extractedConsts&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;,a,b,c, ...)&lt;/span&gt;. This actually done by corresponding overload of &lt;span style=&quot;font-weight: bold;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Caching&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Compile&lt;/span&gt; method. Its typical code is:&lt;br /&gt;&lt;pre&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;public static Func(of T1, T2, ..., TResult) &lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Caching&lt;/span&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;Compile(of T1, T2, ..., TResult)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;  (this Expression(of Func(of T1, T2, TResult)) lambda)&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;{&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;  var result = CachingExpressionCompiler.Instance.Compile(lambda);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;  return ((Func(of object[], T1, T2, ...)) result.First).Bind(result.Second);&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;color: rgb(0, 0, 153);&quot;&gt;}&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;.Bind&lt;/span&gt; is one more extension method provided by &lt;span style=&quot;font-weight: bold;&quot;&gt;Xtensive.Core.Helpers.DelegateBindExtensions&lt;/span&gt; class.&lt;br /&gt;All &lt;span style=&quot;font-weight: bold;&quot;&gt;.Bind&lt;/span&gt; and &lt;span style=&quot;font-weight: bold;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Caching&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Compile&lt;/span&gt; versions are generated by &lt;a href=&quot;http://tips.x-tensive.com/2009/05/built-in-visual-studio-2008-code.html&quot;&gt;T4 templates&lt;/a&gt; - finally we&#39;ve found the place where we could use them ;)&lt;br /&gt;&lt;br /&gt;And finally, we use our &lt;span style=&quot;font-weight: bold;&quot;&gt;ThreadSafeDictionary&lt;/span&gt; as actual cache, so any cached compiled result is &lt;span style=&quot;font-style: italic;&quot;&gt;never purged&lt;/span&gt;. Initially this may look as a serious lack, but actually it isn&#39;t: currently .NET is incapable of unloading &lt;span style=&quot;font-style: italic;&quot;&gt;any&lt;/span&gt; IL code. Even if we don&#39;t use cache, expression compilation eats some RAM, and this RAM is never released. So our &quot;caching compiler&quot; just eats a bit more - certainly, only if we don&#39;t hit the cache.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;Conclusions&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Pros and cons:&lt;/span&gt;&lt;br /&gt;- We&#39;ve got much faster expression compilation&lt;br /&gt;- It&#39;s quite likely this solution significantly decreases the amount of RAM consumed by complied expressions during the application lifetime, since it significantly decrease the number of actual compilations.&lt;br /&gt;- The compiled expression we return is a bit slower, because we replace constants to array accessors there and add one more delegate call (&lt;span style=&quot;font-weight: bold;&quot;&gt;.Bind&lt;/span&gt; method does this). But I feel this will be acceptable in 99.9% of cases.&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Possible improvements:&lt;/span&gt;&lt;br /&gt;- Use lightweight expression adapters instead of &lt;span style=&quot;font-weight: bold;&quot;&gt;Expression&lt;/span&gt; descendants as result of &lt;span style=&quot;font-weight: bold;&quot;&gt;ConstantExtractor&lt;/span&gt;&lt;span&gt;, such as the ones from &lt;/span&gt;&lt;a href=&quot;http://www.codeplex.com/metalinq&quot;&gt;MetaLinq&lt;/a&gt;. .NET expressions perform lots of checks during their construction, which aren&#39;t necessary here. We must just be able to compare such tree for equality with another one, compute its hash code, and much rarely - convert it to .NET expressions to get it compiled. I feel this should improve the performance at least twice.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-size:180%;&quot;&gt;Usage&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;- &lt;a href=&quot;http://www.x-tensive.com/Downloads/?Path=DataObjects.Net_4&quot;&gt;Download DO4&lt;/a&gt; - it contains compiled version of &lt;span style=&quot;font-weight: bold;&quot;&gt;Xtensive.Core.dll&lt;/span&gt;, as well as its source code&lt;br /&gt;- Add reference to &lt;span style=&quot;font-weight: bold;&quot;&gt;Xtensive.Core.dll&lt;/span&gt; to your project&lt;br /&gt;- Add &quot;&lt;span style=&quot;font-weight: bold;&quot;&gt;using Xtensive.Core.Linq;&lt;/span&gt;&quot; to C# file containing &lt;span style=&quot;font-weight: bold;&quot;&gt;lambda.Compile()&lt;/span&gt;&lt;br /&gt;- Replace &lt;span style=&quot;font-weight: bold;&quot;&gt;lambda.Compile()&lt;/span&gt;&lt;span&gt; to &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;lambda.&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Caching&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;Compile()&lt;/span&gt;&lt;span&gt;.&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/4566311586192576602/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2009/05/fast-expression-compilation.html#comment-form' title='207 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/4566311586192576602'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/4566311586192576602'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2009/05/fast-expression-compilation.html' title='Fast expression compilation'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>207</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-1760502279511643695</id><published>2009-05-06T00:53:00.011+06:00</published><updated>2009-05-13T12:07:09.467+06:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="unknown features"/><category scheme="http://www.blogger.com/atom/ns#" term="visual studio"/><title type='text'>Built-in Visual Studio 2008 Code Generator</title><content type='html'>Have you ever heard about code generator built-in VS 2008 by default? Microsoft named it &lt;b&gt;&quot;T4:&amp;nbsp;Text&amp;nbsp;Template&amp;nbsp;Transformation&amp;nbsp;Toolkit&quot;&lt;/b&gt;. It is integrated into Visual Studio and very easy to use. Let me demonstrate it on example.&lt;br /&gt;&lt;br /&gt;Let us suppose we need a class with methods like those for such primitive types as Int32, Double, Decimal, DateTime, TimeSpan and other.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;public static Double ParseDouble(string value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;Default&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return default(Double);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MinValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Double.MinValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MaxValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Double.MaxValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Double.Parse(value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public static Double? ParseNullableDouble(string value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;Default&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return default(Double);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MinValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Double.MinValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MaxValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Double.MaxValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;Null&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return Double.Parse(value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Because it isn&#39;t very interesting to implement it manually I suggest to use built-in code generator. How we can do this:&lt;br /&gt;&lt;br /&gt;1. Create class SmartParser.cs and implement those methods for one type, e.g. for Int32&lt;br /&gt;2. Rename SmartParser.cs to SmartParser.tt&lt;br /&gt;3. Now it is possible to use ASP.NET-like tags &lt;b&gt;&lt;# #&gt;&lt;/b&gt; and &lt;b&gt;&lt;#= #&gt;&lt;/b&gt; to generate this code for all types we need and Visual Studio will automatically create file SmartParser.cs&lt;br /&gt;&lt;br /&gt;In our case SmartParser.tt will look like this:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;using System;&lt;br /&gt;&lt;br /&gt;public static class SmartParser&lt;br /&gt;{&lt;br /&gt;&lt;b&gt;&lt;#&lt;br /&gt;Type[] types = new Type[] {&lt;br /&gt;&amp;nbsp;&amp;nbsp;typeof(Int32),&lt;br /&gt;&amp;nbsp;&amp;nbsp;typeof(Double),&lt;br /&gt;&amp;nbsp;&amp;nbsp;typeof(Decimal),&lt;br /&gt;&amp;nbsp;&amp;nbsp;typeof(DateTime),&lt;br /&gt;&amp;nbsp;&amp;nbsp;typeof(TimeSpan)&lt;br /&gt;};&lt;br /&gt;foreach (Type type in types) {&lt;br /&gt;#&gt;&lt;/b&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public static &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt; Parse&lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;(string value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;Default&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return default(&lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MinValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;.MinValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MaxValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;.MaxValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;.Parse(value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;br /&gt;&amp;nbsp;&amp;nbsp;public static &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;? ParseNullable&lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;(string value)&lt;br /&gt;&amp;nbsp;&amp;nbsp;{&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;Default&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return default(&lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;);&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MinValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;.MinValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;MaxValue&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;.MaxValue;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value == &quot;Null&quot;)&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return &lt;b&gt;&lt;#=type.Name#&gt;&lt;/b&gt;.Parse(value);&lt;br /&gt;&amp;nbsp;&amp;nbsp;}&lt;br /&gt;&lt;b&gt;&lt;# &lt;br /&gt;} &lt;br /&gt;#&gt;&lt;/b&gt;&lt;br /&gt;}&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Now we can open automatically generated SmartParser.cs just to ensure, that everything is OK and our &quot;huge&quot; class is successfully generated (-;&lt;br /&gt;&lt;br /&gt;You can generate not only *.cs files, but *.xml, *.txt, *.html and generally text files with any extension. There are also several useful directives, for example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;b&gt;&lt;#@ output extension=&quot;txt&quot; #&gt;&lt;br /&gt;&lt;#@ template language=&quot;C#v3.5&quot; #&gt;&lt;br /&gt;&lt;#@ import namespace=&quot;System.Linq&quot; #&gt;&lt;br /&gt;&lt;#@ assembly name=&quot;System.Core&quot; #&gt;&lt;/b&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Any additional information on T4 can be easily found оn the web.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/1760502279511643695/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2009/05/built-in-visual-studio-2008-code.html#comment-form' title='40 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/1760502279511643695'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/1760502279511643695'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2009/05/built-in-visual-studio-2008-code.html' title='Built-in Visual Studio 2008 Code Generator'/><author><name>Alex Kofman</name><uri>http://www.blogger.com/profile/09680608957428101581</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>40</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-5640811270898429750</id><published>2008-11-09T00:16:00.013+05:00</published><updated>2008-11-09T20:33:24.086+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="attributes"/><category scheme="http://www.blogger.com/atom/ns#" term="internal"/><category scheme="http://www.blogger.com/atom/ns#" term="unit testing"/><title type='text'>Are internal members really internal?</title><content type='html'>&lt;div&gt;Each C# developer knows the &quot;internal&quot; access level keyword — The &quot;internal&quot; keyword is an access modifier for types and type members. Internal types or members are accessible only within the same assembly. &lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;But don&#39;t be fully confident in it when dealing with microsofties. Those artful guys invented in the .Net Framework 2.0 a special attribute — &quot;&lt;a href=&quot;http://msdn2.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute.aspx&quot;&gt;InternalsVisibleToAttribute&lt;/a&gt;&quot;  and now your internal members can become - guess what? - public! (of course they will be visible only for specified assemblies).&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;No one knows the real reason for such an &quot;invention&quot; but this innovation has been highly estimated by test-driven development adopters because the usage of this attribute allows your test libraries to access internal classes and methods for additional testing and coverage.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;I think that any other usage of it can be considered a lack of architectural design but what about the microsofties? Do they use it for product-level assemblies or for testing purposes only? Let&#39;s see. Oren Eini made some interesting &lt;a href=&quot;http://ayende.com/Blog/archive/2008/06/27/On-InternalsVisibleTo.aspx&quot;&gt;investigation&lt;/a&gt;  on how the attribute is used inside the .Net Framework:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;br /&gt;System.Data allows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;System.Data.Entity&lt;br /&gt;&lt;/li&gt;&lt;li&gt;SqlAccess&lt;br /&gt;&lt;/li&gt;&lt;li&gt;System.Data.DataSetExtensions&lt;/li&gt;&lt;/ul&gt;Microsoft.NETCF.Tools allows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;System.Web.Services&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Microsoft.Office.Tools.Common.v9.0 allows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Microsoft.Office.Tools.Word.v9.0&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Microsoft.VisualStudio.Tools.Office.Designer.Office2007&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Microsoft.VisualStudio.Tools.Office.Designer.Office2007Tests&lt;br /&gt;&lt;/li&gt;&lt;li&gt;Microsoft.VisualStudio.Tools.Office.Outlook.UnitTests&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Microsoft.Build.Conversion allows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Microsoft.Build.Conversion.Unittest&lt;br /&gt;&lt;/li&gt;&lt;/ul&gt;Microsoft.Build.Engine allows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Microsoft.Build.Engine.Unittest&lt;/li&gt;&lt;/ul&gt;System.Core allows:&lt;br /&gt;&lt;ul&gt;&lt;li&gt;Dlinq.Unittests&lt;/li&gt;&lt;/ul&gt;And so on.&lt;div&gt;&lt;br /&gt;As for Xtensive products, unit testing is the only application of the &quot;InternalsVisibleTo&quot; attribute.&lt;br /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/5640811270898429750/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/11/are-internal-members-really-internal.html#comment-form' title='28 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5640811270898429750'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5640811270898429750'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/11/are-internal-members-really-internal.html' title='Are internal members really internal?'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>28</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-379895387411995695</id><published>2008-11-07T11:26:00.006+05:00</published><updated>2008-11-07T12:40:04.071+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="finalization"/><category scheme="http://www.blogger.com/atom/ns#" term="garbage collection"/><category scheme="http://www.blogger.com/atom/ns#" term="memory leaks"/><title type='text'>Link: object disposal, finalization and resource management</title><content type='html'>This time I publish a &lt;a href=&quot;http://www.bluebytesoftware.com/blog/2005/04/08/DGUpdateDisposeFinalizationAndResourceManagement.aspx&quot;&gt;link to an article&lt;/a&gt; - the article itself is really perfect. &quot;Must know&quot; for any .NET developer.&lt;br /&gt;&lt;br /&gt;Here are some quotations from it to make you a bit more interested:&lt;br /&gt;- Do allow your Dispose method to be called &lt;span style=&quot;font-style: italic;&quot;&gt;more than once&lt;/span&gt;. The method may choose to do nothing after the first call. It should not generate an exception.&lt;br /&gt;- Consider setting disposed fields to null &lt;span style=&quot;font-style: italic;&quot;&gt;before&lt;/span&gt; actually executing Dispose when reference cycles in an object graph are possible.&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;Avoid throwing an exception from within Dispose&lt;/span&gt; except under critical situations where the containing process has been corrupted.&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;Do not&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; assume your finalizer will always run.&lt;/span&gt;&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt; Do &lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;write finalizers that are tolerant of partially constructed instances&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;.&lt;/span&gt;&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;Do write finalizers that are threading-agnostic.&lt;/span&gt; Finalizers can execute in any order, on any thread, can occur on multiple objects concurrently, and even on the same object simultaneously.&lt;br /&gt;- Do gracefully handle situations &lt;span style=&quot;font-style: italic;&quot;&gt;in which your finalizer is invoked more than onc&lt;/span&gt;e.&lt;br /&gt;&lt;br /&gt;The original article is &lt;a href=&quot;http://www.bluebytesoftware.com/blog/2005/04/08/DGUpdateDisposeFinalizationAndResourceManagement.aspx&quot;&gt;here&lt;/a&gt;.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/379895387411995695/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/11/excellent-article-about-object-disposal.html#comment-form' title='26 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/379895387411995695'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/379895387411995695'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/11/excellent-article-about-object-disposal.html' title='Link: object disposal, finalization and resource management'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>26</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-8720656645137913720</id><published>2008-10-30T11:36:00.009+05:00</published><updated>2008-10-31T09:54:02.808+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="thread safety"/><category scheme="http://www.blogger.com/atom/ns#" term="unknown features"/><title type='text'>The cost of [ThreadStatic] attribute</title><content type='html'>First of all, raw results:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;Instance field: Operations: 2,650 B/s.&lt;br /&gt;Static field: Operations: 2,630 B/s.&lt;br /&gt;Volatile instance field: Operations: 2,649 B/s.&lt;br /&gt;[ThreadStatic] field: Operations: 43,611 M/s.&lt;br /&gt;Thread data slot: Operations: 4,068 M/s.&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;The test is actually quite simple: we read specified field in a loop. As before, on Core 2 Duo @2.66GHz. The code can be found in &lt;a href=&quot;http://www.x-tensive.com/Downloads/?Path=DataObjects.Net_4&quot; rel=&quot;nofollow&quot;&gt;DataObjects.Net 4.0&lt;/a&gt; test suite, see &lt;span style=&quot;font-style: italic;&quot;&gt;Xtensive.Core\Xtensive.Core.Tests\DotNetFramework\FieldTypeTest.cs.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;Conclusions:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Reading regular, static or volatile field is quite cheap: ~ 0.2x&lt;/span&gt; in &lt;a href=&quot;http://tips.x-tensive.com/2008/10/method-call-performance.html&quot;&gt;previously introduced metrics&lt;/a&gt;, or 20% of virtual method call time&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;[ThreadStatic] fields are actually quite costly in comparison to others: ~ 14x&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Now the main question: &lt;span style=&quot;font-style: italic;&quot;&gt;why&lt;/span&gt;? It isn&#39;t so obvious [ThreadStatic] is ~ 60 times slower than static.&lt;br /&gt;&lt;br /&gt;JITted [ThreadStatic] access code actually always consists of two parts:&lt;br /&gt;- Call to a system routine returning address of [ThreadStatic] field by its token&lt;br /&gt;- Regular field access instruction.&lt;br /&gt;&lt;br /&gt;Obviously, the first part (call) &quot;eats&quot; almost the whole execution time: there is no more efficient way to get the address of such a field by its token rather than using hash table. As I&#39;ve mentioned &lt;a href=&quot;http://tips.x-tensive.com/2008/10/method-call-performance.html&quot;&gt;before&lt;/a&gt;, &lt;span style=&quot;font-style: italic;&quot;&gt;reading from a system hash table takes ~ 10x&lt;/span&gt;. So that&#39;s nearly what we have in this case.&lt;br /&gt;&lt;br /&gt;Why they&#39;re implemented this way in .NET? I can&#39;t imagine why they didn&#39;t use some faster approach. E.g. I suspect calculating the lowest stack boundary (as well as the upper one) from the current stack pointer value is quite simple operation - something like &lt;span style=&quot;font-style: italic;&quot;&gt;bitwise and&lt;/span&gt;. Why we can&#39;t store the address of the first [ThreadStatic] location as fixed address nearby it, and use constant offset for each [ThreadStatic] field relatively to the address of the first one? In this case it would take ~ &lt;span style=&quot;font-weight: bold;&quot;&gt;1x&lt;/span&gt; to access it...&lt;br /&gt;&lt;br /&gt;Ok, this is what could be, but in reality we have &lt;span style=&quot;font-weight: bold;&quot;&gt;14x&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Finally, there are thread data slots as well. But they&#39;re 10 times slower than [ThreadStatic] fields, so it&#39;s always better to simulate their behavior with e.g. a Dictionary stored in [ThreadStatic] field.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/8720656645137913720/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/10/cost-of-threadstatic-attribute.html#comment-form' title='101 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/8720656645137913720'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/8720656645137913720'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/10/cost-of-threadstatic-attribute.html' title='The cost of [ThreadStatic] attribute'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>101</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-759734529913868582</id><published>2008-10-29T10:26:00.013+05:00</published><updated>2008-10-31T09:23:44.464+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="generics"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><title type='text'>The cost of method calls</title><content type='html'>Here I&#39;ll talk about the cost (or performance) of various ways of method invocation in .NET.&lt;br /&gt;&lt;br /&gt;First of all, let&#39;s assume:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Virtual method call time is 1x.&lt;/span&gt; This is about ~ 600M calls / sec. on Core 2 Duo @ 2.66 GHz. To be precise, we&#39;re talking about instance method getting no arguments and returning a single Int32 value (i.e. it is an average property getter).&lt;br /&gt;&lt;br /&gt;So:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Delegate method call time is 1.5x&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;/span&gt; (the same method invoked by delegate pointing to it)&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Interface method call time is &lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;x&lt;/span&gt; (the same method invoked on reference of interface type)&lt;br /&gt;&lt;br /&gt;A bit surprising, yes? The explanation is &lt;a href=&quot;http://www.codeproject.com/KB/cs/net_type_internals.aspx&quot;&gt;here&lt;/a&gt;. Briefly, interface method dispatch is more complex than delegate dispatch, since we must locate appropriate interface method table for the instance we have first. If count of implemented interfaces on a particular type is quite large (that&#39;s actually almost impossible), the only good way to do this is to use hash table. But if it is small (that&#39;s the most frequent case), there must be just a search in small [possibly - ordered] list. But this is anyway more complex than in case with delegate, since delegate already contains the exact method address (this isn&#39;t correct for &lt;a href=&quot;http://tips.x-tensive.com/2008/10/open-delegates_8670.html&quot;&gt;open delegates&lt;/a&gt;).&lt;br /&gt;&lt;br /&gt;Now one more fact to think about: all the tests I&#39;m talking about are &lt;span style=&quot;font-style: italic;&quot;&gt;loop-based&lt;/span&gt;, performed in Release configuration, and their code is nearly the following:&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;for (int i = 0; i&amp;lt;...; i+=10) {&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;o = c.GetValue();&lt;br /&gt;}&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;.NET is able to cache resolved interface method tables (and possibly - even method addresses), so calling interface method in a loop must be a bit faster, than calling it once. So in general the cost of interface method call in comparison to delegate call is even bigger.&lt;br /&gt;&lt;br /&gt;This explains why we have such types as &lt;a href=&quot;http://help.x-tensive.com/Default.aspx##DataObjects.Net%20v4.0/html/T_Xtensive_Core_Hashing_Hasher_1.htm&quot;&gt;Hasher(of T)&lt;/a&gt; (Comparer, Arithmetic, etc.). They cache the delegates performing a set of operations on T type faster than with use of similar interface in their fields. See e.g. &lt;a href=&quot;http://help.x-tensive.com/Default.aspx##DataObjects.Net%20v4.0/html/F_Xtensive_Core_Hashing_Hasher_1_GetHash.htm&quot;&gt;Hasher(of T).GetHash field&lt;/a&gt;. Certainly, such an approach is used only when &lt;span style=&quot;font-style: italic;&quot;&gt;performance is essential&lt;/span&gt; - i.e. when it&#39;s well known these operations will be invoked many times e.g. on any &lt;a href=&quot;http://wiki.dataobjects.net/index.php?title=Index_seek&quot;&gt;index seek&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;Let&#39;s look of few more metrics:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Creating a delegate pointing to non-virtual method time is 7.5&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;x&lt;/span&gt;&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Creating a delegate pointing to virtual method time is 50x&lt;/span&gt;&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Creating a delegate pointing to interface method time is 150&lt;/span&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;x&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;So &lt;span style=&quot;font-style: italic;&quot;&gt;delegate construction isn&#39;t cheap at all&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Now we&#39;re turning to virtual generic methods:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Virtual generic method call time is 10x&lt;/span&gt; - independently on of it is called on interface or not. Dependency on count of generic arguments almost absents as well - adding one more argument makes the call longer by ~ 0.5%.&lt;br /&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;- Creating a delegate pointing to generic virtual method time is 1000x&lt;/span&gt;. Not sure, why - it looks like because of some bug in .NET. Since delegates in .NET may point to fully parameterized methods only (they store method address), so &lt;span style=&quot;font-weight: bold;&quot;&gt;the time of calling such a delegate is 1.5x, as before&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Why it&#39;s so costly to call virtual generic method? Because there can be generally any number of its implementations, dependently on the argument substitution, so .NET framework resolves its address using internal hash table, that must be bound to corresponding virtual (or interface) method table.&lt;br /&gt;&lt;br /&gt;So we may also take that:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;Internal hash table seek time is ~ 10x&lt;/span&gt; - I&#39;ll use this time in my future posts to show how to estimate the cost of generally any basic operation in .NET.&lt;br /&gt;&lt;br /&gt;And few conclusions related to generic virtual methods:&lt;br /&gt;- &lt;span style=&quot;font-weight: bold;&quot;&gt;The smallest heap allocation time is 3.5x; Int32 boxing time is 4x (with its heap allocation).&lt;/span&gt; So it&#39;s almost always cheaper to have non-generic virtual method with &lt;span style=&quot;font-style: italic;&quot;&gt;object&lt;/span&gt; type argument, rather than generic virtual method.&lt;br /&gt;- If generic virtual method seems anyway preferable, you might &lt;span style=&quot;font-style: italic;&quot;&gt;think about implementing its &quot;call caching&quot; with use of delegates&lt;/span&gt;. E.g. we use &lt;a href=&quot;http://help.x-tensive.com/Default.aspx##DataObjects.Net%20v4.0/html/M_Xtensive_Core_Reflection_DelegateHelper_CreateDelegates__1.htm&quot;&gt;DelegateHelper.CreateDelegates&lt;/a&gt; and &lt;a href=&quot;http://help.x-tensive.com/Default.aspx##DataObjects.Net%20v4.0/html/M_Xtensive_Core_Reflection_DelegateHelper_ExecuteDelegates__1.htm&quot;&gt;DelegateHelper.ExecuteDelegates&lt;/a&gt; methods to perform the operations on &lt;a href=&quot;http://help.x-tensive.com/Default.aspx##DataObjects.Net%20v4.0/html/T_Xtensive_Core_Tuples_Tuple.htm&quot;&gt;Tuples&lt;/a&gt; of the same structure faster.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/759734529913868582/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/10/method-call-performance.html#comment-form' title='270 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/759734529913868582'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/759734529913868582'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/10/method-call-performance.html' title='The cost of method calls'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>270</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-1519653532449129643</id><published>2008-10-26T01:19:00.014+06:00</published><updated>2008-10-31T09:19:38.421+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="unknown features"/><title type='text'>Open delegates</title><content type='html'>Do you know what is &lt;span style=&quot;font-style: italic;&quot;&gt;open delegate&lt;/span&gt;? I was quite surprised when I knew this. They exists since .NET 2.0, but I suspect they were not documented in MSDN initially, so e.g. I have known this much later.&lt;br /&gt;&lt;br /&gt;Links:&lt;br /&gt;- &lt;a href=&quot;http://www.process64.com/OpenDelegatesandClosedDelegates.aspx&quot;&gt;An example&lt;/a&gt;&lt;br /&gt;- &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/96b1ayy4.aspx&quot;&gt;Official documentation&lt;/a&gt;.&lt;br /&gt;&lt;br /&gt;So what is open delegate? &lt;span style=&quot;font-style: italic;&quot;&gt;It is a delegate bound only to a method, but not to an instance, so you can use the same delegate to call the method it is bound to on many instances.&lt;/span&gt; That&#39;s it.&lt;br /&gt;&lt;br /&gt;And few more notes:&lt;br /&gt;- Open delegate invocation must take at least the same time as the underlying virtual or interface method invocation, since .NET can&#39;t use the same way of invocation as for the regular one (there is no single method call address to use). We didn&#39;t test this yet, but if we will, I publish the results here.&lt;br /&gt;- An open delegate bound to virtual generic method must be the slowest one - by the same reason. Do you know why virtual generic methods are the slowest ones? My next post here will explain this.&lt;br /&gt;- Btw, various ways of invocation in .NET are perfectly described &lt;a href=&quot;http://www.codeproject.com/KB/cs/net_type_internals.aspx&quot;&gt;here&lt;/a&gt;. The article covers i.e. regular, virtual, interface methods and delegates.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/1519653532449129643/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/10/open-delegates_8670.html#comment-form' title='48 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/1519653532449129643'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/1519653532449129643'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/10/open-delegates_8670.html' title='Open delegates'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>48</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-355528356697383306</id><published>2008-10-17T16:23:00.008+06:00</published><updated>2008-10-31T09:24:32.743+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="finalization"/><category scheme="http://www.blogger.com/atom/ns#" term="garbage collection"/><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><title type='text'>The cost of having a finalizer</title><content type='html'>It&#39;s well-known that having a finalizer is costly. But how much does it cost - to have a finalizer?&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-style: italic;&quot;&gt;&lt;span style=&quot;font-weight: bold;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;Allocation to nothing:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;- SlimObject: Operations: 77.880 M/s.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;- FinalizableSlimObject: Operations: 1.628 M/s.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;Allocation to an array:&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;- SlimObject: Operations: 6.996 M/s.&lt;/span&gt;&lt;br /&gt;&lt;span style=&quot;font-family:courier new;&quot;&gt;- FinalizableSlimObject: Operations: 3.256 M/s.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&quot;Allocation to nothing&quot; here is allocation without storing a reference anywhere. This case must be the most frequent scenario, since most of allocated objects don&#39;t survive the next GC in generation 0.&lt;br /&gt;&lt;br /&gt;There is no any additional GC cost for &lt;span style=&quot;font-weight: bold;&quot;&gt;SlimObject&lt;/span&gt; in this case - there are no references to any of such objects, so garbage collector won&#39;t mark any of them, and consequently, won&#39;t spent any time on them at all.&lt;br /&gt;&lt;br /&gt;But what&#39;s with &lt;span style=&quot;font-weight: bold;&quot;&gt;FinalizeableSlimObject&lt;/span&gt;?&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;It&#39;s allocation is costly, since it is registered for further finalization&lt;/span&gt; (added into a finalization queue). Registration cost must be similar to getting a lock and adding an object to &lt;span style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt;.&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;All of such objects are processed (i.e. marked) during the GC&lt;/span&gt;&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;Then they&#39;re removed from finalization queue&lt;/span&gt; (~ finding &amp;amp; removing an entry from a &lt;span style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt;)&lt;br /&gt;- And &lt;span style=&quot;font-style: italic;&quot;&gt;added to FReachable queue&lt;/span&gt; (cheap, ~ adding to a list).&lt;br /&gt;- They&#39;re also &lt;span style=&quot;font-style: italic;&quot;&gt;processed on heap compaction phase.&lt;/span&gt; We get an additional cost &lt;span style=&quot;font-style: italic;&quot;&gt;proportional to the size of an object&lt;/span&gt; here.&lt;br /&gt;- &lt;span style=&quot;font-style: italic;&quot;&gt;A separate finalization thread will spend some time on them to finalize them.&lt;/span&gt; This must be not quite noticeable in this test, since the test have been performed on multicore CPU.&lt;br /&gt;- And finally, &lt;span style=&quot;font-style: italic;&quot;&gt;if some of them will survive until the next GC in generation 1, we&#39;ll will add further overhead&lt;/span&gt; similar to described.&lt;br /&gt;&lt;br /&gt;So as you see, &lt;span style=&quot;font-style: italic;&quot;&gt;allocation of short living finalizable object is &lt;/span&gt;&lt;span style=&quot;font-weight: bold; font-style: italic;&quot;&gt;50 times&lt;/span&gt;&lt;span style=&quot;font-style: italic;&quot;&gt; more expensive than of the regular one!&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now let&#39;s turn to long-living objects: &quot;Allocation to an array&quot; here is allocation with storing a reference to allocated object in array. The test was running for about a second, so there must be several generation 1 GCs. Certainly &lt;span style=&quot;font-weight: bold;&quot;&gt;SlimObject&lt;/span&gt; wins here as well (about &lt;span style=&quot;font-weight: bold;&quot;&gt;2 times&lt;/span&gt;), but not as mach as in above case. Why?&lt;br /&gt;- Because all the objects survive many GCs, and GC overhead is the most significant factor for both types of them.&lt;br /&gt;- One more factor slowing down the test with &lt;span style=&quot;font-weight: bold;&quot;&gt;SlimObject&lt;/span&gt; is less efficient usage of L1\L2 caches: in first case all the data were &quot;laying&quot; mainly in cache, but here pages must &quot;flow&quot; through the caches. But I suspect this is much less important factor than the first one here.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;In any case, probably it&#39;s time to finalize some finalizers ;) At least the ones that aren&#39;t really necessary.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/355528356697383306/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/10/cost-of-having-finalizer.html#comment-form' title='35 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/355528356697383306'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/355528356697383306'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/10/cost-of-having-finalizer.html' title='The cost of having a finalizer'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>35</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-5242968269702564960</id><published>2008-10-16T18:00:00.004+06:00</published><updated>2008-10-26T12:06:25.454+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="garbage collection"/><category scheme="http://www.blogger.com/atom/ns#" term="memory leaks"/><category scheme="http://www.blogger.com/atom/ns#" term="unknown features"/><title type='text'>Dictionary: surprising fact about Remove and Clear methods</title><content type='html'>Both these methods never resize internal bucket list of the &lt;span style=&quot;font-weight: bold;&quot;&gt;Dictionary&lt;k,v&gt;&lt;/k,v&gt;&lt;/span&gt; - this means dictionary bucket list may only grow up during its lifetime, and the only way to &quot;shrink&quot; it is to recreate the dictionary (create a new one, copy the content of the old one to it and forget the old one).&lt;br /&gt;&lt;br /&gt;To get the approval of this fact, study the source code of both methods - e.g. with &lt;a href=&quot;http://reflector.red-gate.com/&quot;&gt;.NET Reflector&lt;/a&gt;.</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/5242968269702564960/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/10/dictionary-surprising-fact-about-remove.html#comment-form' title='15 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5242968269702564960'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5242968269702564960'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/10/dictionary-surprising-fact-about-remove.html' title='Dictionary&lt;K,V&gt;: surprising fact about Remove and Clear methods'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>15</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8486822413101972100.post-5752611514431364485</id><published>2008-10-16T12:31:00.007+06:00</published><updated>2008-10-31T09:25:02.232+05:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="performance"/><category scheme="http://www.blogger.com/atom/ns#" term="thread safety"/><category scheme="http://www.blogger.com/atom/ns#" term="unknown features"/><title type='text'>When Hashtable is better than Dictionary</title><content type='html'>We all know &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt; class - it exists in .NET from the very beginning. .NET 2.0 has brought a generic replacement to it: &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Dictionary.&lt;k,v&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt; So we usually prefer using it rather than &lt;/span&gt;Hashtable&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;. But it isn&#39;t well known &lt;/span&gt;Hashtable&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt; has a very nice feature related to concurrent usage, which &lt;/span&gt;Dictionary&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt; does not have (at least by documentation):&lt;/span&gt;&lt;/k,v&gt;&lt;/span&gt;&lt;div&gt;- &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt; supports &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: italic;&quot;&gt;single writer &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;and&lt;/span&gt; multiple readers&lt;/span&gt; concurrent access policy&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;&lt;k,v&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: normal;&quot;&gt;- But &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Dictionary&lt;/span&gt; - standard &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-style: italic;&quot;&gt;single writer &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;or&lt;/span&gt; multiple readers &lt;/span&gt;&lt;/span&gt;&lt;/k,v&gt;&lt;/span&gt;concurrent access policy&lt;div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;This means usage of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt; is perfect e.g. in concurrent caching scenarios: only write operations on it require locking to ensure thread safety, but in case with &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Dictionary&lt;/span&gt; both read and write operations require locking.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;The official &quot;approval&quot; of this fact is &lt;a href=&quot;http://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;And a fly in the ointment:&lt;/div&gt;&lt;div&gt;- It&#39;s difficult to find out if this was fully correct for .NET 2.0, since this fact was mentioned only recently. The issue report related to this is &lt;a href=&quot;https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=269447&quot;&gt;here&lt;/a&gt;.&lt;/div&gt;&lt;div&gt;- This is not completely true:&lt;span&gt;&lt;span&gt; &lt;a href=&quot;http://blogs.msdn.com/brada/archive/2003/04/13/49969.aspx&quot;&gt;here&lt;/a&gt; is the report. &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;Possible workarounds:&lt;/div&gt;&lt;div&gt;- Never clear or remove items from &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt; at all. In many cases this is ok, e.g. when you cache something for a lifetime of the cache or the whole application. An example is caching of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Reflection.Emit&lt;/span&gt; results or any other code loaded into &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;AppDomain&lt;/span&gt;: you anyway can&#39;t unload the code, so there is no reason to clean up any references to it from caches.&lt;/div&gt;&lt;div&gt;- Implement item removal by dropping the old &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt; and copying its content (without the item to remove) to a new one. Or implement a wrapper doing the same using two &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt;s (one contains just added keys and values, and andother one just removed keys) and periodically doing the garbage collection be re-creating both of them as described above. But actually the cost of looking into two &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt;s must be close to the cost of locking.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;In any case, this feature is very nice even with all the mentioned problems. We utilize this feature of &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;Hashtable&lt;/span&gt; e.g. in our &lt;span class=&quot;Apple-style-span&quot; style=&quot;font-weight: bold;&quot;&gt;ThreadSafeDictionary&lt;/span&gt; class.&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://tips.x-tensive.com/feeds/5752611514431364485/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://tips.x-tensive.com/2008/10/when-hashtable-is-better-then.html#comment-form' title='37 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5752611514431364485'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8486822413101972100/posts/default/5752611514431364485'/><link rel='alternate' type='text/html' href='http://tips.x-tensive.com/2008/10/when-hashtable-is-better-then.html' title='When Hashtable is better than Dictionary'/><author><name>Unknown</name><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>37</thr:total></entry></feed>