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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:22:45+00:00 2026-05-25T16:22:45+00:00

I always thought that objects where always passed as reference in C# and that

  • 0

I always thought that objects where always passed as reference in C# and that if a function modifies it then the parent method should have a modified version of that object. However, reading this code that was left by an old developer that was causing a bug makes me think that this is not always the case. The code is the following:

Item item = new Item();
PopulateItem(sodItem, id, item);

The function code is the following:

private void PopulateItem(eSodTypes.Item sodItem, int id, Item item)
        {

                if (sodItem != null)
                {
                    item = (Item)sodItem;
                    item.Delivery = (Delivery)sodItem.Delivery;
                    if (sodItem.Delivery != null)
                    {
                        item.Delivery.DeliveryContact = (CustomerContact)sodItem.Delivery.DeliveryContact;
                        item.Delivery.DeliveryAddress = (Address)sodItem.Delivery.DeliveryAddress;
                        item.Delivery.ShippingInstructions = (ShippingInstructions)sodItem.Delivery.ShippingInstructions;
                    }
                    item.Installation = (Installation)sodItem.Installation;
                    item.Training = (Training)sodItem.Training;
                    item.Service = (Service)sodItem.Service;
                    item.Service.Address = (Address)sodItem.Service.Address;
                    if (sodItem.Service.Address != null)
                        item.Service.Address.Contact = (CustomerContact)sodItem.Service.Address.Contact;
                    item.Service.Customer = (Customer)sodItem.Service.Customer;
                    if (sodItem.ItemLines != null)
                        item.ItemLines = sodItem.ItemLines.Items.ToList().ConvertAll(new Converter<eSodTypes.ItemLine, ItemLine>(ItemLine.ItemLineTypeToItemLineTypeModel));
                }
        }

If I use “ref” with the item it works, but I thought this was only for value types like “int” or “double”.

  • 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-25T16:22:46+00:00Added an answer on May 25, 2026 at 4:22 pm

    I’m assuming you mean that you expect that ‘item’ will be modified upon return from the method. The problem is here:

    item = (Item)sodItem;
    

    You’re modifying the variable item to point to the parameter sodItem, so it’s no longer pointing to the same item that was passed in. The original reference ‘item’ was never modified. When you change the item parameter to ‘ref’, this does cause the original reference of the variable passed as a parameter to change.

    Note that this current code is actually modifying the original sodItem (copying its own values back to itself), which might not be your intention.

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

Sidebar

Related Questions

I have always thought that the .equals() method in java should be overridden to
I have always been taught that non-primitive types should be passed by const reference
In C#, I have always thought that non-primitive variables were passed by reference and
I have always thought that the terms internationalization and localization (and their funny abbreviations
I have always thought that in order to connect to SQL server using windows
i have always thought that returning Arrays were better than lists when having a
I always thought that if you didn't implement a heartbeat, there was no way
I always thought that parentheses improved readability, but in my textbook there is a
I always thought that when you dropped a control onto an .aspx page that
I always thought that internal class has access to all data in its external

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.