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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:27:56+00:00 2026-06-09T22:27:56+00:00

I’m doing a big refactor of a pile of code that used to use

  • 0

I’m doing a big refactor of a pile of code that used to use a bunch of multidimensional arrays that were constantly getting resized. I created a data object to replace the 2D array, and I’m now passing a list of these around.

I discovered something that worries me a little though. Let’s say I have some code that looks like this:

List<NCPoint> basePoints = new List<NCPoint>();

// ... snip populating basePoints with starting data

List<NCPoint> newPoints = TransformPoints(basePoints, 1, 2, 3);

public List<NCPoint> TransformPoints(List<NCPoint> points, int foo, int bar, int baz){
    foreach(NCPoint p in points){
        points.X += foo
        points.Y += bar
        points.Z += baz
    }

    return points;
}

The idea is to keep a list of the original points (basePoints) and a list of the updated points (newPoints). But C# passes the list by reference, as with any object. This updates basePoints in place, so now both basePoints and newPoints will have the same data.

At the moment, I’m trying to be careful about making a full copy of the passed-in List before I muck with the data. Is that the only sensible way to make sure changes to an object within a function don’t have side effects outside the function? Is there anything akin to passing an object with const?

  • 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-06-09T22:27:57+00:00Added an answer on June 9, 2026 at 10:27 pm

    In short: no.

    C# does not have the concept of a const reference, per se. If you want to make an object immutable, you must code it explicitly so or take advantage of other “tricks”.

    You can make your collection immutable in many ways (ReadOnlyColelction, return an iterator, return a shallow copy) but that only protects the sequence, not the data stored inside.

    Thus, what you’d really need to do what you want is to return a deep copy or projection, possibly using LINQ:

    public IEnumerable<NCPoint> TransformPoints(List<NCPoint> points, int foo, int bar, int baz)
    {
        // returning an iterator over the sequence so original list won't be changed
        // and creating new NCPoint using old NCPoint + modifications so old points
        // aren't altered.
        return points.Select(p => new NCPoint
            { 
               X = p.X + foo,
               Y = p.Y + bar,
               Z = p.Z + baz
            });
    }
    

    Also, the beauty of returning an iterator (as opposed to just returning a List<T> as an IEnumerable<T>, etc.) is that it can’t be cast back to the original collection type.

    UPDATE: Or, in .NET 2.0 parlance:

    public IEnumerable<NCPoint> TransformPoints(List<NCPoint> points, int foo, int bar, int baz)
    {
        // returning an iterator over the sequence so original list won't be changed
        // and creating new NCPoint using old NCPoint + modifications so old points
        // aren't altered.
        NCPoint[] result = new NCPoint[points.Count];
    
        for (int i=0; i<points.Count; ++i)
        { 
            // if you have a "copy constructor", can use it here.
            result[i] = new NCPoint();
            result[i].X = points[i].X + foo;
            result[i].Y = points[i].Y + bar;
            result[i].Z = points[i].Z + baz;
        }
    
        return result;
    }
    

    The point is, there are many ways to treat something as immutable, but I wouldn’t try to implement C++-style “const correctness” in C# or you will go mad. Implement it as needed when you want to avoid side-effects, etc.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

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.