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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:50:57+00:00 2026-05-23T12:50:57+00:00

How do I implement this functionality? I think its not working because I save

  • 0

How do I implement this functionality? I think its not working because I save it in the constructor?
Do I need to do some Box/Unbox jiberish?

    static void Main(string[] args)
    {
        int currentInt = 1;

        //Should be 1
        Console.WriteLine(currentInt);
        //is 1

        TestClass tc = new TestClass(ref currentInt);

        //should be 1
        Console.WriteLine(currentInt);
        //is 1

        tc.modInt();

        //should be 2
        Console.WriteLine(currentInt);
        //is 1  :(
    }

    public class TestClass
    {
        public int testInt;

        public TestClass(ref int testInt)
        {
            this.testInt = testInt;
        }

        public void modInt()
        {
            testInt = 2;
        }

    }
  • 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-23T12:50:57+00:00Added an answer on May 23, 2026 at 12:50 pm

    You can’t, basically. Not directly. The "pass by reference" aliasing is only valid within the method itself.

    The closest you could come is have a mutable wrapper:

    public class Wrapper<T>
    {
        public T Value { get; set; }
    
        public Wrapper(T value)
        {
            Value = value;
        }
    }
    

    Then:

    Wrapper<int> wrapper = new Wrapper<int>(1);
    ...
    TestClass tc = new TestClass(wrapper);
    
    Console.WriteLine(wrapper.Value); // 1
    tc.ModifyWrapper();
    Console.WriteLine(wrapper.Value); // 2
    
    ...
    
    class TestClass
    {
        private readonly Wrapper<int> wrapper;
    
        public TestClass(Wrapper<int> wrapper)
        {
            this.wrapper = wrapper;
        }
    
        public void ModifyWrapper()
        {
            wrapper.Value = 2;
        }
    }
    

    You may find Eric Lippert’s recent blog post on "ref returns and ref locals" interesting.

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

Sidebar

Related Questions

I'm having some conceptual trouble on figuring out how to best implement this... I
How do I implement this method (see below)? I'm new to Objective-C and I'm
How do you implement this in CodeIgniter?
I'm looking for a way to implement this design in wxPython on Linux... I
I wondering what the best practice way (in C#) is to implement this xpath
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
So this post talked about how to actually implement url rewriting in an ASP.NET
This probably sounds really stupid but I have noo idea how to implement jquery's
I want to implement my own clustering algorithm using this Virtual Earth javascript API:
I asked this question a while back but now I'm looking to implement an

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.