<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Peter Petrov's Weblog</title>
	<atom:link href="http://ppetrov.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://ppetrov.wordpress.com</link>
	<description>var me = from practice in programming where practice.IsBestPractice &#38;&#38; practice.UseLambda select practice.OptimalPerformance;</description>
	<lastBuildDate>Tue, 11 Oct 2011 06:27:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='ppetrov.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Peter Petrov's Weblog</title>
		<link>http://ppetrov.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://ppetrov.wordpress.com/osd.xml" title="Peter Petrov&#039;s Weblog" />
	<atom:link rel='hub' href='http://ppetrov.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Parse string having comma separated integers</title>
		<link>http://ppetrov.wordpress.com/2011/08/01/parse-string-having-comma-separated-integers/</link>
		<comments>http://ppetrov.wordpress.com/2011/08/01/parse-string-having-comma-separated-integers/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 17:01:56 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[Useful methods]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=124</guid>
		<description><![CDATA[I&#8217;ve read a blog post about splitting a string containing comma separated integers and then creating an array. It&#8217;s a very short and simple code but there&#8217;s a little problem &#8211; it uses the Split() method and the result is an array, potentially big array. What if you only need the first 10 results out [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=124&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve read a blog <a title="String to integer array" href="http://www.codeasp.net/blogs/raghav_khunger/microsoft-net/1821/c-convert-a-string-having-comma-separated-integers-to-an-array-of-integers">post</a> about splitting a string containing comma separated integers and then creating an array. It&#8217;s a very short and simple code but there&#8217;s a little problem &#8211; it uses the <a title="String.Split Method" href="http://msdn.microsoft.com/en-us/library/system.string.split.aspx">Split()</a> method and the result is <strong>an array</strong>, <strong><a title="Arrays considered somewhat harmful" href="http://blogs.msdn.com/b/ericlippert/archive/2008/09/22/arrays-considered-somewhat-harmful.aspx">potentially big array</a></strong>. What if you only need the first 10 results out of ten thousand integers ? You will do the work to split ten thousand integers and you will consume a lot memory then you need and finally you will create a lot of garbage. I&#8217;ve written a class to solve this issues.</p>
<pre class="brush: csharp;">public class IntConverter
{
private readonly char _separator;

public IntConverter()
: this(',')
{
}

public IntConverter(char separator)
{
_separator = separator;
}

public IEnumerable&lt;int&gt; From(string input)
{
if (input == null) throw new ArgumentNullException(&quot;input&quot;);

return FromImplementation(input.Trim(), _separator);
}

public string To(IEnumerable&lt;int&gt; numbers)
{
if (numbers == null) throw new ArgumentNullException(&quot;numbers&quot;);

var buffer = new StringBuilder();

foreach (var n in numbers)
{
if (buffer.Length &gt; 0)
{
buffer.Append(_separator);
}
buffer.Append(n.ToString());
}

return buffer.ToString();
}

private IEnumerable&lt;int&gt; FromImplementation(string input, char separator)
{
if (input == string.Empty)
{
yield break;
}

var buffer = new StringBuilder();

var symbols = input.ToCharArray();
for (var i = 0; i &lt; symbols.Length; i++)
{
var symbol = symbols[i];
var isSeparator = (symbol == separator);
if (isSeparator)
{
yield return int.Parse(buffer.ToString());
buffer.Length = 0;
}
else
{
buffer.Append(symbol);
}
}

yield return int.Parse(buffer.ToString());
}
}</pre>
<p>I wish there will be an overload of int.Parse or int.TryParse witch accepts StringBuilder as input to reduce even more the memory usage in this scenario.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/124/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/124/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/124/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=124&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2011/08/01/parse-string-having-comma-separated-integers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
		<item>
		<title>Free Typemock licenses – ASP.NET bundle launch</title>
		<link>http://ppetrov.wordpress.com/2009/05/19/free-typemock-licenses-%e2%80%93-asp-net-bundle-launch/</link>
		<comments>http://ppetrov.wordpress.com/2009/05/19/free-typemock-licenses-%e2%80%93-asp-net-bundle-launch/#comments</comments>
		<pubDate>Tue, 19 May 2009 07:21:23 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Typemock]]></category>
		<category><![CDATA[Unit Testing ASP.NET]]></category>
		<category><![CDATA[Unit Testing ASP.NET MVC]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=96</guid>
		<description><![CDATA[Today I&#8217;ve read this Get Free Typemock licenses – ASP.NET bundle launch on the official Typemock blog. I thought to myself : Is it possible for me to win a FREE license ? Why not. After all it&#8217;s Typemock and it&#8217;s an official blog post.  I hope I&#8217;ll be in the group of these 60 [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=96&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.typemock.com/"></a></p>
<p>Today I&#8217;ve read this<a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html"> Get Free Typemock licenses – ASP.NET bundle launch</a> on the official Typemock blog. I thought to myself : Is it possible for me to win a FREE license ? Why not. After all it&#8217;s Typemock and it&#8217;s an official blog post.  I hope I&#8217;ll be in the group of these 60 lucky bloggers. I need to hurry <img src='http://s0.wp.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p><a href="http://www.typemock.com/">Unit Testing</a> ASP.NET? <a href="http://www.typemock.com/ASP.NET_unit_testing_page.php">ASP.NET unit testing</a> has never been this easy.</p>
<p>Typemock is launching a new product for ASP.NET developers – the <strong>ASP.NET Bundle</strong> &#8211; and for the launch will be giving out <span style="color:#006600;"><strong>FREE licenses</strong></span> to bloggers and their readers.</p>
<p>The ASP.NET Bundle is the ultimate ASP.NET unit testing solution, and offers both <a href="http://www.typemock.com/">Typemock Isolator</a>, a <a href="http://www.typemock.com/">unit test</a> tool and <a href="http://sm-art.biz/Ivonna.aspx">Ivonna</a>, the Isolator add-on for <a href="http://sm-art.biz/Ivonna.aspx">ASP.NET unit testing</a>, for a bargain price.</p>
<p>Typemock Isolator is a leading <a href="http://www.typemock.com/">.NET unit testing</a> tool (C# and VB.NET) for many ‘hard to test’ technologies such as <a href="http://typemock.com/sharepointpage.php">SharePoint</a>, <a href="http://www.typemock.com/ASP.NET_unit_testing_page.php">ASP.NET</a>, <a href="http://www.typemock.com/ASP.NET_unit_testing_page.php">MVC</a>, <a href="http://www.typemock.com/wcfpage.php">WCF</a>, WPF, <a href="http://www.typemock.com/Silverlight_unit_testing_page.php">Silverlight</a> and more. Note that for <a href="http://www.typemock.com/Silverlight_unit_testing_page.php">unit testing Silverlight</a> there is an open source Isolator add-on called <a href="http://www.typemock.com/Silverlight_unit_testing_page.php">SilverUnit</a>.</p>
<p>The first 60 bloggers who will blog this text in their blog and <a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html">tell us about it</a>, will get a Free Isolator ASP.NET Bundle license (Typemock Isolator + Ivonna). If you post this in an ASP.NET <strong>dedicated</strong> blog, you&#8217;ll get a license automatically (even if more than 60 submit) during the first week of this announcement.</p>
<p>Also 8 bloggers will get an <strong>additional 2 licenses</strong> (each) to give away to their readers / friends.</p>
<p>Go ahead, click the following link for <a href="http://blog.typemock.com/2009/05/get-free-typemock-licenses-aspnet.html">more information </a>on how to get your free license.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/96/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/96/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/96/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=96&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2009/05/19/free-typemock-licenses-%e2%80%93-asp-net-bundle-launch/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
		<item>
		<title>Stop posting images of the code, post the code !</title>
		<link>http://ppetrov.wordpress.com/2009/04/14/stop-posting-images-of-the-code-post-the-code/</link>
		<comments>http://ppetrov.wordpress.com/2009/04/14/stop-posting-images-of-the-code-post-the-code/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 07:08:03 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[General notes]]></category>
		<category><![CDATA[blog posts]]></category>
		<category><![CDATA[source code]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=23</guid>
		<description><![CDATA[First there is no Copy/Paste so the reader can&#8217;t easily copy the code to try it and/or test it. Second there is no search available on the page. Third for a search engines it&#8217;s impossible to mine the data represented by the image. You won&#8217;t have a match event if the user keywords perfectly match [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=23&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div id="attachment_90" class="wp-caption alignleft" style="width: 293px"><img class="size-full wp-image-90" title="Soure code" src="http://ppetrov.files.wordpress.com/2009/04/fake_code.jpg?w=283&#038;h=500" alt="Windows Vista Source Code" width="283" height="500" /><p class="wp-caption-text">Windows Vista Source Code</p></div>
<p>First there is no Copy/Paste so the reader can&#8217;t easily copy the code to try it and/or test it.</p>
<p>Second there is no search available on the page.</p>
<p>Third for a <a title="Web search engine" href="http://en.wikipedia.org/wiki/Web_search_engine">search engines</a> it&#8217;s impossible to mine the data represented by the image. You won&#8217;t have a match event if the user keywords perfectly match your (image)contents.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=23&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2009/04/14/stop-posting-images-of-the-code-post-the-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>

		<media:content url="http://ppetrov.files.wordpress.com/2009/04/fake_code.jpg" medium="image">
			<media:title type="html">Soure code</media:title>
		</media:content>
	</item>
		<item>
		<title>ForEach method on IEnumerable</title>
		<link>http://ppetrov.wordpress.com/2009/01/22/foreach-method-on-ienumerable/</link>
		<comments>http://ppetrov.wordpress.com/2009/01/22/foreach-method-on-ienumerable/#comments</comments>
		<pubDate>Thu, 22 Jan 2009 14:37:55 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Extension methods]]></category>
		<category><![CDATA[Excetion Method]]></category>
		<category><![CDATA[IEnumerable]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=71</guid>
		<description><![CDATA[I&#8217;ve noticed that when I use IEnumerable&#60;T&#62; very often I have a construction like this in my code. I&#8217;ve thinked about it and I&#8217;ve realized that ToList() will create (probably) a large list just to iterrate over it and perform some action. That&#8217;s just because IEnumerable&#60;T&#62; doesn&#8217;t have ForEach method. Using an extension method I&#8217;m [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=71&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve noticed that when I use IEnumerable&lt;T&gt; very often I have a construction like this</p>
<pre class="brush: csharp;">.ToList().ForEach(x=&gt; ...)</pre>
<p>in my code. I&#8217;ve thinked about it and I&#8217;ve realized that ToList() will create (probably) a large list just to iterrate over it and perform some action. That&#8217;s just because IEnumerable&lt;T&gt; doesn&#8217;t have ForEach method. Using an extension method I&#8217;m able to fill this gap. So here&#8217;s the ForEach method on IEnumerable&lt;T&gt; :</p>
<pre class="brush: csharp;">public static void ForEach&lt;T&gt;(this IEnumerable&lt;T&gt; values, Action&lt;T&gt; action)
{
    foreach (var v in values)
    {
        action(v);
    }
}</pre>
<p>It&#8217;s possible to return the values reference to allow method chaining  but personally I think the foreach must be the last thing to perform, otherwise it will lead you to a bad practice. Let me explain what I mean. Imagine we have this Person class and a method that returns all Persons.</p>
<pre class="brush: csharp;">public static IEnumerable&lt;Person&gt; GetAllPersons()
{
    // ...
    yield break;
}
public class Person
{
    public long Fortune { get; private set; }

    public void SlowMethod()
    {
        Thread.Sleep(1000);
        // ...
    }
}</pre>
<p>Take a look a the fowling two methods that retrieves all rich persons</p>
<pre class="brush: csharp;">var richPersonsSlow = GetAllPersons().ForEach(p =&gt; p.SlowMethod()).Where(p =&gt; p.Fortune &gt; 1000000);
var richPersonsFast = GetAllPersons().Where(p =&gt; p.Fortune &gt; 1000000).ForEach(p =&gt; p.SlowMethod());</pre>
<p>Take this scenario : we have 3600 persons and only one have more then one million.</p>
<p>The first line will take <strong>an hour</strong> to return the millionaire.</p>
<p>The second line will take only <strong>one second</strong> to return the millionaire because p.SlowMethod will be called only on the millionaire.</p>
<p>There&#8217;s a little semantic difference in the example but I think you get the point. Using the ForEach returning void eliminates this possible issue.</p>
<p>I hope the BCL Team will include this method (not necessarly my implementation) in C# 4.0 .</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/71/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/71/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/71/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=71&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2009/01/22/foreach-method-on-ienumerable/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
		<item>
		<title>SCOPE_IDENTITY() vs @@IDENTITY &#8211; 1:0</title>
		<link>http://ppetrov.wordpress.com/2008/09/19/scope_identity-vs-identity-10/</link>
		<comments>http://ppetrov.wordpress.com/2008/09/19/scope_identity-vs-identity-10/#comments</comments>
		<pubDate>Fri, 19 Sep 2008 13:55:10 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[Note to self]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[@@IDENTITY]]></category>
		<category><![CDATA[SCOPE_IDENTITY()]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=61</guid>
		<description><![CDATA[@@IDENTITY returns the most recently created identity for your current connection, not necessarily the identity for the recently added row in a table. Always use SCOPE_IDENTITY() to return the identity of the recently added row.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=61&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p><strong><a title="@@IDENTITY (Transact-SQL)" href="http://msdn.microsoft.com/en-us/library/ms187342.aspx">@@IDENTITY</a> returns the most recently created identity for your current connection, not necessarily the identity for the recently added row in a table. Always use <a title="SCOPE_IDENTITY (Transact-SQL)" href="http://msdn.microsoft.com/en-us/library/ms190315.aspx">SCOPE_IDENTITY() </a>to return the identity of the recently added row.</strong></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/61/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/61/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/61/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=61&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2008/09/19/scope_identity-vs-identity-10/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
		<item>
		<title>Usuful methods &#8211; 11 of N &#8211; Copy/Move Directory</title>
		<link>http://ppetrov.wordpress.com/2008/09/12/usuful-methods-11-of-n-copymove-directory/</link>
		<comments>http://ppetrov.wordpress.com/2008/09/12/usuful-methods-11-of-n-copymove-directory/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 14:09:14 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Copy directory]]></category>
		<category><![CDATA[copy entire directory structure]]></category>
		<category><![CDATA[Copy folder]]></category>
		<category><![CDATA[Move directory]]></category>
		<category><![CDATA[move entire directory structure]]></category>
		<category><![CDATA[Move folder]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=52</guid>
		<description><![CDATA[Today I have to copy an entire directory including all files, sub directories and all sub files to another directory. The .NET framework doesn&#8217;t provide such method (at least I don&#8217;t know of it) so I have coded one myself. Here&#8217;s the implementation of the CopyDirectory method. MoveDirectory is very simple when we have CopyDirectory. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=52&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Today I have to copy an entire directory including all files, sub directories and all sub files to another directory. The .NET framework doesn&#8217;t provide such method (at least I don&#8217;t know of it) so I have coded one myself. Here&#8217;s the implementation of the <strong>CopyDirectory </strong>method.</p>
<pre class="brush: csharp;">        public static void CopyDirectory(string source, string destination)
        {
            if (destination[destination.Length - 1] != Path.DirectorySeparatorChar)
            {
                destination += Path.DirectorySeparatorChar;
            }
            if (!Directory.Exists(destination))
            {
                Directory.CreateDirectory(destination);
            }
            var entries = Directory.GetFileSystemEntries(source);
            foreach (var e in entries)
            {
                if (Directory.Exists(e))
                {
                    CopyDirectory(e, destination + Path.GetFileName(e));
                }
                else
                {
                    File.Copy(e, destination + Path.GetFileName(e), true);
                }
            }
        }</pre>
<p><strong>MoveDirectory</strong> is very simple when we have <strong>CopyDirectory</strong>. I know it&#8217;s not as fast as it can be ( if we use <strong>File.Move()</strong>) but still it gets the job done.</p>
<pre class="brush: csharp;">        public static void MoveDirectory(string source, string destination)
        {
            CopyDirectory(source, destination);
            Directory.Delete(source);
        }</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ppetrov.wordpress.com/52/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ppetrov.wordpress.com/52/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/52/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/52/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/52/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=52&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2008/09/12/usuful-methods-11-of-n-copymove-directory/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
		<item>
		<title>SQL Server 2008 &#8211; FILESTREAM feature is disabled.</title>
		<link>http://ppetrov.wordpress.com/2008/08/15/sql-server-2008-enable-filestream/</link>
		<comments>http://ppetrov.wordpress.com/2008/08/15/sql-server-2008-enable-filestream/#comments</comments>
		<pubDate>Fri, 15 Aug 2008 13:21:02 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=45</guid>
		<description><![CDATA[Yesterday I have installed SQL Server 2008. I have downloaded the product samples from codeplex and I have tried to restore the Adventure Works 2008 Sample Database but I&#8217;ve this error : System.Data.SqlClient.SqlError: FILESTREAM feature is disabled. (Microsoft.SqlServer.Smo) OK the FILESTREAM is disabled but the question is how can I enable it ? The solution [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=45&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yesterday I have installed SQL Server 2008. I have downloaded the <a href="http://www.codeplex.com/MSFTDBProdSamples">product samples from codeplex</a> and I have tried to restore the Adventure Works 2008 Sample Database but I&#8217;ve this error :</p>
<p><a href="http://ppetrov.files.wordpress.com/2008/08/filestream.jpg"><img class="alignnone size-medium wp-image-46" src="http://ppetrov.files.wordpress.com/2008/08/filestream.jpg?w=300&#038;h=77" alt="" width="300" height="77" /></a></p>
<p>System.Data.SqlClient.SqlError: FILESTREAM feature is disabled. (Microsoft.SqlServer.Smo)</p>
<p>OK the FILESTREAM is disabled but the question is how can I enable it ? The solution is very simple but it took me some time to find in on google. Here&#8217;s the link to the SQL Server 2008  Books Online on MSDN &#8211; <a href="http://msdn.microsoft.com/en-us/library/cc645923.aspx"><strong>How to: Enable FILESTREAM<br />
</strong></a></p>
<p>Just fire SQL Server 2008 Management Studio and type</p>
<pre class="brush: sql;">EXEC sp_configure filestream_access_level, 2
RECONFIGURE</pre>
<p>I don&#8217;t know why Microsoft doesn&#8217;t provided this link as help. Instead of this they suggested m</p>
<p>ADDITIONAL INFORMATION:</p>
<p>System.Data.SqlClient.SqlError: FILESTREAM feature is disabled. (Microsoft.SqlServer.Smo)</p>
<p>For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&amp;ProdVer=10.0.1600.22+((SQL_PreRelease).080709-1414+)&amp;LinkId=20476</p>
<p>which is useless.</p>
<p>The final result is that I have FILESTREAM enabled and I have my AdventureWorks 2008 database restored.</p>
<p><a href="http://ppetrov.files.wordpress.com/2008/08/success.jpg"><img class="alignnone size-medium wp-image-48" src="http://ppetrov.files.wordpress.com/2008/08/success.jpg?w=300&#038;h=62" alt="" width="300" height="62" /></a></p>
<p>Now I can continue with my tests.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ppetrov.wordpress.com/45/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ppetrov.wordpress.com/45/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/45/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/45/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/45/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=45&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2008/08/15/sql-server-2008-enable-filestream/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>

		<media:content url="http://ppetrov.files.wordpress.com/2008/08/filestream.jpg?w=300" medium="image" />

		<media:content url="http://ppetrov.files.wordpress.com/2008/08/success.jpg?w=300" medium="image" />
	</item>
		<item>
		<title>SQL Server 2008 Management Studio &#8211; Rename column &amp; Intellisense</title>
		<link>http://ppetrov.wordpress.com/2008/08/08/sql-server-2008-management-studio-rename-column-intellisense/</link>
		<comments>http://ppetrov.wordpress.com/2008/08/08/sql-server-2008-management-studio-rename-column-intellisense/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 14:41:04 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2008 Management Studio]]></category>
		<category><![CDATA[SSMS]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=35</guid>
		<description><![CDATA[Yesterday when I have played around with SQL Server 2008 Management Studio(SSMS) RC0. I have spotted a bug and I have filed a bug report. The new SSMS has Intellisense but it isn&#8217;t invalidated when you rename a column. When we fire SSMS and write a simple query everything is correct. When I rename the [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=35&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Yesterday when I have played around with SQL Server 2008 Management Studio(SSMS) RC0. I have spotted a bug and I have filed a <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=360585">bug report</a>. The new SSMS has Intellisense but it isn&#8217;t invalidated when you rename a column. When we fire SSMS and write a simple query everything is correct.</p>
<p><a href="http://ppetrov.files.wordpress.com/2008/08/initial.jpg"><img class="alignnone size-medium wp-image-36" src="http://ppetrov.files.wordpress.com/2008/08/initial.jpg?w=300&#038;h=108" alt="" width="300" height="108" /></a></p>
<p>When I rename the column from <strong>Name</strong> to <strong>RoomName</strong> the Intellisense isn&#8217;t updated.</p>
<p><a href="http://ppetrov.files.wordpress.com/2008/08/intellisenseincorrect.jpg"><img class="alignnone size-medium wp-image-37" src="http://ppetrov.files.wordpress.com/2008/08/intellisenseincorrect.jpg?w=300&#038;h=91" alt="" width="300" height="91" /></a></p>
<p>As you can see the column name is <strong>RoomName </strong>but Intellisense still proposes <strong>Name</strong>.</p>
<p>The Query editor is also not refreshed as shown above.</p>
<p><a href="http://ppetrov.files.wordpress.com/2008/08/queryeditorincorrect.jpg"><img class="alignnone size-medium wp-image-38" src="http://ppetrov.files.wordpress.com/2008/08/queryeditorincorrect.jpg?w=300&#038;h=81" alt="" width="300" height="81" /></a></p>
<p>It says <em>&#8220;Invalid column name&#8221; </em>when the it&#8217;s perfectly valid. Note that the parse command completed successfully (as expected). The query runs with no problems.</p>
<p>I hope it will be fixed in the final release.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ppetrov.wordpress.com/35/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ppetrov.wordpress.com/35/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/35/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/35/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/35/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=35&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2008/08/08/sql-server-2008-management-studio-rename-column-intellisense/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>

		<media:content url="http://ppetrov.files.wordpress.com/2008/08/initial.jpg?w=300" medium="image" />

		<media:content url="http://ppetrov.files.wordpress.com/2008/08/intellisenseincorrect.jpg?w=300" medium="image" />

		<media:content url="http://ppetrov.files.wordpress.com/2008/08/queryeditorincorrect.jpg?w=300" medium="image" />
	</item>
		<item>
		<title>Usuful methods &#8211; 10 of N &#8211; Dictionary with unique values</title>
		<link>http://ppetrov.wordpress.com/2008/07/04/usuful-methods-9-of-n-dictionary-with-unique-values/</link>
		<comments>http://ppetrov.wordpress.com/2008/07/04/usuful-methods-9-of-n-dictionary-with-unique-values/#comments</comments>
		<pubDate>Fri, 04 Jul 2008 10:24:01 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Dictionary unique values]]></category>
		<category><![CDATA[Key Value mapping]]></category>
		<category><![CDATA[One to One Key Value mapping]]></category>
		<category><![CDATA[Unique keys unique values]]></category>
		<category><![CDATA[Unique values]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=30</guid>
		<description><![CDATA[The .NET Dictionary&#60;TKey, TValue&#62; is a great class. MSDN: The Dictionary&#60;(Of &#60;(TKey, TValue&#62;)&#62;) generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=30&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>The .NET <a href="http://msdn.microsoft.com/en-us/library/xfhwa508.aspx">Dictionary&lt;TKey, TValue&gt;</a> is a great class.</p>
<p>MSDN:</p>
<blockquote><p>The <span class="selflink">Dictionary<span class="cs">&lt;</span><span class="vb">(Of </span><span class="cpp">&lt;</span><span class="nu">(</span>TKey, TValue<span class="cs">&gt;</span><span class="vb">)</span><span class="cpp">&gt;</span><span class="nu">)</span></span> generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the <span class="selflink">Dictionary<span class="cs">&lt;</span><span class="vb">(Of </span><span class="cpp">&lt;</span><span class="nu">(</span>TKey, TValue<span class="cs">&gt;</span><span class="vb">)</span><span class="cpp">&gt;</span><span class="nu">)</span></span> class is implemented as a hash table</p></blockquote>
<p>I&#8217;m using this class very often. I&#8217;m not going to explain when to use a dictionary, an array, a sorted array or a tree or any other data structures &#8211; there is a lot of articles on this subject on the net. Instead I&#8217;ll extend the class and I&#8217;ll constraint the set of values to be unique which will give us a one-to-one mapping.It&#8217;s a very simple thing to do.</p>
<pre class="brush: csharp;">
public class UniqueValuesDictionary&lt;TKey, TValue&gt; : Dictionary&lt;TKey, TValue&gt;
{
    public new void Add(TKey key, TValue value)
    {
        if (this.Values.Count &gt; 0)
        {
            bool hasDuplicates = this.Values.Contains(value);
            if (hasDuplicates)
            {
                throw new ArgumentException(&quot;An element with the same value already exists.&quot;);
            }
        }
        base.Add(key, value);
    }
}
</pre>
<p>And here&#8217;s an example which demonstrates how this class will help us find errors</p>
<pre class="brush: csharp;">
            var lookup = new Dictionary&lt;int, string&gt;();
            lookup.Add(1, &quot;One&quot;);
            lookup.Add(2, &quot;One&quot;);   // No exception as expected

            var uniqueLookup = new UniqueValuesDictionary&lt;int, string&gt;();
            uniqueLookup.Add(1, &quot;One&quot;);
            uniqueLookup.Add(2, &quot;One&quot;); // Here we'll get an exception
</pre>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ppetrov.wordpress.com/30/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ppetrov.wordpress.com/30/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/30/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/30/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/30/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=30&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2008/07/04/usuful-methods-9-of-n-dictionary-with-unique-values/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
		<item>
		<title>Usuful methods &#8211; 9 of N &#8211; Count string occurences</title>
		<link>http://ppetrov.wordpress.com/2008/07/02/usuful-methods-9-of-n-count-occurences/</link>
		<comments>http://ppetrov.wordpress.com/2008/07/02/usuful-methods-9-of-n-count-occurences/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 15:06:26 +0000</pubDate>
		<dc:creator>ppetrov</dc:creator>
				<category><![CDATA[.NET Framework]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[count how many times a string contains another string]]></category>
		<category><![CDATA[Count Occurences]]></category>
		<category><![CDATA[count string]]></category>
		<category><![CDATA[count string occurences]]></category>
		<category><![CDATA[CountOccurences]]></category>

		<guid isPermaLink="false">http://ppetrov.wordpress.com/?p=29</guid>
		<description><![CDATA[One more extension method(hopefully useful) on String.  If we want to count how many times a string contains another string this method will help us. We have two versions &#8211; simple and overlapping. When we use the simple version like this we&#8217;ll have count = 2. When we use the overlapping one on the same [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=29&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>One more extension method(hopefully useful) on <strong>String</strong>.  If we want to count how many times a string contains another string this method will help us.</p>
<pre class="brush: csharp;">
    public static int CountOccurences(this string original, string value)
    {
        return original.CountOccurences(value, StringComparison.CurrentCulture);
    }

    public static int CountOccurences(this string original, string value, StringComparison comparisionType)
    {
        return GenericCountOccurences(original, value, comparisionType, value.Length);
    }

    public static int CountOverlapOccurences(this string original, string value)
    {
        return GenericCountOccurences(original, value, StringComparison.CurrentCulture, 1);
    }

    public static int CountOverlapOccurences(this string original, string value, StringComparison comparisionType)
    {
        return GenericCountOccurences(original, value, comparisionType, 1);
    }

    private static int GenericCountOccurences(string original, string value, StringComparison comparisionType, int step)
    {
        int occurences = 0;

        if (!string.IsNullOrEmpty(original))
        {
            int foundIndex = original.IndexOf(value, 0, comparisionType);
            while (foundIndex &gt;= 0)
            {
                occurences++;
                foundIndex = original.IndexOf(value, foundIndex + step, comparisionType);
            }
        }

        return occurences;
    }
</pre>
<p>We have two versions &#8211; simple and overlapping.<br />
When we use the simple version like this</p>
<pre class="brush: csharp;">
            var input = &quot;aaaa&quot;;
            var count = input.CountOccurences(&quot;aa&quot;);
</pre>
<p>we&#8217;ll have <strong>count = 2</strong>.<br />
When we use the overlapping one on the same input</p>
<pre class="brush: csharp;">
            var input = &quot;aaaa&quot;;
            var count = input.CountOverlapOccurences(&quot;aa&quot;);
</pre>
<p>we&#8217;ll have <strong>count = 3</strong>.</p>
<p>That&#8217;s because the search of the next occurrence begins right after the start of the match and in the other version the search begins after the end of the previous match.</p>
<p><strong>Note</strong>: I&#8217;m sure my colleague and friend <strong>Vlado </strong>will appreciate this <img src='http://s1.wp.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/ppetrov.wordpress.com/29/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/ppetrov.wordpress.com/29/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/ppetrov.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/ppetrov.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/ppetrov.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/ppetrov.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/ppetrov.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/ppetrov.wordpress.com/29/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/ppetrov.wordpress.com/29/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/ppetrov.wordpress.com/29/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=ppetrov.wordpress.com&amp;blog=3677569&amp;post=29&amp;subd=ppetrov&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://ppetrov.wordpress.com/2008/07/02/usuful-methods-9-of-n-count-occurences/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://0.gravatar.com/avatar/6d46748840d472e37549bdc6e3f504f4?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">ppetrov</media:title>
		</media:content>
	</item>
	</channel>
</rss>
