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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:02:49+00:00 2026-06-18T05:02:49+00:00

I’m experienced with C++, but a little new to C#. When you add objects

  • 0

I’m experienced with C++, but a little new to C#.

When you add objects to a container, are they passed by reference or by value?

That is, if I do this:

myClass m = new myClass(0);       //Assume the class just holds an int
List<myClass> myList = new List<myClass>(1);
myList.Add(m);
myList[0] += 1;
Console.WriteLine(m);
Console.WriteLine(myList[0]);

Will the result be:

0
1

or will it be

1
1

?

If the former, then how can get I make it do the latter? My first instinct was to do something like

myClass ref mref = m; 
Console.WriteLine(mref);

But this doesn’t seem to be valid syntax.

  • 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-18T05:02:50+00:00Added an answer on June 18, 2026 at 5:02 am

    The value is passed by value to the Add method; however, if you pass a reference type (a class is always a reference type), then the value itself is a reference. So the question is not so much whether the value is passed by value or by reference, but if the type is a value type or a reference type.

    class MyClass
    {
        public int Number { get; set; }
    }
    

    With this declaration, we get:

    MyClass m = new MyClass();
    List<MyClass> myList = new List<MyClass>();
    myList.Add(m);
    
    myList[0].Number += 1;
    Console.WriteLine(myList[0].Number); // Displays 1
    Console.WriteLine(m.Number); // Displays 1
    
    myList[0].Number += 1;
    Console.WriteLine(myList[0].Number); // Displays 2
    Console.WriteLine(m.Number); // Displays 2
    

    Note that this would not work with a struct, because the value returned by myList[0] would be a copy of the value stored in the list. The += 1 would only increment the Number property of this temporary copy and thus have no other effect than consuming a few processor cycles. Therefore it is a good advice to create only immutable structs.


    If you want to display the object directly, override ToString

    class MyClass
    {
        public int Number { get; set; }
    
        public override string ToString()
        {
            return Number.ToString();
        }
    }
    

    Now, you can write

    myList[0].Number += 1;
    Console.WriteLine(myList[0]);
    Console.WriteLine(m);
    

    You could even make myList[0] += 1 work with an operator overload. In MyClass declare

    public static MyClass operator +(MyClass m, int i) 
    {
        m.Number += i;
        return m;
    }
    

    But this is a bit weird, unless your class represents a number, but in that case an immutable struct would be preferred, as numbers are generally perceived as immutable value types.

    public static MyStruct operator +(MyStruct m, int i) 
    {
        return new MyStruct(m.Number + i);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
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 want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

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.