How to implement call-by-reference in C# ?

Posted on 2003-03-16
Medium Priority
Last Modified: 2009-08-31
plz look at the following code.

class Test
     public static void swap(int &a,int &b)
          int c=a; a=b; b=c;
     public static void Main()
          int x=10,y=20;
          System.Console.WriteLine("x={0} and y={1}",x,y);

   It is not working. I tried the same
swap function using pointers too as follows

     public static void swap(int *p,int *q)
          int c=*p; *p=*q; *q=c;
Question by:balakiran_bs
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Lagi earned 200 total points
ID: 8146890
You should use

void swap(ref int a, ref int b)
  int c = a;
  a = b;
  b = c;

static void main()
  int a = 10;
  int b = 20;
  swap(ref a, ref b);

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question