Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

Arrays by ref (yes, yet another ?)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mr peanut - 09 Sep 2007 00:32 GMT
I see that array names are pointers and that a given array, say A[,], can be
altered within a sub when passed as an argument; whether it be by (ref
double[,] A) or by (double[,] A).

I do not clearly understand when to use (ref double[,] A) but I believe it
is when I want my sub to be able to change the identity of the array in
question (that is, change what A is pointing to). Is this correct?

More importantly for me is the question of which technique to use when I
know that I want the sub to operate on a specific array. Is there any
difference in data flow or efficiency between the two methods? Is there a
preferred SOP?
Arne Vajhøj - 09 Sep 2007 00:45 GMT
> I see that array names are pointers and that a given array, say A[,], can be
> altered within a sub when passed as an argument; whether it be by (ref
[quoted text clipped - 3 lines]
> is when I want my sub to be able to change the identity of the array in
> question (that is, change what A is pointing to). Is this correct?

Yes.

You can always change the content of the array the reference points to.

By using ref you can get the reference to point to another array.

> More importantly for me is the question of which technique to use when I
> know that I want the sub to operate on a specific array. Is there any
> difference in data flow or efficiency between the two methods? Is there a
> preferred SOP?

I would not expect any measurable performance difference.

I would only use ref where it is really needed, because in
general it makes the code more difficult to read.

Arne
Göran Andersson - 09 Sep 2007 01:12 GMT
> I see that array names are pointers and that a given array, say A[,], can be
> altered within a sub when passed as an argument; whether it be by (ref
[quoted text clipped - 3 lines]
> is when I want my sub to be able to change the identity of the array in
> question (that is, change what A is pointing to). Is this correct?

That's right.

> More importantly for me is the question of which technique to use when I
> know that I want the sub to operate on a specific array. Is there any
> difference in data flow or efficiency between the two methods? Is there a
> preferred SOP?

Just don't use the ref keyword unless you have to, or if you find that
it really makes things clearer.

When you send a reference type as an argument without the ref keyword, a
copy of the reference is sent. If you use the ref keyword a reference to
the reference is sent instead, which adds another level of indirection
whenever the object is used.

An example of where the ref keyword is used in the framework is the
Array.Resize method. The method actually doesn't resize the array (as
that is not possible in .NET), but creates a new array and copies the
data to it. The method could avoid the ref keyword by simply returning
the new array instead, but then the method would have to be renamed to
something like Array.CreateNewAndCopyTo to avoid misunderstandings.

Signature

Göran Andersson
_____
http://www.guffa.com


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.