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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:52:18+00:00 2026-05-23T04:52:18+00:00

Check this method: public static void Swap(ref int i, ref int j) { int

  • 0

Check this method:

public static void Swap(ref int i, ref int j)
{
   int aux=i;
   i=j;
   j=aux;
}

Is it more efficient than this?

public static void Swap1(int[] a, int i, int j)
{
   int aux=a[i];
   a[i]= a[j];
   a[j]=aux;
}

I’m using these methods like this:

static void Main(string[] args)
{
    int[] a = { 5, 4, 3, 1 };
    Swap(a[1], a[2]);
    Swap(a, 1, 2);
}

Which of these methods is more efficient and why?

  • 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-23T04:52:19+00:00Added an answer on May 23, 2026 at 4:52 am

    Your method will not swap any parameters at all. I mean it will swap parameters inside your method, but it will not affect the values of source parameters. That’s because value types are copied when they are passed into methods. Here’s the example:

    void IncorrectSwap(int a, int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    
    void HereHappensNothing()
    {
        int a = 1;
        int b = 2;
        IncorrectSwap(a, b);
        // a still = 1, and b = 2, nothing happens
    }
    

    To make your method work you have to pass value types by reference like that:

    void CorrectSwap(ref int a, ref int b)
    {
        int temp = a;
        a = b;
        b = temp;
    }
    
    void HereSwapHappens()
    {
        int a = 1;
        int b = 2;
        CorrectSwap(ref a,ref b);
        // a = 2, and b = 1, Ok.
    }
    

    Here you can read about value types and how to work with them.

    Update

    Following your update. I don’t think there should be any significant difference in performance as long as value types don’t get boxed when passed by ref. There can be some penalty when you pass more parameters, but I don’t think it should be significant, you will not see the difference.

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

Sidebar

Related Questions

I've written this method which should check for file changes. public static void watch()
Check out this test: [TestFixture] public class Quick_test { [Test] public void Test() {
public static void fillCheckList(string ListType,int RecordNum,CheckBox chkRequired,TextBox txtComplete,TextBox txtMemo) { string sql_Check = String.Format(@SELECT
public static void check(){ String name; System.out.println(Enter Customer Name to CHECK RESERVATION ticket for
I'm using this to check for the availability of a URL: $fp = fsockopen($url,
static void Main(string[] args) { List<int> listArray = new List<int>(); listArray.Add(100); foreach (int item
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
First check out this code. I seems like it should work for me, but
I've been struggling with this check constraint for a few hours and was hoping
I receive this Run-Time Check Failure upon the return in the following code. I

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.