Hi,
C# is very like C and C++, but not identical. C# doesn't have pointers, instead it has references, like C++ does.
Using the ref keyword has the disadvantage of insisting that the value be initialized before being passed to the method. Fortunately, the out keyword saves us. Hereâs a simple program using references.
using System;
public class SwapExample
{
static void swap(ref int x, ref int y, out int sum)
{
int temp;
temp = x;
x = y;
y = temp;
sum = x + y;
}
public static void Main()
{
int x = 1, y = 2, sum ;
SwapExample.swap(ref x,ref y, out sum);
Console.WriteLine("(after swap) x = {0} y = {1} sum = {2}",
x,y,sum);
}
}
You can do at least as much in C# and in C and C++, but slightly differently.
HtH
Sandra MacGregor
|