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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:04:37+00:00 2026-06-04T07:04:37+00:00

Having a user defined class, like this: class Foo { public int dummy; public

  • 0

Having a user defined class, like this:

class Foo
    {
        public int dummy;

        public Foo(int dummy)
        {
            this.dummy = dummy;
        }
    }

And having then something like this:

ArrayList dummyfoo = new ArrayList();

Foo a = new Foo(1);
dummyfoo.add(a);

foreach (Foo x in dummyfoo)
    x.dummy++;

How much is a.dummy?

How can i create my ArrayList so that a.dummy equals 2, meaning that my ArrayList contains basically pointers to my objects and not copies.

  • 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-04T07:04:38+00:00Added an answer on June 4, 2026 at 7:04 am

    It is already 2, as Array/Collections (to be precise any .NET Class/reference type) are passed by reference by default.

    In fact the reference variable is passed by value, but behaves as if passed by reference.

    Why ->

      Consider var arr = new ArrayList();
    

    The above statement first creates an ArrayList object and a reference is assigned to arr. (This is similar for any Class as class are reference type).

    Now at the time of calling,

    example ->    DummyMethod(arr) , 
    

    the reference is passed by value, that is even if the parameter is assigned to a different object within the method, the original variable remains unchanged.
    But as the variable points(refer) to same object, any operation done on underlying pointed object is reflected outside the called method.
    In your example, any modification done in for each will be reflected in the arrayList.

    If you want to avoid this behavior you have to create copy/clone of the object.

    Example:

    Instead of

    foreach (Foo x in dummyfoo)
            x.dummy++;
    

    Use

    foreach (Foo x in (ArrayList)dummyfoo.Clone())
            x.dummy++;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a snippet like this: import yaml class User(object): def __init__(self, name, surname): self.name=
I am having issues populating a List of User defined object attributes in Struts2.
Im having this function to determine weather a user exists in a database or
I'm having trouble passing a variable defined in one class instance to another class
I'm having a user control (referred to as 'parent') which dynamically adds or removes
I am having a user object which has one-to-many relation with Address object. My
I just want to get email id of user having phone number given. so
Hai am having a WPF user control in my WPF Form, when i click
Hai am having a WPF user control, when i use that control in another
What's the purpose of having the Simulated User Interface elements, i.e. the top and

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.