<?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/"
	>

<channel>
	<title>.NET Tips and Tricks &#187; net c#</title>
	<atom:link href="http://kossovsky.net/index.php/tag/net-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://kossovsky.net</link>
	<description>C# Code Snippets, ASP.NET Code Samples, .NET Tips and Tricks</description>
	<lastBuildDate>Sat, 25 Dec 2010 08:32:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Division by zero is simple, isn&#8217;t it ?</title>
		<link>http://kossovsky.net/index.php/2010/04/division-by-zero-is-simple-but-the-result-not/</link>
		<comments>http://kossovsky.net/index.php/2010/04/division-by-zero-is-simple-but-the-result-not/#comments</comments>
		<pubDate>Sun, 04 Apr 2010 19:17:00 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[according to the rules]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[divide]]></category>
		<category><![CDATA[elementary arithmetic]]></category>
		<category><![CDATA[few days]]></category>
		<category><![CDATA[floating point]]></category>
		<category><![CDATA[floating point numbers]]></category>
		<category><![CDATA[infinity]]></category>
		<category><![CDATA[large numbers]]></category>
		<category><![CDATA[microsoft]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[running]]></category>
		<category><![CDATA[ugly bug]]></category>
		<category><![CDATA[visual c#]]></category>
		<category><![CDATA[zero]]></category>
		<category><![CDATA[zero exception]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=834</guid>
		<description><![CDATA[I&#8217;m sure you all know that according to the rules of elementary arithmetic division by zero is not allowed*. Also, I&#8217;m pretty sure you are familiar with the &#8220;Attempted to divide by zero&#8221; exception and some of you even write a specific catch in try&#8230;catch blocks to catch the DivideByZeroException to be able to handle [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (4 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m sure you all know that according to the rules of elementary arithmetic division by zero is not allowed*. Also, I&#8217;m pretty sure you are familiar with the &#8220;Attempted to divide by zero&#8221; exception and some of you even write a specific catch in try&#8230;catch blocks to catch the DivideByZeroException to be able to handle it according to your application needs.</p>
<p><span id="more-834"></span><br />
A few days ago I had to solve some very ugly bug that was caused by exactly the same approach I mentioned.</p>
<p>The code contained some calculation with a very large numbers and some how the results weren&#8217;t close to what we expected.</p>
<p>Let&#8217;s see some code</p>
<pre class="c-sharp" name="code">
            // Example #2
            var firstValue = 3;
            var secondValue = 0;
            var result = firstValue / secondValue;
</pre>
<p> Simple, isn&#8217;t it. Running the above code will throw DivideByZeroException exception as we all expected.</p>
<p><br/><br />
Now look at the following code. Will it behave the same way ?</p>
<pre class="c-sharp" name="code">
            // Example #2
            var firstValue = 3.5;
            var secondValue = 0;
            var result = firstValue / secondValue;
</pre>
<p>As you probably suspected &#8211; it won&#8217;t. It will run and complete without throwing any exception at all.<br />
So, what happened here ? Are we talking about some serious bug Microsoft isn&#8217;t aware of ? </p>
<p>Well, first of all it is not a bug. The difference between the two examples is the type we are trying to divide by zero. In the first example firstValue is an Integer and in the second example is Double.</p>
<p>Does that mean we can divide floating point numbers by zero ? Yes we can.</p>
<p>Now you are probably wondering what will be the the value of result. Well, the answer is &#8211; Infinity. Yes, there is a value called Infinity and to be more specific, in our case the result is a Positive Infinity. The interesting part is because Infinity is a value you can make calculations with it and it won&#8217;t throw any exception. Let&#8217;s see some examples :</p>
<pre class="c-sharp" name="code">
            var i = 3.5 / 0;                // Infinity
            var t1 = i++;                   // Infinity
            var t2 =  i--;                  // Infinity
            var t3 = i/i;                   // NaN
            var t4 = i * i;                 // Infinity
            var t5 = i>0;                   // true
            var t6 = i > double.MaxValue;   // true
            var t7 = double.MaxValue - i;   // -Infinity [ Negative Infinity ]
            var t8 = i-i;                   // NaN
</pre>
<p>So, now we know that calculations using Infinity are possible but in most cases the result won&#8217;t be something you can use for further calculation. By the way, if you will try to insert Infinity into Microsoft SQL field (at least in 2005) it will throw an exception saying &#8220;The supplied value is not a valid instance of data type float&#8221;.</p>
<p>And now i would like to return to the bug i was talking about at the beginning of this post.<br />
What do you think will be the result of the following code.</p>
<pre class="c-sharp" name="code">
            var i = 3.5 / 0;
            var result = (long)i;
</pre>
<p>The result is very numeric, not NaN and not Infinite but -9223372036854775808.<br />
Now imagine situation when you must calculate some numbers that represent money, I mean loans, balance, etc. I&#8217;m sure you can easily understand how far can it go and which unpleasant result we can get.</p>
<p>C# provide us some usefull functions and constants we can and must use to avoid the above mistakes.</p>
<pre class="c-sharp" name="code">
            //  Returns a value indicating whether the specified number
            //  evaluates to negative or positive infinity
            double.IsInfinity(double d) 

            //  Returns a value indicating whether the specified number
            // evaluates to negative infinity.
            double.IsNegativeInfinity (double d)

            //  Returns a value indicating whether the specified number
            //  evaluates to positive infinity.
            double.IsPositiveInfinity (double d)

            //  Represents negative infinity. This field is constant.
            double.NegativeInfinity 

            //  Represents positive infinity. This field is constant.
            double.PositiveInfinity

            //  Represents a value that is not a number (NaN).
            //  This field is constant.
            double.NaN
</pre>
<p>Floating point is necessary in so many cases &#8211; use it wisely and carefully.</p>
<div>
<script type="text/javascript">
var dzone_url = 'http://kossovsky.net/index.php/2010/04/division-by-zero-is-simple-but-the-result-not/';
var dzone_style = '1';
</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (4 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2010%2F04%2Fdivision-by-zero-is-simple-but-the-result-not%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2010/04/division-by-zero-is-simple-but-the-result-not/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>jTemplates &#8211; jQuery Template Engine</title>
		<link>http://kossovsky.net/index.php/2009/10/jtemplates-jquery-template-engine/</link>
		<comments>http://kossovsky.net/index.php/2009/10/jtemplates-jquery-template-engine/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 22:46:51 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[AJAX]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[dhtml]]></category>
		<category><![CDATA[dom template]]></category>
		<category><![CDATA[dynamic template]]></category>
		<category><![CDATA[endless event]]></category>
		<category><![CDATA[engine]]></category>
		<category><![CDATA[event handlers]]></category>
		<category><![CDATA[IE]]></category>
		<category><![CDATA[javascript library]]></category>
		<category><![CDATA[javascript templates]]></category>
		<category><![CDATA[jquery templates]]></category>
		<category><![CDATA[JS]]></category>
		<category><![CDATA[js template]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[rapid web development]]></category>
		<category><![CDATA[render]]></category>
		<category><![CDATA[server side code]]></category>
		<category><![CDATA[side requests]]></category>
		<category><![CDATA[support parameters]]></category>
		<category><![CDATA[template]]></category>
		<category><![CDATA[template engine]]></category>
		<category><![CDATA[templates]]></category>
		<category><![CDATA[time manipulation]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[usable features]]></category>
		<category><![CDATA[web template]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=789</guid>
		<description><![CDATA[jTemplates is a template engine for JavaScript, wich is wraped in a jQuery Plugin.
The syntax is very simple and  intuitive. It's support parameters, includes, if/else, loops, JavaScript functions and many other nice and usable  features.<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.9" /></div><div>Rating: 4.9/<strong>5</strong> (12 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>About a year ago i had to architucture some web application with a very rich client side UI.</p>
<p>ASP.NET was an easy choise for a server side code, but what about client side ?<br />
The heavy viewstate, endless event handlers and infinity number of server side requests just to make the GUI  friendly &#8211; well, it just didn&#8217;t sound as a proper way to do it.</p>
<p><span id="more-789"></span>So, what do we have ?<br />
.NET as a server side platform, but despite  all it&#8217;s powerful abilities it has nothing to do with client side. At the end of the day all those fancy control are just HTML and JavaScript.</p>
<p> JavaScript is still the engine that runs at the client side and it&#8217;s great, but we all know how many lines of code we have to write and time to spend to make it fast, modular and cross browser.</p>
<p><a title="jQuery" href="http://jquery.com/" target="_blank">jQuery</a> &#8211; it&#8217;s a fast and concise JavaScript Library that simplifies HTML document traversing, event handling, animating, and Ajax interactions for rapid web development.</p>
<p>Indeed, we can do a many things with jQuery and it realy simplify the coding, but what if we have to build a product list with complicated HMTL, complex logic and real time manipulation ?</p>
<p>As i mentioned before, ASP.NET and UpdatePanels are just to heavy and JavaScript is to long.</p>
<p>So, i did some research and find a couple of JavaScript Template Engines.<br />
The first one i bumped in was <a href="http://plugins.jquery.com/project/jsrepeater" target="_blank">jsRepeater</a>. At first it seemed like a great solution but after a few tests i found all kinds of bugs related to the syntax conflicts with HTML and JavaScript.</p>
<p>After a few days of googling, tests and googling again i found the one and only &#8211;  <a title="jTemplates" href="http://jtemplates.tpython.com/" target="_blank">jTemplates</a>.<br />
jTemplates is a template engine for JavaScript, wich is wraped in a jQuery Plugin.<br />
The syntax is very simple and  intuitive. It&#8217;s support parameters, includes, if/else, loops, JavaScript functions and many other nice and usable  features.</p>
<p>Let&#8217;s say we want to list all our products. What we have to do is to write the template (HTML), pass the data and we have a product list.</p>
<p>
<div><span style="color: #0000ff;">{#foreach $T.Product as P}</span></div>
<div style="padding-left: 30px;"><span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">div</span> <span style="color: #008000;">style</span>=&#8221;float:left;width:150px;height:150px&#8221;<span style="color: #0000ff;">&gt;</span></div>
<div style="padding-left: 60px;"><span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">div</span><span style="color: #0000ff;">&gt;</span><strong>{$T.P.Title}</strong><span style="color: #0000ff;">&lt;/div&gt;</span></div>
<div style="padding-left: 60px;"><span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">img</span> <span style="color: #008000;">src</span>=&#8221;<strong>{$T.P.Url}</strong>&#8221; <span style="color: #008000;">alt</span>=&#8221;<strong>{$T.P.Title}</strong>&#8221; style=&#8221;width:10px;height:10px&#8221; <span style="color: #0000ff;">/&gt;</span></div>
<div style="padding-left: 60px;"><span style="color: #0000ff;">&lt;hr /&gt;</span></div>
<div style="padding-left: 60px;"><span style="color: #0000ff;">&lt;</span><span style="color: #0000ff;">div</span><span style="color: #0000ff;">&gt;</span>Price : <strong>{$T.P.Price}$</strong><span style="color: #0000ff;">&lt;/div&gt;</span></div>
<div style="padding-left: 30px;"><span style="color: #0000ff;">&lt;/</span><span style="color: #0000ff;">div</span><span style="color: #0000ff;">&gt;</span></div>
<div><span style="color: #0000ff;">{#/for}</span></div>
</p>
<p>Look how easy this is. You just write HTML and werever you need to insert some data you put {$.T.P.ColumnName}.</p>
<p>So, how we use it ?<br />
Just add some textarea to your page, give it some id, set it&#8217;s display style to none and write your template inside.</p>
<pre class="html" name="code">
&lt;textarea cols="0" rows ="0" style="display:none;" id="ProductListTemplate"&gt;&lt;!--
{#foreach $T.Product as P}
	&lt;div style=”float:left;width:150px;height:150px”&gt;
		&lt;div>{$T.P.Title}&lt;/div&gt;
		&lt;img src=”{$T.P.Url}” alt=”{$T.P.Title}” style=”width:10px;height:10px” /&gt;
		&lt;hr /&gt;
		&lt;div>Price : {$T.P.Price}$&lt;/div&gt;
	&lt;/div&gt;
{#/for}
--&gt;&lt;/textarea&gt;
</pre>
<p>Add some empty container that will hold the result.</p>
<pre class="html" name="code">
&lt;div id="ProductListContainer"&gt;&lt;/div&gt;
</pre>
<p>Execute setTemplateElement and execute processTemplate while passing the data into it as a parameter</p>
<pre class="javascript" name="code">
$("#ProductListContainer").setTemplateElement("ProductListTemplate");
$("#ProductListContainer").processTemplate(data);
</pre>
<p>Easy, isn&#8217;t it ?<br />
I must say that the above example is a simple one, but I&#8217;ve been using jTemplates to create complicated lists, grids, trees, popups and even an image editing web application. Use it, I&#8217;m sure you will love it.</p>
<div style="margin-top:-15px;margin-bottom:15px;"><script type="text/javascript">
var dzone_url = 'http://kossovsky.net/index.php/2009/10/jtemplates-jquery-template-engine/';
var dzone_style = '1';
</script><br />
<script language="javascript" src="http://widgets.dzone.com/links/widgets/zoneit.js"></script>
</div>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.9" /></div><div>Rating: 4.9/<strong>5</strong> (12 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F10%2Fjtemplates-jquery-template-engine%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/10/jtemplates-jquery-template-engine/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>C# Set method timeout using Generics</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 18:30:17 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[execution]]></category>
		<category><![CDATA[file ext]]></category>
		<category><![CDATA[func]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[generics]]></category>
		<category><![CDATA[httpwebrequest]]></category>
		<category><![CDATA[ins]]></category>
		<category><![CDATA[limit]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[milliseconds]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[quick solution]]></category>
		<category><![CDATA[remote computer]]></category>
		<category><![CDATA[return result]]></category>
		<category><![CDATA[synchronous]]></category>
		<category><![CDATA[thread]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[timeout]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[unc]]></category>
		<category><![CDATA[unc path]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=687</guid>
		<description><![CDATA[I&#8217;m pretty sure all of you know the WebRequest and it&#8217;s derived class HttpWebRequest. And what a marvelous property both of them have &#8211; the TimeOut. Yesterday I had to write some app that reads files located on some remote computer. As I knew already this ins&#8217;t such a good practice, because your code can [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (18 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m pretty sure all of you know the WebRequest and it&#8217;s derived class HttpWebRequest.<br />
And what a marvelous property both of them have &#8211; the TimeOut.</p>
<p><span id="more-687"></span></p>
<p>Yesterday I had to write some app that reads files located on some remote computer.<br />
As I knew already this ins&#8217;t such a good practice, because your code can just hang/freeze for seconds waiting for that UNC path to become available or just checking it&#8217;s existence.</p>
<p>Still, I had to provide some quick solution and &#8220;hoping for the best&#8221; wasn&#8217;t good enough.<br />
So, this is what I came up with.</p>
<pre class="c-sharp" name="code">
        public static T Limex&lt;T&gt;(Func&lt;T&gt; F, int Timeout, out bool Completed)
        {
            T result = default(T);
            Thread thread = new Thread(() =&gt; result = F());
            thread.Start();
            Completed = thread.Join(Timeout);
            if (!Completed) thread.Abort();
            return result;
        }

        // Overloaded method, for cases when we don't
        // need to know if the method was terminated
        public static T Limex&lt;T&gt;(Func&lt;T&gt; F, int Timeout)
        {
            bool Completed;
            return Limex(F, Timeout, out Completed);
        }
</pre>
<p>The usage is very simple, just pass any method (declared or anonymous) and the desired Timeout in milliseconds to the Limex. </p>
<p>Example :</p>
<pre class="c-sharp" name="code">
bool Completed;
string Content = Limex(() => File.ReadAllText(@"\\unc\dir\file.ext")
                       ,100 // milliseconds
                       ,out Completed);

if (Completed)
   // Do something
else
  // Do something else
</pre>
<p>Comments and suggestions for improvement are welcome and will be gratefully appreciated</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (18 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-how-to-limit-method-execution-time%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-how-to-limit-method-execution-time/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>C# LINQ Teaser</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-linq-teaser/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-linq-teaser/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 09:31:00 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[delegate]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[interview question]]></category>
		<category><![CDATA[lambda]]></category>
		<category><![CDATA[linq]]></category>
		<category><![CDATA[lt]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[puzzles]]></category>
		<category><![CDATA[string args]]></category>
		<category><![CDATA[tezer]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[toarray]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=517</guid>
		<description><![CDATA[Another great interview question. Will the following code compile and if yes what will be the result of it ? static void Main(string[] args) { int[] array = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; Func&#60;int, int&#62; func = i =&#62; { Console.Write(array[i]); return i; }; var result = array.Where(e =&#62; [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.8" /></div><div>Rating: 4.8/<strong>5</strong> (9 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Another great interview question.</p>
<p>Will the following code compile and if yes what will be the result of it ?<br />
<span id="more-517"></span></p>
<pre class="c#" name="code">static void Main(string[] args)
{
    int[] array = new[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    Func&lt;int, int&gt; func = i =&gt; {
                          Console.Write(array[i]);
                          return i;
                     };

    var result = array.Where(e =&gt; e &lt;= func(2)).ToArray ();
}</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.8" /></div><div>Rating: 4.8/<strong>5</strong> (9 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-linq-teaser%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-linq-teaser/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# Byte Iteration Question</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-byte-iteration-question/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-byte-iteration-question/#comments</comments>
		<pubDate>Fri, 10 Jul 2009 08:27:34 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[byte]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[interview question]]></category>
		<category><![CDATA[iteration]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[string args]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=473</guid>
		<description><![CDATA[This one is a great c# interview question &#8211; short and simple, but tricky. static void Main(string[] args) { for (byte b = byte.MinValue; b<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.4" /></div><div>Rating: 4.4/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This one is a great c# interview question &#8211; short and simple, but tricky.</p>
<p><span id="more-473"></span></p>
<pre class="c-sharp" name="code">
static void Main(string[] args)
{
    for (byte b = byte.MinValue; b <= byte.MaxValue; b++)
        Console.Write(b);
}

// A little tip
// byte.MinValue = 0
// byte.MaxValue = 255
</pre>
<p>How many times the above statement will be executed ?</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.4" /></div><div>Rating: 4.4/<strong>5</strong> (5 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-byte-iteration-question%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-byte-iteration-question/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>C# Image Processing with AForge.NET Framework</title>
		<link>http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/</link>
		<comments>http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 21:35:20 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[computer vision library]]></category>
		<category><![CDATA[genetic evolution]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[grayscale image]]></category>
		<category><![CDATA[grayscale images]]></category>
		<category><![CDATA[Image]]></category>
		<category><![CDATA[image edges]]></category>
		<category><![CDATA[imaging library]]></category>
		<category><![CDATA[Jpg]]></category>
		<category><![CDATA[machine learning library]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[Quality]]></category>
		<category><![CDATA[Resize]]></category>
		<category><![CDATA[Resizer]]></category>
		<category><![CDATA[robotics kits]]></category>
		<category><![CDATA[robotics library]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[UI]]></category>
		<category><![CDATA[vision computer]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=436</guid>
		<description><![CDATA[AForge.NET is a C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence - image processing, neural networks, genetic algorithms, machine learning, robotics, etc.<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (8 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>A couple of days ago i found a great framework called AForge.NET.</p>
<p><a title="AForge.NET" href="http://www.aforgenet.com/framework/" target="_blank">AForge.NET</a> is a C# framework designed for developers and researchers in the fields of Computer Vision and Artificial Intelligence &#8211; image processing, neural networks, genetic algorithms, machine learning, robotics, etc.</p>
<p><span id="more-436"></span></p>
<p>The framework is published under <a title="GNU LESSER GENERAL PUBLIC LICENSE" href="http://www.gnu.org/licenses/lgpl.html" target="_blank">LGPL v3 license</a> and comprised by the set of libraries and sample applications, which demonstrate their features:</p>
<ul>
<li>AForge.Imaging &#8211; library with image processing routines and filters</li>
<li>AForge.Vision &#8211; computer vision library</li>
<li>AForge.Neuro &#8211; neural networks computation library</li>
<li>AForge.Genetic &#8211; evolution programming library</li>
<li>AForge.MachineLearning &#8211; machine learning library</li>
<li>AForge.Robotics &#8211; library providing support of some robotics kits</li>
<li>AForge.Video &#8211; set of libraries for video processing</li>
</ul>
<p>The framework is provided not only with different libraries and their sources, but with many sample applications, which demonstrate the use of this framework, and with documentation help files, which are provided in HTML Help format.</p>
<p>As you can see this framework contains varied libraries, but as for me i was especially interested in the image processing routines and filters.</p>
<p>Let&#8217;s see  the  CannyEdgeDetector in action.</p>
<pre class="c-sharp" name="code">using AForge.Imaging.Filters;

// Loading some file
using (Bitmap SampleImage = (Bitmap)Image.FromFile("test.jpg"))
{
    // We must convert it to grayscale because
    // the filter accepts 8 bpp grayscale images
    Grayscale GF = new Grayscale(0.2125, 0.7154, 0.0721);
    using (Bitmap GSampleImage = GF.Apply(SampleImage))
    {
        // Saving the grayscale image, so we could see it later
        GSampleImage.SaveAsJpeg("testBW.jpg", 100);

        // Detecting image edges and saving the result
        CannyEdgeDetector CED = new CannyEdgeDetector(0,70);
        CED.ApplyInPlace(GSampleImage);
        GSampleImage.SaveAsJpeg("testEDGED.jpg",100);
    }
}

// SaveAsJpeg is an extension method i wrote to be able to save
// Jpeg images with parametrized quality
// You can use Image.Save ("image.jpg", ImageFormat.Jpeg) instead
</pre>
<p>Now, let see the results ( click the image to see full size )</p>

<a href='http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/aforgesamples/' title='The original image [ AForge Test ]'><img width="150" height="150" src="http://kossovsky.net/wp-content/uploads/2009/07/AForgeSamples-150x150.jpg" class="attachment-thumbnail" alt="The original image" title="The original image [ AForge Test ]" /></a>
<a href='http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/aforgesamplesgrayscale/' title='Grayscaled image [ AForge Test ]'><img width="150" height="150" src="http://kossovsky.net/wp-content/uploads/2009/07/AForgeSamplesGrayscale-150x150.jpg" class="attachment-thumbnail" alt="Grayscaled image" title="Grayscaled image [ AForge Test ]" /></a>
<a href='http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/aforgesamplesresult/' title='Edged image [ AForge Test ]'><img width="150" height="150" src="http://kossovsky.net/wp-content/uploads/2009/07/AForgeSamplesResult-150x150.jpg" class="attachment-thumbnail" alt="Edged image" title="Edged image [ AForge Test ]" /></a>

<p>The result is great, and of course we can modify the threshold levels to gain even better results.<br />
As for other filter and effects there are many of them in this library.<br />
There are Noise generators, color correction filters, smoothing filters and <a title="AForge.Imaging filters and effects" href="http://www.aforgenet.com/framework/features/" target="_blank">more</a>.</p>
<p>Sure, it&#8217;s no Photoshop, but i must say it&#8217;s very close to it.</p>
<p>Hope you will find this useful, I did.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.6" /></div><div>Rating: 4.6/<strong>5</strong> (8 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Faforge-net-image-processing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/aforge-net-image-processing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C# Rectangle Packing</title>
		<link>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/</link>
		<comments>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 12:45:48 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Graphics]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[ascii character]]></category>
		<category><![CDATA[bitmap fonts]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[common interface]]></category>
		<category><![CDATA[cygon]]></category>
		<category><![CDATA[Gif]]></category>
		<category><![CDATA[humble webmaster]]></category>
		<category><![CDATA[lightmaps]]></category>
		<category><![CDATA[linear increase]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[Png]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[space efficiency]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[suitable position]]></category>
		<category><![CDATA[support assembly]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=414</guid>
		<description><![CDATA[Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Sometimes, you&#8217;re faced with the problem to cramp as many smaller textures as possible onto a larger texture. Typical cases are lightmaps, which are very small textures with dimensions that usually are not powers of two, or bitmap fonts where you want to try and fit the entire ascii character set onto a texture without wasting much space.</p>
<p>What you need then, is a rectangle packer, an algorithm that arranges as many smaller rectangles on a larger rectangle as can possibly fit.</p>
<p><span id="more-414"></span></p>
<p>The <a title="Nuclex - Rectangle Packing" href="http://www.nuclex.org/pages/framework/rectangle-packing" target="_blank">Nuclex.Support</a> assembly contains several rectangle packing algorithms in the Nuclex.Support.Packing namespace offering various levels of compromises between space efficiency and runtime performance. All of these algorithms have been implemented as classes with a common interface, so, once implemented, you can easily switch forth and back between different algorithms to find the one best suites for your purpose.</p>
<p>Currently, there are three different packing algorithms for you to choose:</p>
<table style="width: 100%; border: none;" border="0" cellspacing="2" cellpadding="0">
<tbody>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="The Simple Packer" src="http://kossovsky.net/wp-content/uploads/2009/07/simple-rectangle-packer.png" alt="The Simple Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Simple Packer</strong><br />
This packer is optimized for runtime performance. It does sacrifice a bit of space, but the time needed to find a position for a new rectangle is O(1). In english, that means it doesn&#8217;t take longer to search for a suitable position, no matter how many rectangles have been added to the packing area.</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="cygon-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/cygon-rectangle-packer.png" alt="The Cygon Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Cygon Packer</strong><br />
Named after your humble webmaster, this algorithm is highly efficient in its space usage and offers reasonable performance. It will never exceed O(n) time but generally achieves almost O(1) on average. English translation: Search time is usually constant and guaranteed to never exceed a linear increase (so when you&#8217;ve got 99 rectangles already packed and add the 100th one, it guarantees that search time will at most be 1% longer than the time taken for the previous rectangle).</td>
</tr>
<tr>
<td style="border:none;padding-bottom:20px;" align="center" valign="top"><img style="padding: 0px; margin: 0px; border: none;" title="arevalo-rectangle-packer" src="http://kossovsky.net/wp-content/uploads/2009/07/arevalo-rectangle-packer.png" alt="The Arevalo Packer" width="128" height="128" /></td>
<td style="border:none;padding-bottom:20px;" valign="top"><strong>The Arevalo Packer</strong><br />
Named after Javier Arevalo, who posted his implementation of this packer on flipcode back in the golden times. This algorithm offers very good space efficiency and runs in slightly less then O(n) time. Just to be consistent, in english, that means, the time needed to find a suitable place for a new rectangle will only increase linearly.</td>
</tr>
</tbody>
</table>
<p>You can download the C# version of the algorithms from <a title="Nuclex Framework - C# Source Code" href="https://devel.nuclex.org/framework/browser/game/Nuclex.Game/trunk/Source/Packing" target="_blank">here</a>.</p>
<p>The usage is very simple :</p>
<pre class="c-sharp" name="code">
	Point PP;
	var ARP = new ArevaloRectanglePacker(200, 200);

	if(!ARP.TryPack(Width, Height, out PP)) {
		// Do something
	}
	else {
		// Do something else
	}
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcshar-rectangle-packing%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/cshar-rectangle-packing/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>SpeedTrace &#8211; .NET Profiler and Tracer</title>
		<link>http://kossovsky.net/index.php/2009/07/speedtrace-net-profiler-and-tracer/</link>
		<comments>http://kossovsky.net/index.php/2009/07/speedtrace-net-profiler-and-tracer/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 11:28:30 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Profiling Tools]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[QA]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[data flow]]></category>
		<category><![CDATA[dot net framework]]></category>
		<category><![CDATA[dot net framework 2]]></category>
		<category><![CDATA[flow problems]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[peak times]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[performance bottlenecks]]></category>
		<category><![CDATA[profiler]]></category>
		<category><![CDATA[profilers]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[program flow]]></category>
		<category><![CDATA[return values]]></category>
		<category><![CDATA[Speed]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=406</guid>
		<description><![CDATA[It is very important to be able to dynamically control the behavior of .NET applications and to keep track of some of the aspects of the application (i.e. how the application is performing, what errors are produced during runtime, how the application performs at peak times, how to dynamically alter the behavior of the application, [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.7" /></div><div>Rating: 4.7/<strong>5</strong> (7 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>It is very important to be able to dynamically control the behavior of .NET applications and to keep track of some of the aspects of the application (i.e. how the application is performing, what errors are produced during runtime, how the application performs at peak times, how to dynamically alter the behavior of the application, etc).</p>
<p><span id="more-406"></span></p>
<p>The <a title="SpeedTrace - .NET Profiler and Tracer" href="http://www.ipcas.com/trace-and-profile/c-sharp-and-vb.net-tracer-and-profiler/" target="_blank">SpeedTrace Pro</a> profiler follows and records the program flow of any .NET application on a function/method level for later profiling or tracing analysis, allowing you to identify performance bottlenecks, deadlocks, software design problems as well as resource and data flow problems.</p>
<p>One of the nicest features in this application is the presence of  &#8220;Callback-API&#8221;.<br />
It provides user-extended functionality to allow developers to adapt the tracer to their needs and to write user-specific extensions for dot.NET Framework 2.0, 3.0 or 3.5 applications.</p>
<p>SpeedTrace Pro can make accessible the arguments and return values of a function. This outstanding feature enables you not only to trace function calls but also to obtain the values passed or returned &#8211; important data that normal profilers lose during aggregations. You can make use of this data to obtain the relevant information, especially when bugs appear under certain conditions.</p>
<p>The data tracing values of .NET applications may be used to trap bugs easily and to perform data-dependent analyses.</p>
<div id="attachment_408" class="wp-caption alignnone" style="width: 310px"><img class="size-medium wp-image-408" title="dotnet-tracer-and-profiler" src="http://kossovsky.net/wp-content/uploads/2009/07/dotnet-tracer-and-profiler-300x224.png" alt="SpeedTrace - .NET Profiler and Tracer" width="300" height="224" /><p class="wp-caption-text">SpeedTrace - .NET Profiler and Tracer</p></div>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.7" /></div><div>Rating: 4.7/<strong>5</strong> (7 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fspeedtrace-net-profiler-and-tracer%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/speedtrace-net-profiler-and-tracer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>C# The &#8220;double&#8221; trouble</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/#comments</comments>
		<pubDate>Mon, 06 Jul 2009 21:37:16 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[QA]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[c-sharp]]></category>
		<category><![CDATA[d1 d2]]></category>
		<category><![CDATA[digits]]></category>
		<category><![CDATA[double]]></category>
		<category><![CDATA[double trouble]]></category>
		<category><![CDATA[floating]]></category>
		<category><![CDATA[floating point]]></category>
		<category><![CDATA[guarantees]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[numeric value]]></category>
		<category><![CDATA[point]]></category>
		<category><![CDATA[precision]]></category>
		<category><![CDATA[Puzzle]]></category>
		<category><![CDATA[simple answer]]></category>
		<category><![CDATA[string args]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=360</guid>
		<description><![CDATA[This is a very simple question with a not so simple answer&#8230; Forget for a second about .NET. If someone asks you how much is 1.000025 &#8211; 0.000025 your answer will probably be &#8220;1&#8243; and correct. An easy question, right ? Now, let&#8217;s go back to .NET and check if our calculations are correct. static [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>This is a very simple question with a not so simple answer&#8230;</p>
<p>Forget for a second about .NET.<br />
If someone asks you how much is 1.000025 &#8211; 0.000025 your answer will probably be &#8220;1&#8243; and correct. An easy question, right ?</p>
<p>Now, let&#8217;s go back to .NET and check if our calculations are correct.</p>
<p><span id="more-360"></span></p>
<pre class="c-sharp" name="code">        static void Main(string[] args)
        {
            double d1 = 1.000025;
            double d2 = 0.000025;
            Console.WriteLine((d1 - d2) == 1);
        }</pre>
<p>If you think the result will be &#8220;true&#8221;&#8230; well, it&#8217;s not.</p>
<p>Let&#8217;s see another example :</p>
<pre class="c-sharp" name="code">        static void Main(string[] args)
        {
            double d1 = 1.000025;
            double d2 = 0.000025;

            // Will result 1
            Console.WriteLine(d1 - d2);

            // Will result 0.99999999999999989
            Console.WriteLine((d1 - d2).ToString ("R"));
        }</pre>
<p>For those who might not know, the meaning of ToString(&#8220;R&#8221;) is <a title="MSDN - Standard Numeric Format Strings" href="http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx" target="_blank">Round-trip</a>.</p>
<blockquote><p>This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.</p></blockquote>
<p>So, what happened here ? Is it a bug ?<br />
The answer is &#8211; NO, IT&#8217;S NOT A BUG ( and it&#8217;s not happening only on my computer ).</p>
<p>The thing is, that the above is about how .NET rounding a floating point number.<br />
I won&#8217;t explain here the math ( there is already a great <a title="What Every Computer Scientist Should Know About Floating-Point Arithmetic" href="http://docs.sun.com/source/806-3568/ncg_goldberg.html" target="_blank">article</a> explaining it ), and i won&#8217;t say don&#8217;t use floating point types.</p>
<p>You just need to know that when you are using it &#8211; be carefull, especialy in equations and formatting, otherwise your calculations will be wrong and your result not accurate.</p>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=4.3" /></div><div>Rating: 4.3/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-the-double-trouble%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-the-double-trouble/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C# Increment operator (++) question</title>
		<link>http://kossovsky.net/index.php/2009/07/csharp-increment-operator-question/</link>
		<comments>http://kossovsky.net/index.php/2009/07/csharp-increment-operator-question/#comments</comments>
		<pubDate>Tue, 30 Jun 2009 22:30:32 +0000</pubDate>
		<dc:creator>Xander</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Interview Questions]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Teasers]]></category>
		<category><![CDATA[Tips & Tricks]]></category>
		<category><![CDATA[bug]]></category>
		<category><![CDATA[bugs]]></category>
		<category><![CDATA[c#.net]]></category>
		<category><![CDATA[dot net]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[increment]]></category>
		<category><![CDATA[increment operator]]></category>
		<category><![CDATA[interview]]></category>
		<category><![CDATA[main string]]></category>
		<category><![CDATA[net c#]]></category>
		<category><![CDATA[operators]]></category>
		<category><![CDATA[puzzles]]></category>
		<category><![CDATA[Tips and Tricks]]></category>
		<category><![CDATA[visual c#]]></category>

		<guid isPermaLink="false">http://kossovsky.net/?p=293</guid>
		<description><![CDATA[Well, this is a nice one. We all familiar with incremental operators, but from what i saw yesterday&#8230; well, there is no need to give a developer some tricky assignment so he could find a way to make some bugs. 3bj4h786fc What will be the output of the next code ? static void Main(string[] args) [...]<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />]]></description>
			<content:encoded><![CDATA[<p>Well, this is a nice one. We all familiar with incremental operators, but from what i saw yesterday&#8230; well, there is no need to give a developer some tricky assignment so he could find a way to make some bugs.</p>
<div style="display:none">3bj4h786fc</div>
<p><span id="more-293"></span><br />
What will be the output of the next code ?</p>
<pre class="c-sharp" name="code">
static void Main(string[] args)
{
    int i = 100;
    for (int n = 0; n < 100; n++)
    {
         i = i++;
    }
    Console.WriteLine(i);
}
</pre>
<br /><div><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx.php?value=5.0" /></div><div>Rating: 5.0/<strong>5</strong> (6 votes cast)</div><br /><a target="_blank" href="http://www.gdstarrating.com/"><img src="http://kossovsky.net/wp-content/plugins/gd-star-rating/gfx/powered.png" border="0" width="80" height="15" /></a><br />
<p class="FacebookLikeButton"><fb:like href="http%3A%2F%2Fkossovsky.net%2Findex.php%2F2009%2F07%2Fcsharp-increment-operator-question%2F" layout="standard" show_faces="false" width="450" action="like" colorscheme="light"></fb:like></p>
]]></content:encoded>
			<wfw:commentRss>http://kossovsky.net/index.php/2009/07/csharp-increment-operator-question/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

