[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 577
  • Last Modified:

How to pass ArrayList as ref?

hi-

i have a synched ArrayList

ArrayList synchedResultList = ArrayList.Synchronized(resultList);

i pass it to a method like so

myMethod(ref synchedResultList);

and my method is declared like

void myMethod(ref ArrayList list)...

it appears that only a copy of the list
is being passed back.

thanks - bg
0
BeerGod
Asked:
BeerGod
  • 2
1 Solution
 
naveenkohliCommented:
What did you expect to be returned? What exactly are you trying to do in myMethod? When you will pass a argument by "ref" that means memory will be allocated by client. And if you will pass in "null" argument value, then method can allocated memory for your array.
If you could give more detaila bout the problem, probabl somebody can point out the problem.

Naveen
0
 
AibrasCommented:
class Class1
     {
          // ArrayList is "ref" Type By Default Because it is Class
          // and all Classes is "ref" type
          // try this code

          static void Main(string[] args)
          {
               ArrayList arr = new ArrayList();

               refArrayList(arr); // Passing by value arr is "ref" by default

               Console.WriteLine(arr[0]);


          }
          private static void refArrayList(ArrayList A)
          {
               A.Add(15);
          }
0
 
BeerGodAuthor Commented:
Thanks. Which objects are pass-by-value and which are pass-by-ref (by default)?
0
 
AibrasCommented:
Any primitive data types can be passed by
value
ex:
int i=0;
myMethod(i);
float x=1.22F;
myMethod2(x);
if you want to pass it as a "ref" type
you can prefix it with "ref" keyword
ex:
int i=10;
myMethod3(ref i)
.
.
.
static void myMethod3(ref arg)
{...}

you must know that int,float,long .... are struct types
that mean you can pass it as value because struct types store in stack
or as a ref type by using ref keyword
but
object,string,ArrayLlist,Stack,Queue.... are
class type that mean you can pass them as a value
and change any thing in them as a classes "ref by default
" because class types store in heap
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now