Wrox Programmer Forums
|
VS.NET 2002/2003 Discussions about the Visual Studio.NET programming environment, the 2002 (1.0) and 2003 (1.1). ** Please don't post code questions here ** For issues specific to a particular language in .NET, please see the other forum categories.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VS.NET 2002/2003 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 20th, 2003, 03:46 PM
semiloof
Guest
 
Posts: n/a
Default Passing Reference Types by Value

Is it possible to pass an array (a reference type) as a parameter to a function by value? I simply want to pass a copy of an array through a function argument list - at present the function is destroying the array.

I could, of course, create a copy of the array and pass that through the argument list, but I feel that there must be a better solution.

Semiloof

 
Old December 22nd, 2003, 12:16 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Found this MSDN article concerning array types in .net. Here's an excerpt that addresses this topic:

Passing and Returning Arrays
    Arrays are always passed by reference to a method. Since the CLR doesn't support the notion of constant parameters, this means that the method is able to change the elements in the array. If you don't want to allow the method to modify the elements, then you must make a copy of the array and pass the copy into the method. Note that the Array.Copy method does a shallow copy, and therefore if the array's elements are reference types, the new array refers to the already existing objects.
    To obtain a deep copy, you may want to clone the individual elements, but this requires that each object's type implements the ICloneable interface. Alternatively, you could serialize each object to a System.IO.MemoryStream and then immediately deserialize the memory stream to construct a new object. Depending on the object's types, the performance of these operations can be prohibitive and not all types are serializable either.





Similar Threads
Thread Thread Starter Forum Replies Last Post
as,in and reference types error msg..... raykaash C# 2005 2 January 8th, 2008 07:14 AM
Passing by reference quiksilverhg Excel VBA 5 November 21st, 2005 03:49 AM
Passing clas by reference ThunderBird Visual C++ 5 August 6th, 2004 06:26 AM
Passing a class by reference James Diamond VB How-To 13 February 16th, 2004 06:07 AM
Passing by reference jacob ASP.NET 1.0 and 1.1 Basics 1 July 12th, 2003 05:07 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.