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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T09:40:02+00:00 2026-05-25T09:40:02+00:00

So this works.. public MyClass(ref Apple apple) { apple = new Apple(Macintosh); // Works

  • 0

So this works..

public MyClass(ref Apple apple)
{
    apple = new Apple("Macintosh"); // Works fine
}

But is it possible to do something like this?

private Apple myApple;

public MyClass(ref Apple apple)
{
    myApple = apple;
}

public void ModifyApple()
{
    myApple = new Apple("Macintosh"); // does not change the input variable like the first example did
}

When the ref variable is copied to the member variable myApple it appears to lose it’s ‘ref-ness’ and re-assigning it no longer changes the input variable. Is there a way around 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-25T09:40:02+00:00Added an answer on May 25, 2026 at 9:40 am

    Not really, no. By the time your code is next invoked, the original variable used as the method argument may no longer even exist:

    void Foo()
    {
        MyClass x = Bar();
        x.ModifyApple();
    }
    
    MyClass Bar()
    {
        Apple apple = new Apple();
        return new MyClass(ref apple);
    }
    

    Here, apple is a local variable, in a stack frame which will have been popped by the time we call ModifyApple.

    Are you sure you need to modify the original caller’s variable rather than just changing the object itself?

    One way to sort of fake this would be to use a wrapper type to start with:

    public class MutableWrapper<T>
    {
        public T Value { get; set; }
    }
    

    Then pass in a MutableWrapper<Apple>, and store that in your class. Then in ModifyApple you can write:

    wrapper.Value = new Apple();
    

    This won’t change the caller’s variable, but next time the caller looks at the Value property, they’ll see your new apple.

    To be honest, this sort of thing tends to make for hard-to-maintain code, and even ref isn’t great for readability. If you can explain the bigger picture of what you’re trying to achieve, we may be able to suggest a better overall approach.

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

Sidebar

Related Questions

When I compile something like this: public class MyClass { void myMethod(String name, String
This works: class MyClass { int a; public MyClass() { int b = a;
If I set a Friend-level scope on a setter, like this... Public Class MyClass
I have a class that works like so: @Component public class MyClass { @Autowired
I have a class like this: class MyClass { Map<String, String[]> arrays; public void
My WCF Data Service includes this: [WebGet] public MyClass GetClass() { return new MyClass();
I have a class setup like this public class MyCLass { //... public IList<MyInnerClass>
As in: public class MyClass { private static var MyProp = new {item1 =
How to correct the Flaw in this function class MyClass { private $_callingscript; public
This code works: class Test { public: Test(string name) : _name(name) {}; bool operator()()

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.