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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:04:16+00:00 2026-05-27T20:04:16+00:00

In C#, I have always thought that non-primitive variables were passed by reference and

  • 0

In C#, I have always thought that non-primitive variables were passed by reference and primitive values passed by value.

So when passing to a method any non-primitive object, anything done to the object in the method would effect the object being passed. (C# 101 stuff)

However, I have noticed that when I pass a System.Drawing.Image object, that this does not seem to be the case? If I pass a system.drawing.image object to another method, and load an image onto that object, then let that method go out of scope and go back to the calling method, that image is not loaded on the original object?

Why is this?

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

    Objects aren’t passed at all. By default, the argument is evaluated and its value is passed, by value, as the initial value of the parameter of the method you’re calling. Now the important point is that the value is a reference for reference types – a way of getting to an object (or null). Changes to that object will be visible from the caller. However, changing the value of the parameter to refer to a different object will not be visible when you’re using pass by value, which is the default for all types.

    If you want to use pass-by-reference, you must use out or ref, whether the parameter type is a value type or a reference type. In that case, effectively the variable itself is passed by reference, so the parameter uses the same storage location as the argument – and changes to the parameter itself are seen by the caller.

    So:

    public void Foo(Image image)
    {
        // This change won't be seen by the caller: it's changing the value
        // of the parameter.
        image = Image.FromStream(...);
    }
    
    public void Foo(ref Image image)
    {
        // This change *will* be seen by the caller: it's changing the value
        // of the parameter, but we're using pass by reference
        image = Image.FromStream(...);
    }
    
    public void Foo(Image image)
    {
        // This change *will* be seen by the caller: it's changing the data
        // within the object that the parameter value refers to.
        image.RotateFlip(...);
    }
    

    I have an article which goes into a lot more detail in this. Basically, “pass by reference” doesn’t mean what you think it means.

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

Sidebar

Related Questions

I have always been taught that non-primitive types should be passed by const reference
I have always thought that the .equals() method in java should be overridden to
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
Previously I would always have thought a Vector was good to use for non-descript
I have an odd question that I have always thought about, but could never
I have always thought that the warp scheduler will execute one warp at a
I have always thought it was best practice to be explicit in naming my
I am a self thought hobby programmer and therefore don't have the fundamentals always
I have always hoped and assumed that it is not - that set theory

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.