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

  • Home
  • SEARCH
  • 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 853041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:41:40+00:00 2026-05-15T07:41:40+00:00

I have a delegate that modifies an object. I pass an object to the

  • 0

I have a delegate that modifies an object. I pass an object to the delegate from a calling method, however the calling method does not pick up these changes. The same code works if I pass a List as the object.

I thought all objects were passed by reference so any modifications would be reflected in the calling method. Is that correct?

I can modify my code to pass a ref object to the delegate. But I am wondering why this is necessary. Or is it?

public class Binder
{
    protected delegate int MyBinder<T>(object reader, T myObject);

    public void BindIt<T>(object reader, T myObject)
    {
        //m_binders is a hashtable of binder objects
        MyBinder<T> binder = m_binders["test"] as MyBinder<T>;
        int i = binder(reader, myObject);
    }
}

public class MyObjectBinder
{
    public MyObjectBinder()
    {
        m_delegates["test"] = new MyBinder<MyObject>(BindMyObject);
    }

    private int BindMyObject(object reader, MyObject obj)
    {
        obj = new MyObject
        {
            //update properties
        };
        return 1;
    }
}

///calling method in some other class
public void CallingMethod()
{
    MyObject obj = new MyObject();

    MyObjectBinder binder = new MyObjectBinder();
    binder.BindIt(myReader, obj); //don't worry about myReader

    //obj should show reflected changes
}

Update:

I am now passing objects by ref to the delegate as I am instantiating a new object inside BindMyObject.

protected delegate int MyBinder<T>(object reader, ref T myObject);
  • 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-15T07:41:41+00:00Added an answer on May 15, 2026 at 7:41 am

    Objects aren’t passed by reference. Objects aren’t passed at all.

    By default, the value of the argument is passed by value – whether that value is a value type value or a reference. If an object is modified via that reference, then that change will be visible to the calling code as well.

    In the code you showed originally, there was no reason to use ref. The ref keyword is used when you want a method that changes the value of a parameter (e.g. to make it refer to a different object entirely) and have that change visible to the caller.

    Now, in the code you’ve shown (originally) you’ve only got:

    private int BindMyObject(object reader, MyObject obj)
    {
        //make changes to obj in here
    }
    

    Do you mean code like this:

    private int BindMyObject(object reader, MyObject obj)
    {
        obj = new MyObject();
    }
    

    or code like this:

    private int BindMyObject(object reader, MyObject obj)
    {
        obj.SomeProperty = differentValue;
    }
    

    ? If it’s the latter, then you don’t need ref. If it’s the former, then you do need ref because you’re changing the parameter itself, not making changes to the object that the value refers to. In fact, if you’re just setting the value of obj without ever reading it, you should use out instead of ref.

    If you can show a short but complete program which demonstrates your problem, it’ll be a lot easier to explain what’s going on.

    It’s hard to do this topic justice in just a few paragraphs – so I’ve got an entire article about it, which will hopefully make things more obvious.

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

Sidebar

Related Questions

I have my main application delegate which contains a method that returns an object.
I have this method in App Delegate that makes a window and content view
I have a method that accepts a generic delegate as a parameter, and inserts
In .NET, when you have a delegate that only has an empty method subscribed
Here's a method delegate that I have : delegate void ExceptionHandelr<T>(T exception,out bool handeled)
I have a method in my app delegate that updates the badge count for
I have this delegate method below that is setting a few fields in my
I have an email delegate method that shows a result message. After the result
VB.NET 2010, .NET 4 Hello all, I have a System.Timers.Timer object that does some
I have a class that serves as a delegate to another. public class Delegate

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.