Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 61563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:20:19+00:00 2026-05-10T18:20:19+00:00

When you attempt to declare an unsigned variable in C#.NET with a value outside

  • 0

When you attempt to declare an unsigned variable in C#.NET with a value outside its value range it is flagged as a compiler error, but if you produce a negative value at runtime and assign it to that variable at runtime the value wraps.

uint z = -1; // Will not compile  uint a = 5; uint b = 6; uint c = a - b; // Will result in uint.MaxValue 

Is there a good reason why unsigned variables wrap in such a situation instead of throwing an exception?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:20:20+00:00Added an answer on May 10, 2026 at 6:20 pm

    Declaring an unassigned variable in C# isn’t flagged with an error – trying to assign an invalid value to a variable is. For instance, here’s a variable which isn’t definitely assigned (assuming it’s local) after declaration:

    uint z; 

    -1 isn’t a valid value for a uint any more than 0.5 is, which is why your example wouldn’t compile.

    Now, as for the rest: integers types just wrap on overflow – just as adding 1 to int.MaxValue returns int.MinValue. This is a significant performance improvement over having the program check each operation for overflow – at the cost of potentially not spotting an error.

    That’s only if you’re in an unchecked context, mind you – if you perform any of these operations in a checked context, you’ll get an exception instead. For instance;

    class Test {     static void Main()     {         checked         {             uint a = 5;             uint b = 6;             uint c = a - b;         }     } } 

    Run that and you’ll see an OverflowException get thrown. If that’s what you want for your whole project, you can set it in the project properties (or compile with the /checked+ command line option to csc.)

    EDIT: It’s worth noting that the other answers have shown that you could put smaller amounts of code in the checked context – just the declaration and assignment of c or even just the calculation. It’s all pretty flexible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Does a transaction in a stored procedure do any locking… May 11, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer positional parameters. If you google "positional parameters", you'll usually find… May 11, 2026 at 4:27 pm
  • Editorial Team
    Editorial Team added an answer There's a solution, but it's not QUITE as simple as… May 11, 2026 at 4:27 pm

Related Questions

It seems to me that, in some ways, Java is where C was a
In Java, we're using the following package to programmatically create excel documents: org.apache.poi.hssf If
There is a statement in the MySQL Docs ( http://dev.mysql.com/doc/refman/5.0/en/adding-udf.html ) that is bothering
When you first fire up Visual Studio, or even before that (Hopefully) What do

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.