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 3302794
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:53:23+00:00 2026-05-17T20:53:23+00:00

Why are int s and double s immutable? What is the purpose of returning

  • 0

Why are ints and doubles immutable? What is the purpose of returning a new object each time you want to change the value?

The reason I ask is because I’m making a class: BoundedInt, which has a value and an upper and lower bound. So I was wondering: should I make this type immutable too? (Or should it be a struct?)

  • 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-17T20:53:23+00:00Added an answer on May 17, 2026 at 8:53 pm

    Firstly:

    What is the purpose of returning a new object each time you want to change the value?

    I think you might be mistaken about how value types work. This isn’t some costly operation like you may be imagining; it’s simply the overwriting of data (as opposed to, e.g., dynamic allocation of new memory).

    Secondly: here’s a very simple example of why numbers are immutable:

    5.Increase(1);
    Console.WriteLine(5); // What should happen here?
    

    Granted, that is a contrived example. So let’s consider a couple more involved ideas.

    Mutable reference type

    First, there’s this one: what if Integer were a mutable reference type?

    class Integer
    {
        public int Value;
    }
    

    Then we could have code like this:

    class Something
    {
        public Integer Integer { get; set; }
    }
    

    And:

    Integer x = new Integer { Value = 10 };
    
    Something t1 = new Something();
    t1.Integer = x;
    
    Something t2 = new Something();
    t2.Integer = t1.Integer;
    
    t1.Integer.Value += 1;
    
    Console.WriteLine(t2.Integer.Value); // Would output 11
    

    This seems to defy intuition: that the line t2.Integer = t1.Integer would simply copy a value (actually, it does; but that “value” is in fact a reference) and thus that t2.Integer would remain independent of t1.Integer.

    Mutable value type

    This could be approached another way, of course, keeping Integer as a value type but maintaining its mutability:

    struct Integer
    {
        public int Value;
    
        // just for kicks
        public static implicit operator Integer(int value)
        {
            return new Integer { Value = value };
        }
    }
    

    But now let’s say we do this:

    Integer x = 10;
    
    Something t = new Something();
    t.Integer = x;
    
    t.Integer.Value += 1; // This actually won't compile; but if it did,
                          // it would be modifying a copy of t.Integer, leaving
                          // the actual value at t.Integer unchanged.
    
    Console.WriteLine(t.Integer.Value); // would still output 10
    

    Basically, immutability of values is something that is highly intuitive. The opposite is highly unintuitive.

    I guess that is subjective, though, in all fairness 😉

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

Sidebar

Related Questions

In C# if I want to convert a double (1.71472) to an int then
this snippet of code Tuple<int,double>[, ,] myArray = new Tuple<int, double> () [xsize, ysize,
I have a Python function, fooPy() that returns some value. ( int / double
This code works (C# 3) double d; if(d == (double)(int)d) ...; Is there a
#include <iostream> using namespace std; int main() { double u = 0; double w
Consider the following signature in C#: double Divide(int numerator, int denominator); Is there a
Like there are 3 methods like: Modify ( int arg ) Modify ( double
Currently i have this method: static boolean checkDecimalPlaces(double d, int decimalPlaces){ if (d==0) return
If I have the code: int f(int a) { return a; } double f(double
I have the following C++ method : __declspec(dllexport) void __stdcall getDoubles(int *count, double **values);

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.