<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ToddHa's WebLog : C#</title><link>http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx</link><description>Tags: C#</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>When Fixes Cause Problems</title><link>http://blogs.msdn.com/toddha/archive/2007/11/30/when-fixes-cause-problems.aspx</link><pubDate>Fri, 30 Nov 2007 04:46:41 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:6606420</guid><dc:creator>toddha</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/toddha/comments/6606420.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=6606420</wfw:commentRss><description>&lt;p&gt;&lt;em&gt;Note that there are tons of stories about backwards compatibility; for some of the best go see &lt;/em&gt;&lt;a href="http://blogs.msdn.com/oldnewthing/search.aspx?q=backwards+compat&amp;amp;p=1"&gt;&lt;em&gt;Raymond&lt;/em&gt;&lt;/a&gt;&lt;em&gt;.&lt;/em&gt;&lt;/p&gt;  &lt;p&gt;For a sample application that I'm writing, I'm using .NET CF 3.5. I had upgraded from the version shipped with Visual Studio 2008 Beta2 to the one shipped in VS2k8 RC1, and I hadn't had any problems. Take note, because if you've written some buggy code and are unaware of it, this may affect you, too (perhaps even coming from .NET CF 2.0 code).&lt;/p&gt;  &lt;p&gt;This morning, I upgraded from VS2k8 RC1 to VS2k8 RTM and all of a sudden, none of my network requests were working -- they ALL timed out.&lt;/p&gt;  &lt;p&gt;I kind of freaked out for a bit, thinking that it &lt;strong&gt;couldn't&lt;/strong&gt; have been my code -- I didn't change anything! Plus, as a developer, I've learned the hard and fast rule that it's &lt;strong&gt;never &lt;/strong&gt;my code -- it's always somebody else's. :) However, I was on a new machine, so I hadn't set up ActiveSync to my emulator. I convinced myself that AS was having issues and my PocketPC phone wasn't getting good service.&lt;/p&gt;  &lt;p&gt;An hour or so later, I realized it was my code.&lt;/p&gt;  &lt;p&gt;In my app, I have a rudimentary &lt;a href="http://labs.biztalk.net/connectivity.aspx"&gt;BizTalk Connectivity Services&lt;/a&gt; aware engine (although isn't specific, as it uses simple HTTP PUT/GET/POST) which takes a URL. This URL is given by the user, so there has to be some checking done on it. Mostly, this is done for me by the framework, but the one key addition which I added was checking to ensure that the URL had a trailing '/' at the end.&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Thus, &lt;/p&gt;  &lt;p&gt;&lt;code&gt;http://connect.biztalk.net/services/username/service&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;turns into&lt;/p&gt;  &lt;p&gt;&lt;code&gt;http://connect.biztalk.net/services/username/service/&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Then, internally my operations all have corresponding URL I should append to the end. However, the bug in my code was that all of the URL operations START with a forward slash. So after I glued the different pieces of the URL together&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;&lt;code&gt;&amp;quot;http://connect.biztalk.net/services/username/service/&amp;quot; + &amp;quot;/Operation&amp;quot;&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;I would get:&lt;/p&gt;  &lt;p&gt;&lt;code&gt;http://connect.biztalk.net/services/username/service//Operation&lt;/code&gt;&lt;/p&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;Apparently in the older versions, this would work fine. But in the version shipping with VS2k8 RTM, it won't work. Simple fix, but caused havok with me for a while. Fortunately, I hadn't released it, so it was easy enough to catch and fix.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=6606420" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/toddha/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Programming/default.aspx">Programming</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Windows+Mobile/default.aspx">Windows Mobile</category><category domain="http://blogs.msdn.com/toddha/archive/tags/.NET+Compact+Framework/default.aspx">.NET Compact Framework</category></item><item><title>Performance Tip-O-The-Day #1 : for[each]</title><link>http://blogs.msdn.com/toddha/archive/2007/07/05/performance-tip-o-the-day-1-for-each.aspx</link><pubDate>Thu, 05 Jul 2007 21:29:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:3710347</guid><dc:creator>toddha</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/toddha/comments/3710347.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=3710347</wfw:commentRss><description>&lt;p&gt;This starts a new series (I hope) of performance tips that you may/may not know about.&lt;/p&gt; &lt;p&gt;This post is brought to you by &lt;a href="http://www.microsoft.com/MSPress/books/10294.aspx"&gt;Microsoft Mobile Development Handbook&lt;/a&gt;, in which I found this gem. It's on page 217 at the bottom if you have the book.&lt;/p&gt; &lt;p&gt;The basic idea is that &lt;em&gt;foreach&lt;/em&gt; is slower than&amp;nbsp;a &lt;em&gt;for&lt;/em&gt; loop. I've pretty much always known this, but never really known how much slower it is. There's some overhead, sure, but I've always assumed it was negligible.&lt;/p&gt; &lt;p&gt;For this demonstration, I'm going to be targeting the full .NET framework as opposed to the compact framework just so I can take advantage of a couple classes that aren't available in the compact framework, but the idea applies to both.&lt;/p&gt; &lt;p&gt;So here's the basic test code.&lt;/p&gt; &lt;p&gt;&lt;code&gt;void Test(List&amp;lt;int&amp;gt; data)&lt;br&gt;&lt;/code&gt;&lt;code&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; TimeSpan start, end;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/code&gt;&lt;code&gt;int total, max, milliseconds;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Process p = Process.GetCurrentProcess();&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; total = 0;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; start = p.TotalProcessorTime;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // start test&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (int i in data)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; total += i;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // end&amp;nbsp;test&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/code&gt;&lt;code&gt;end = p.TotalProcessorTime;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; milliseconds = end.Subtract(start).TotalMilliseconds;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("foreach : " +&amp;nbsp;milliseconds + " ms");&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; total = 0;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; max = data.Count;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; start = p.TotalProcessorTime;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // start test&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int ctr = 0; ctr &amp;lt; max; ctr++)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; total += data[ctr];&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;/code&gt;&lt;code&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // end&amp;nbsp;test&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end = p.TotalProcessorTime;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; milliseconds = end.Subtract(start).TotalMilliseconds;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("foreach : " +&amp;nbsp;milliseconds + " ms");&lt;br&gt;} &lt;/code&gt;&lt;/p&gt; &lt;p&gt;This code just takes a list of integers and then does a foreach loop and a for loop, calculating (roughly) the time for each.&lt;/p&gt; &lt;p&gt;Next, on to the driver code.&lt;/p&gt; &lt;p&gt;&lt;code&gt;// 100,000,000&lt;br&gt;int DataSetSize = 100000000;&lt;br&gt;int Runs = 10;  &lt;p&gt;try&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; List&amp;lt;int&amp;gt; dataset = new List&amp;lt;int&amp;gt;(DataSetSize);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; DataSetSize; i++)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; dataset.Add(i);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for (int i = 0; i &amp;lt; Runs; i++)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Test(dataset);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;br&gt;catch (OutOfMemoryException)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine("Please shrink DataSetSize.");&lt;br&gt;}&lt;/p&gt; &lt;p&gt;&lt;/code&gt;I arbitrarily decided to run this&amp;nbsp;5 times. The first time is always slower, and that's expected. The OutOfMemoryException is in there because I made the data set a little bit too big sometimes and kept on crashing the process harshly.&lt;/p&gt; &lt;p&gt;Anyways, the results (in Release mode) are :&lt;/p&gt; &lt;p&gt; &lt;table&gt; &lt;tbody&gt; &lt;tr&gt; &lt;td width="300"&gt;&lt;/td&gt; &lt;td width="300"&gt;&lt;b&gt;foreach loop&lt;/b&gt;&lt;/td&gt; &lt;td width="300"&gt;&lt;b&gt;for loop&lt;/b&gt;&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Test Run #1&lt;/b&gt;&lt;/td&gt; &lt;td&gt;1250&lt;/td&gt; &lt;td&gt;187.5&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Test Run #2&lt;/b&gt;&lt;/td&gt; &lt;td&gt;984.375&lt;/td&gt; &lt;td&gt;187.5&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Test Run #3&lt;/b&gt;&lt;/td&gt; &lt;td&gt;765.625&lt;/td&gt; &lt;td&gt;218.75&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Test Run #4&lt;/b&gt;&lt;/td&gt; &lt;td&gt;828.125&lt;/td&gt; &lt;td&gt;203.125&lt;/td&gt;&lt;/tr&gt; &lt;tr&gt; &lt;td&gt;&lt;b&gt;Test Run #5&lt;/b&gt;&lt;/td&gt; &lt;td&gt;765.625&lt;/td&gt; &lt;td&gt;218.75&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/p&gt; &lt;p&gt;All times are in milliseconds and are on my arbitrary workstation (nothing special on it : Vista, Visual Studio 2005). Note that the for loop can be 3 times faster than the foreach. Obviously, it depends how many items you're iterating over, but unless you really have to (i.e. the class only provides an iterator), I'd suggest not using the foreach.&lt;/p&gt; &lt;p&gt;As always, thoughts, comments, and questions are always welcome.&lt;/p&gt; &lt;p&gt;&lt;em&gt;&lt;strong&gt;Edit &lt;/strong&gt;(2007-07-06&amp;nbsp;11:18am PST) : Minor code fix, it's always a good idea to pass an initial size to Lists, among others.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=3710347" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/toddha/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Assumptions are the mother of all (part 2).</title><link>http://blogs.msdn.com/toddha/archive/2007/04/26/assumptions-are-the-mother-of-all-part-2.aspx</link><pubDate>Thu, 26 Apr 2007 04:33:31 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2278032</guid><dc:creator>toddha</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/toddha/comments/2278032.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=2278032</wfw:commentRss><description>&lt;p&gt;In my own time, I've been learning Objective-C for fun. I'm not sure why, perhaps I didn't feel I spent &lt;strong&gt;enough&lt;/strong&gt; time in front of a computer. But I digress.&lt;/p&gt; &lt;p&gt;One of the interesting things I've learned is that the following code works.&lt;/p&gt; &lt;p&gt;Note that I assume we have a class named &lt;em&gt;ArbitraryObject&lt;/em&gt;, and that it has a method that takes no parameters entitled &lt;em&gt;foo&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Objective-C&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;code&gt;ArbitraryObject * obj;&lt;br&gt;obj = nil;&lt;br&gt;[obj foo];&lt;/code&gt;&lt;/p&gt; &lt;p&gt;Interesting, this code doesn't crash. In comparison, the following languages all crash when you try to do the equivalent.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;C#&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;code&gt;ArbitraryObject obj = null;&lt;br&gt;obj.foo();&lt;/code&gt;&lt;/p&gt; &lt;p&gt;This is the easy one. We get a &lt;em&gt;NullReferenceException&lt;/em&gt; thrown.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Java&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;code&gt;ArbitraryObject obj = null;&lt;br&gt;obj.foo();&lt;/code&gt;&lt;/p&gt; &lt;p&gt;This is another easy one. We get a &lt;em&gt;NullPointerException&lt;/em&gt; thrown.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Python&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;code&gt;obj = None&lt;br&gt;obj.foo()&lt;/code&gt;&lt;/p&gt; &lt;p&gt;Since we don't have to type what &lt;em&gt;obj&lt;/em&gt; is, we get an &lt;em&gt;AttributeError&lt;/em&gt; talking about how the &lt;em&gt;NoneType&lt;/em&gt; object doesn't have an attribute of &lt;em&gt;foo&lt;/em&gt;. Not exactly the same as before, but close enough.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;&lt;/strong&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;&lt;strong&gt;C++&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;&lt;code&gt;ArbitraryObject * obj = 0;&lt;br&gt;obj-&amp;gt;foo();&lt;/code&gt;&lt;/p&gt; &lt;p&gt;Note that this isn't the entire code required to get the program to crash (and I'm not sure what spurred me to prove to myself that it wouldn't work). The MS VC++ compiler was optimizing my defects away in different ways. I had to add an instance variable to the ArbitraryObject class, assign it in the constructor, and have foo return the variable. Then in the calling code I printf'd it. But I'm ignoring this for simplicity.&lt;/p&gt; &lt;p&gt;But regardless, it's an access violation error.&lt;/p&gt; &lt;p&gt;&amp;nbsp;&lt;/p&gt; &lt;p&gt;What we've proved is that Objective-C works very differently from how other programming languages work. There are some pros and cons of this, depending on your perspective. It's just important to know how things work.&lt;/p&gt; &lt;p&gt;A couple notes : &lt;/p&gt; &lt;ol&gt; &lt;li&gt;I use&amp;nbsp;&lt;em&gt;nil&lt;/em&gt;, &lt;em&gt;null&lt;/em&gt;, &lt;em&gt;0&lt;/em&gt;, and &lt;em&gt;None&lt;/em&gt; interchangeably.&amp;nbsp;  &lt;li&gt;I'm assuming the compiler hasn't optimized your (possibly intentional) errors away.  &lt;li&gt;I really have nothing against any programming language or technology, so please don't assume that I'm some [arbitrary technology/programming langauge] basher or fanboy (even though Objective-C is primarily an Apple&amp;nbsp;language, and Apple is a competitor to Microsoft).&amp;nbsp;This is just another example of where you might think things work one way, and work a different way completely. &lt;li&gt;I find this particular example fascinating.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;Comparing Objective-C performance to C#, Java, and Python isn't really fair as they all have to be translated into machine language at runtime.&lt;/p&gt; &lt;p&gt;However the C++ comparison&amp;nbsp;is fair. What I'm assuming is happening in Objective-C (without diving into&amp;nbsp;PPC assembly) is that there's a check before each function call that says, "Hey, if this address is nil then jump over this block of code that sets up for and executes the&amp;nbsp;function call." So assumedly you have a some extra instructions every time you make a function call. Not a big deal, especially these days where we have a bazillion clock cycles per second, but definitely something that you need to think about if you're trying to squeeze every cycle out of the processor.&lt;/p&gt; &lt;p&gt;&lt;strong&gt;Program Flow&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;This is the main root problems I would assume. I'm going to use C# to flesh out the code, but you can easily translate it to Objective-C. More on this after the code.&lt;/p&gt; &lt;p&gt;Let's go back to our example code and flesh it out a bit in order to prove a point.&lt;/p&gt; &lt;p&gt;&lt;code&gt;public class ArrayWrapper&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;private&amp;nbsp;static int [] array = new int[] { 1, 2, 3, 4 };&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private static int index = 10;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public static SetIndex(int index)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ArrayWrapper.index = index;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public int GetIndexedValue()&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // note that this crashes&amp;nbsp;if index is the default value&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return ArrayWrapper.array[ArrayWrapper.index];&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;}&lt;br&gt;&lt;br&gt;public class ArbitraryObject&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; //&amp;nbsp;we don't have to define an empty constructor, but will anyways!&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public ArbitraryObject() { }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; public void foo()&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ArrayWrapper.SetIndex(2);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/code&gt;&lt;code&gt;&lt;br&gt;}&lt;/code&gt;&lt;/p&gt; &lt;p&gt;And now for the driver code function.&lt;/p&gt; &lt;p&gt;&lt;code&gt;public void DriverFunction(ArbitraryObject obj)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; obj.foo();&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; // here we have assumed that obj.foo() did it's job and set the&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// index of the ArrayWrapper to a valid value&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; Console.WriteLine(ArrayWrapper.GetIndexedValue().ToString());&lt;br&gt;}&lt;/code&gt;&lt;/p&gt; &lt;p&gt;Ok, so this seems like a really stupid example, but it's point is to show that you're assuming that the ArbitraryObject class worked in order for the rest of the function to work.&lt;/p&gt; &lt;p&gt;Let's run some tests. These are based on whether or not obj.foo( ) worked. I'm not going to translate this to Objective-C, but you can easily see what happens.&lt;/p&gt; &lt;p&gt;C# Tests&lt;br&gt;Valid instance of Arbitrary Object : Wrote out "3" to the console.&lt;br&gt;Null reference : NullReference exception at line &lt;em&gt;xxxx&lt;/em&gt;.&lt;br&gt;&lt;br&gt;Objective-C Tests&lt;br&gt;Valid instance of Arbitrary Object : Wrote out "3" to the console.&lt;br&gt;Null reference : Out of bounds error.&lt;/p&gt; &lt;p&gt;Ok, so we still get an error in Objective-C. And it's relatively easy to debug what's happening. But if the user gets this, the app will have crashed. And they'll get upset. Arguably, they'd get upset in C# too.&lt;/p&gt; &lt;p&gt;But what if the default index was valid? Like 0? We wouldn't crash, but we'd get incorrect results. Which is a LOT harder to debug. But doesn't crash for the user. Maybe it affects the final results that they're looking it for. Maybe it doesn't.&lt;/p&gt; &lt;p&gt;My point to all this is that this introduces a lot of subtleties to debugging something that you have to think about when writing or debugging the code. I'm not saying that it's bad at all. Sometimes you don't care about if the line fails or not -- and you don't have to write a bunch of function calls prefixed with&amp;nbsp;&lt;em&gt;if (reference != null)&lt;/em&gt;.&lt;/p&gt; &lt;p&gt;Another reason to not make assumptions about the way things work (especially in new programming languages). :)&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2278032" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/Python/default.aspx">Python</category><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Objective-C/default.aspx">Objective-C</category><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2B002B00_/default.aspx">C++</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Programming/default.aspx">Programming</category></item><item><title>There are no stupid questions, only stupid mistakes.</title><link>http://blogs.msdn.com/toddha/archive/2007/04/03/there-are-no-stupid-questions-only-stupid-mistakes.aspx</link><pubDate>Tue, 03 Apr 2007 23:45:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:2020905</guid><dc:creator>toddha</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/toddha/comments/2020905.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=2020905</wfw:commentRss><description>&lt;p&gt;Stupid mistakes are often the most painful.&lt;/p&gt; &lt;p&gt;Take the following sample code.&lt;/p&gt; &lt;p&gt;&lt;code&gt;public class Sample&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int [] array = new int[32];&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; private int x&amp;nbsp;= 0;&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public&amp;nbsp;int Foo()&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/code&gt;&lt;code&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return array[x];&lt;/code&gt;&lt;code&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/code&gt;&lt;code&gt;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;public int X&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;get { return this.x; }&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;set&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#if DEBUG&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value &amp;lt; 0 || value &amp;gt; 31) return;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;#endif&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;this.x = value;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;}&lt;/code&gt;&lt;/p&gt; &lt;p&gt;I know that most people who see this probably will probably already know what's wrong with the above, but it's always good to keep in mind. I wrote something like&amp;nbsp;the above and didn't catch it until I came back around to comment what I was doing.&lt;/p&gt; &lt;p&gt;For those who haven't caught it yet, let's assume I'm using a debug build. Therefore, the conditional statement&amp;nbsp;is compiled.&amp;nbsp;Then what if the calling code does something like :&lt;/p&gt; &lt;p&gt;&lt;code&gt;Sample instance = new Sample();&lt;br&gt;instance.X = 33;&lt;br&gt;instance.Foo( );&lt;br&gt;&lt;/code&gt;&lt;/p&gt; &lt;p&gt;The first line doesn't modify the class in the Debug build, so Foo just returns array[0] (which is 0). But now assume you use a Release build. instance.x gets changed to 33. The results of the call to instance.Foo( ) now (conveniently) change and&amp;nbsp;will crash with an OutOfRangeException( ).&lt;/p&gt; &lt;p&gt;What I &lt;strong&gt;meant&lt;/strong&gt; to say, should have been something like:&lt;/p&gt; &lt;p&gt;&lt;code&gt;#if DEBUG&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (value &amp;lt; 0 || value &amp;gt; 31) throw new ArgumentException("!x");&lt;br&gt;#endif&lt;/code&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;(where "!x" is a more verbose--and localized--message).&lt;/p&gt; &lt;p&gt;The idea behind checking property assignments in Debug versions is that you have better insight into what exactly is going wrong in order to debug it. Otherwise, Foo( ) might crash with a value of 33, but you won't figure out who assigned that value.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=2020905" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/toddha/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Hey You! This is a stick up!</title><link>http://blogs.msdn.com/toddha/archive/2007/02/22/hey-you-this-is-a-stick-up.aspx</link><pubDate>Fri, 23 Feb 2007 00:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1743501</guid><dc:creator>toddha</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/toddha/comments/1743501.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=1743501</wfw:commentRss><description>&lt;p&gt;Hand over your &lt;strike&gt;wallet&lt;/strike&gt; InfoCard!&lt;/p&gt; &lt;p&gt;I've been playing around with CardSpace and trying to get it to do what I need it to do : authenticate some user in a little 3-tier play app that I'm writing.&amp;nbsp;It's not as simple to use as most people would think. Here's my experience with it and how I got it to work. Hopefully I'll save you some time and frustration. (Note your milage may vary, especially since I'm not as detailed here as I would like to be). &lt;/p&gt; &lt;p&gt;&lt;strong&gt;Also, please note : this is&amp;nbsp;NOT prescriptive guidance.&amp;nbsp;There are no claims, warranties, or rights implied.&lt;/strong&gt;&lt;/p&gt; &lt;p&gt;First, install IE7. CardSpace requires it. You can do the next couple of steps without it, but eventually you'll need it.&lt;/p&gt; &lt;p&gt;I needed a cert in my test environment. You may or may not need this step, but&amp;nbsp;I went off and got the SelfSSL&amp;nbsp;tool in the &lt;a href="http://support.microsoft.com/kb/840671"&gt;IIS 6.0 Resource Kit&lt;/a&gt;. I created a self signed cert and installed it into my computer's certificates store (via mmc).&lt;/p&gt; &lt;p&gt;Next, I created a new IIS website (via inetmgr). I right clicked on it, clicked properties, went to the Directory Security tab, clicked on Server Certificate, and chose my cert. I checked that my site (https) worked in Internet Explorer and that there were no cert issues.&lt;/p&gt; &lt;p&gt;There's one small step that you'll probably need to do with certs that I missed, however. You'll need to know what identity your web site is running under. Open up inetmgr, find the web site or virtual directory that you are using. Right click it, click properties, and on the Web Site or Virtual Directory tab it will say the AppPool. Go back to the main window of inetmgr, expand Application Pools, find that AppPool, right click it, properties, and it will show you on the Identity tab what account it's running under.&lt;/p&gt; &lt;p&gt;Next.&lt;/p&gt; &lt;p&gt;Go download and install the WSE 3.0 tools from &lt;a href="http://www.microsoft.com/downloads/details.aspx?familyid=018A09FD-3A74-43C5-8EC1-8D789091255D&amp;amp;displaylang=en"&gt;here&lt;/a&gt;. Run the Certificates tool.&lt;/p&gt; &lt;ol&gt; &lt;li&gt;Choose Local Computer as the location.  &lt;li&gt;Click&amp;nbsp;Open Certificate  &lt;li&gt;Choose the Certificate you signed and click OK.  &lt;li&gt;Click View Private Key File Properties.  &lt;li&gt;Click on the Security tab.  &lt;li&gt;Make sure the account that we identified as the AppPool identity is listed and has read access.  &lt;li&gt;Done.&lt;/li&gt;&lt;/ol&gt; &lt;p&gt;Now you can implement your login. See &lt;a href="http://msdn2.microsoft.com/en-us/library/aa967562.aspx"&gt;here&lt;/a&gt;&amp;nbsp;for the sample code on how to do this. I used some of this. I wrote my own code for looking up the Certificates by subject like this :&lt;/p&gt; &lt;p&gt;&lt;code&gt;protected static X509Certificate2 LookupCert(string subject)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;X509Store store = new X509Store(&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/code&gt;&lt;code&gt;StoreName.My,&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; StoreLocation.LocalMachine);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;store.Open(OpenFlags.ReadOnly);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; foreach (X509Certificate2 cert in store.Certificates)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (cert.Subject.Equals(subject)) return cert;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return null;&lt;br&gt;}&lt;/code&gt;&lt;/p&gt; &lt;p&gt;Probably not the most efficient as you can call store.Certificates.Find(...).&lt;/p&gt; &lt;p&gt;I then took just Token.DecryptToken to do what I needed and discarded most of the rest (there are some supporting data structures that DecryptToken requires). Remember, no rights or warranties implied. Use at your own risk.&lt;/p&gt; &lt;p&gt;&lt;code&gt;string xmlToken = Request.Params["_xmlToken"];&lt;br&gt;if (xmlToken == null || xmlToken.Length == 0)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowError("ERROR : Token presented was null");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;}&lt;br&gt;string serverSubject = Request.Params["HTTPS_SERVER_SUBJECT"];&lt;br&gt;if (serverSubject == null || serverSubject.Length == 0)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowError("ERROR : serverSubject was null");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;} &lt;br&gt;try&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; X509Certificate2 c = LookupCert(serverSubject);&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;if (c == null)&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ShowError("ERROR : Could not find cert to decrypt with");&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;return;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;}&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;// decrypt the token into a byte array&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;byte [] decrypted = DecryptToken(c, xmlToken);&lt;br&gt;&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // get&amp;nbsp;SAML string from decrypted&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;string data = new UnicodeEncoding().GetString(bytes);&lt;/code&gt;&lt;code&gt;&lt;br&gt;}&lt;br&gt;catch (Exception ex)&lt;br&gt;{&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ShowError("ERROR : " + ex.ToString());&lt;br&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return;&lt;br&gt;}&lt;br&gt;&lt;/code&gt;&lt;/p&gt; &lt;p&gt;&amp;nbsp;You now have the SAML and can authenticate as appropriate!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1743501" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category><category domain="http://blogs.msdn.com/toddha/archive/tags/InfoCard/default.aspx">InfoCard</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Encryption/default.aspx">Encryption</category><category domain="http://blogs.msdn.com/toddha/archive/tags/CardSpace/default.aspx">CardSpace</category><category domain="http://blogs.msdn.com/toddha/archive/tags/.NET/default.aspx">.NET</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Programming/default.aspx">Programming</category></item><item><title>Assumptions are the mother of all.</title><link>http://blogs.msdn.com/toddha/archive/2007/02/07/assumptions-are-the-mother-of-all.aspx</link><pubDate>Wed, 07 Feb 2007 23:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:1621572</guid><dc:creator>toddha</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/toddha/comments/1621572.aspx</comments><wfw:commentRss>http://blogs.msdn.com/toddha/commentrss.aspx?PostID=1621572</wfw:commentRss><description>&lt;P&gt;While working on one of&amp;nbsp;my side projects at work,&amp;nbsp;I encounted something interesting with the % sign. The code that I'm going to talk about has nothing to do with any upcoming products -- it's simply a side project I'm working on in my free time that uses WCF and WPF (which are 2 major features of the .NET Framework 3.0). That being said, I've (hopefully) abstracted away everything project specific.&lt;/P&gt;
&lt;P&gt;The problem I encountered came from doing some basic math in C# code. Simply, I construct an equation and evaluate what the answer should be by going from left to right (ignoring operator precedence). So when I came up with some final result, I wanted to&amp;nbsp;double check that I was correct.&amp;nbsp;But since I don't keep track of every equation or answer, I had to write a quick tool to trace through it. I'm a big fan of Python for prototyping&amp;nbsp;because I've spent a lot of time in it and understand it well. So I copied over my C# code, made some slight modifications to save the intermediate data&amp;nbsp;and then parse and print the data at the end, and ran it. 
&lt;P&gt;My final answer in Python didn't match that from the one calculated in C# (note that C++ returns the same as C#). The algorithm looked correct, but the final results didn't match. 
&lt;P&gt;Eventually, I&amp;nbsp;found my problem. Take the following simple equation as an example: 
&lt;P&gt;&lt;EM&gt;-13&amp;nbsp;% 6 = ?&lt;/EM&gt; 
&lt;P&gt;C# evaluates this to -1. Python, on the other hand, evaluates to 5. &lt;EM&gt;Um...what?&lt;/EM&gt; Which one is correct? 
&lt;P&gt;Well, it depends on your point of view. Technically they're both correct.&amp;nbsp; Just one method takes it&amp;nbsp;one step further than the other. 
&lt;P&gt;&lt;EM&gt;-13 + 6 = -7&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;-7 + 6 = -1&lt;/EM&gt; (C# answer) 
&lt;P&gt;&lt;EM&gt;-1 + 6 = 5&lt;/EM&gt; (Python answer) 
&lt;P&gt;Hm. At first I thought that the in Python the answer will always be in the set &lt;EM&gt;{0, 1, 2, .... n-1 }&lt;/EM&gt;, and the C# answer will always be in the set &lt;EM&gt;{0, ±1, ±2, .... ±(n-1) }&lt;/EM&gt;. To check this, I ran another simple equation through Python and C#. 
&lt;P&gt;&lt;EM&gt;8 % -3 = ?&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;8&amp;nbsp;+ -3 = 5&lt;/EM&gt; 
&lt;P&gt;&lt;EM&gt;5&amp;nbsp;+ -3 = 2 &lt;/EM&gt;(C# answer)&amp;nbsp; 
&lt;P&gt;&lt;EM&gt;2 + -3 = -1&lt;/EM&gt; (Python answer) 
&lt;P&gt;Theory crushed. Python and C# follow the same basic principle rule of all the answer being in the set &lt;EM&gt;{0, ±1, ±2, ... ±(n-1) }&lt;/EM&gt;. But why does Python always take it further than the C# ? 
&lt;P&gt;So I looked up the community &lt;A title="defition of modulo" href="http://en.wikipedia.org/wiki/Modulo_operation" mce_href="http://en.wikipedia.org/wiki/Modulo_operation"&gt;definition of modulo&lt;/A&gt;. 
&lt;P&gt;It turns out that the % sign has no strict definition. Python keeps the same sign as the divisor. C# keeps the same sign as the dividend. 
&lt;P&gt;What you should take away : don't always assume that operators will work the same in different languages. 
&lt;P&gt;Extra credit : 
&lt;P&gt;Windows Live Search and Google both provide basic calculator functions in their search. For example, searching for the term "2 + 2" in WLS and Google both give 4. But what about "-13 % 6"?&amp;nbsp;Why&amp;nbsp;do / don't they give the same answer?&amp;nbsp; 
&lt;P&gt;&lt;A title="(Hint : look at who uses Python)" href="http://en.wikipedia.org/wiki/Python_software" mce_href="http://en.wikipedia.org/wiki/Python_software"&gt;Answer&lt;/A&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=1621572" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/toddha/archive/tags/Math/default.aspx">Math</category><category domain="http://blogs.msdn.com/toddha/archive/tags/Python/default.aspx">Python</category><category domain="http://blogs.msdn.com/toddha/archive/tags/C_2300_/default.aspx">C#</category></item></channel></rss>