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

  • SEARCH
  • Home
  • 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 6628257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:06:34+00:00 2026-05-25T22:06:34+00:00

Definitely, I know the basic differences between unsigned integers ( uint ) and signed

  • 0

Definitely, I know the basic differences between unsigned integers (uint) and signed integers (int).

I noticed that in .NET public classes, a property called Length is always using signed integers.

Maybe this is because unsigned integers are not CLS compliant.

However, for example, in my static function:

public static double GetDistributionDispersion(int tokens, int[] positions)

The parameter tokens and all elements in positions cannot be negative. If it’s negative, the final result is useless. So if I use int both for tokens and positions, I have to check the values every time this function is called (and return non-sense values or throw exceptions if negative values found???), which is tedious.

OK, then we should use uint for both parameters. This really makes sense to me.

I found, however, as in a lot of public APIs, they are almost always using int. Does that mean inside their implementation, they always check the negativeness of each value (if it is supposed to be non-negative)?

So, in a word, what should I do?

I could provide two cases:

  1. This function will only be called by myself in my own solution;
  2. This function will be used as a library by others in other team.

Should we use different schemes for these two cases?

Peter

P.S.: I did do a lot of research, and there is still no reason to convince me not to use uint 🙂

  • 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. Editorial Team
    Editorial Team
    2026-05-25T22:06:35+00:00Added an answer on May 25, 2026 at 10:06 pm

    I see three options.

    Use uint. The framework doesn’t because it’s not CLS compliant. But do you have to be CLS compliant? (There are also some not-fun issues with arithmetic that crop up; it’s not fun to cast all over the place. I tend to void uint for this reason).

    Use int but use contracts:

    Contract.Requires(tokens >= 0);
    Contract.Requires(Contract.ForAll(positions, position => position >= 0));
    

    Make it explicit it exactly what you require.

    Create a custom type that encapsulates the requirement:

    struct Foo {
        public readonly int foo;
    
        public Foo(int foo) {
            Contract.Requires(foo >= 0);
            this.foo = foo;
        }
    
        public static implicit operator int(Foo foo) {
            return this.foo;
        }
    
        public static explicit operator Foo(int foo) {
            return new Foo(foo);
        }
    }
    

    Then:

    public static double GetDistributionDispersion(Foo tokens, Foo[] positions) { }
    

    Ah, nice. We don’t have to worry about it in our method. If we’re getting a Foo, it’s valid.

    You have a reason for requiring non-negativity in your domain. It’s modeling some concept. Might as well promote that concept to a bonafide object in your domain model and encapsulate all the concepts that come with it.

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

Sidebar

Related Questions

The first one is definitely something that works, but which one below is the
This is probably something silly I'm missing but I'm definitely lost. I'm using .NET
Sorry for such a basic question, but I need to know how to add
I know that, in jQuery, we are told to use $(document).ready() in order to
I know that browser detection using the userAgent property is a terrible idea because
I know with ASP.NET there's a global handler Application_EndRequest handler where I could change
For the past few days I've been writing classes that at first I thought
I'm trying to connect to a web service using IO::Socket::INET (yes, I know that
Does anyone know of a git plumbing (definitely not porcelain) means of determining whether:
does anyone know some good open source editors / IDES that are using SynEdit?

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.