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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:01:41+00:00 2026-05-13T15:01:41+00:00

I’ve read the difference between passng and not passing ref in parameters, however, when

  • 0

I’ve read the difference between passng and not passing ref in parameters, however, when would I want to use them?

For example, I had some logic in a method which could be refactored into its own method. Resharper 4.5 made one of the parameters a ref type but I didn’t think I would have of done this if I did the refactoring manually.

Obviously I am missing some understanding. Perhaps an example of what happens when certain types or certain scenarios in coding miss the ref keyword will help?

Thanks

  • 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-13T15:01:42+00:00Added an answer on May 13, 2026 at 3:01 pm

    Let me break that down into two questions:

    1) When should one use ref/out formal parameter declarations when writing a method?

    Use ref/out when you desire your method to be able to read and write a variable passed in from the caller, rather than merely reading a value.

    2) Why does an “extract method” refactoring produce a ref parameter?

    I don’t know the details of Resharper, but I can make a guess. Consider the following evil mutable value type:

    struct S 
    { 
      private int x;
      public int X() { return this.x; } 
      public void M() { this.x += 1; } 
    }
    

    You have a method:

    void Foo() 
    {
        S s = new S();
        Fred(s);
        Blah(s);
        Bar(s);
        s.M();
        Console.WriteLine(s.X()); // prints 1
    }
    

    and you do “extract method” on the middle bit:

    void NewMethod(ref S s)
    {
        Blah(s);
        Bar(s);
        s.M();
    }
    
    void Foo() 
    {
        S s = new S();
        Fred(s);
        NewMethod(ref s);
        Console.WriteLine(s.X()); // still prints 1
    }
    

    If instead you made a method without “ref” then calling NewMethod(s) would pass a copy of s to NewMethod. Remember, value types are copied by value; that’s why we called them “value types”. It would be the copy that gets mutated, and then s.X() returns zero. It is a bad idea for a refactoring to introduce a semantic change in a program, and it is difficult for a refactoring engine to know whether a given method relies on the mutability of a value type or not.

    This is just another reason why you should avoid mutable value types.

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

Sidebar

Ask A Question

Stats

  • Questions 373k
  • Answers 373k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I made a macro for Visual Studio 2005 which needed… May 14, 2026 at 7:40 pm
  • Editorial Team
    Editorial Team added an answer From various reading it appears that CFNetwork is responsible for… May 14, 2026 at 7:40 pm
  • Editorial Team
    Editorial Team added an answer http://www.tsjensen.com/blog/CommentView,guid,904052d4-8ee0-47b5-bacb-eb1788137233.aspx May 14, 2026 at 7:40 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.