<p>Most of time we represent concepts which can never be less than 0. For example to declare length, we write:</p>

<pre><code>int length;
</code></pre>

<p>The name expresses its purpose well but you can assign negative values to it. It seems that for some situations, you can represent your intent more clearly by writing it this way instead:</p>

<pre><code>uint length; 
</code></pre>

<p>Some disadvantages that I can think of:</p>

<ul>
<li>unsigned types (uint, ulong, ushort) are not CLS compliant so you can't use it with other languages that don't support this</li>
<li>.Net classes use signed types most of the time so you have to cast</li>
</ul>

<p>Thoughts?</p>

[{'answer_id': 33857, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 3, 'selected': False, 'text': '<p>If you decrement a signed number with a value of 0, it becomes negative and you can easily test for this. If you decrement an unsigned number with a value of 0, it underflows and becomes the maximum value for the type - somewhat more difficult to check for. </p>\n'}, {'answer_id': 33858, 'author': 'Dan Herbert', 'author_id': 392, 'author_profile': 'https://Stackoverflow.com/users/392', 'pm_score': 3, 'selected': False, 'text': '<p>Your second point is the most important. Generally you should just use <code>int</code> since that\'s a pretty good "catch-all" for integer values. I would only use <code>uint</code> if you absolutely need the ability to count higher than <code>int</code>, but without using the extra memory <code>long</code> requires (it\'s not much more memory, so don\'t be cheap :-p).</p>\n'}, {'answer_id': 33865, 'author': 'Rob Bazinet', 'author_id': 2305, 'author_profile': 'https://Stackoverflow.com/users/2305', 'pm_score': 2, 'selected': False, 'text': "<p>I think the subtle use of uint vs. int will cause confusing with developers unless it was written into developer guidelines for the company.</p>\n\n<p>If the length, for example, can't be less than zero then it should be expressed clearly in the business logic so future developers can read the code and know the true intent.</p>\n\n<p>Just my 2 cents.</p>\n"}, {'answer_id': 34008, 'author': 'Eric Burnett', 'author_id': 3524, 'author_profile': 'https://Stackoverflow.com/users/3524', 'pm_score': 2, 'selected': False, 'text': '<p>I will point out that in C# you can turn on <a href="http://msdn.microsoft.com/en-us/library/h25wtyxf.aspx" rel="nofollow noreferrer"><code>/checked</code></a> to check for arithmetic overflow / underflow, which isn\'t a bad idea anyways. If performance matters in a critical section, you can still use <code>unchecked</code> to avoid this. </p>\n\n<p>For internal code (ie code that won\'t be referenced in any interop manor with other languages) I vote for using unsigned when the situation warrants it, such as <code>length</code> variables as mentioned earlier. This - along with checked arithmetic - provides one more net for developers, catching subtle bugs earlier.</p>\n\n<p>Another point in the signed vs unsigned debate is that some programmers use values such as -1 to indicate errors, when they wouldn\'t otherwise have meaning. I subscribe to the view that each variable should have only one purpose, but if you - or colleagues you code with - like to indicate errors in this way, leaving variables signed gives you the flexibility to add error states later.</p>\n'}, {'answer_id': 34049, 'author': 'Sander', 'author_id': 2928, 'author_profile': 'https://Stackoverflow.com/users/2928', 'pm_score': 0, 'selected': False, 'text': '<p>Your two points are good. The primary reason to avoid it is casting, though. Casting makes them incredibly annoying to use. I tried using unisigned variables once but I had to sprinkle casts absolutely everywhere because the framework methods all use signed integers. Therefore, whenever you call a framework method, you have to cast.</p>\n'}, {'answer_id': 34108, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 4, 'selected': True, 'text': "<p>“When in Rome, do as the Romans do.”</p>\n\n<p>While there is theoretically an advantage in using unsigned values where applicable because it makes the code more expressive, this is simply not done in C#. I'm not sure why the developers initially didn't design the interfaces to handle <code>uints</code> and make the type CLS compliant but now the train has left the station.</p>\n\n<p>Since consistency is generally important I'd advise taking the C# road and using <code>int</code>s.</p>\n"}]
['https://Stackoverflow.com/questions/33852', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/718/']