This way is more instructive?
---------------------------------------
<code>
using System;
namespace refArray
{
class Class1
{
private static int[,] intArr = new int[2,3];
[STAThread]
static void Main(string[] args)
{
intArr[0,1] = 44;
int result = RefArray( intArr );
Console.WriteLine( intArr[0,1] );
}
public static int RefArray( int[,] refarr ) {
refarr[0,1] = 99;
return refarr[0,1];
}
}
}
</code>
---------------------------------
Other reference types are *class, interface, delegate, object, string*.
Array is not in the list because there's no array type in C# (you define
double[], bool[], string[,] etc.). When you pass an *instance* of any of
those types as argument, 'ref' is irrelevant.
Try to compile this one. Before accusing me of not understanding see if
you
understand what you're talking about.
Cheers.
-----------------------------------------------
<code>
using System;
namespace refArray
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
private static int[,] intArr = new int[2,3];
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
intArr[0,1] = 44;
int result = RefArray( intArr );
Console.WriteLine( result.ToString() );
}
public static int RefArray( int[,] refarr ) {
refarr[0,1] = 99;
return refarr[0,1];
}
}
}
</code>
---------------------------------
AHN said:
That compiles and works. Just try.
I have. Have you? Try compiling the following, and watch it fail.
using System;
public class Test
{
static int ReadFile(ref ushort[,] nArray,
string sFname, int w, int h)
{
// Implementation skipped
return 0;
}
static void Main()
{
ushort[,] defArray = null;
string defFileName = null;
int w = 0;
int h = 0;
int blah = ReadFile( defArray, defFileName, w, h);
}
}
I get the following with 2.0 beta 2:
Test.cs(18,20): error CS1502: The best overloaded method match for
'Test.ReadFile(ref ushort[*,*], string, int, int)' has some invalid
arguments
Test.cs(18,30): error CS1620: Argument '1' must be passed with the
'ref' keyword
And this with 1.1:
Test.cs(17,20): error CS1502: The best overloaded method match for
'Test.ReadFile(ref ushort[*,*], string, int, int)' has some
invalid arguments
Test.cs(17,30): error CS1503: Argument '1': cannot convert from 'ushort
[*,*]' to 'ref ushort[*,*]'
Both seem reasonably clear to me.
Array variable is the pointer to the address. So when you give it as
argument, anithing done to it in the function body is done to the
array itself.
Changes to the value of the parameter itself, however (rather than the
array it references) will not be visible to the caller unless the
parameter is passed by reference.
No 'ref' is needed. Ref is needed if your variable is not a reference
type. Array is a reference type. Cheers.
I'm sorry, but you clearly don't understand what "ref" actually means.
Once again, please read
http://www.pobox.com/~skeet/csharp/parameters.html