Sunday, May 23, 2010

All of us when calling a method call it just like this “LoadChildrenOf(employee)”

Say the signature of the method is

Public List<Child>  LoadChildrenOf(Employee emp)
{
Return DatabasePortal.LoadChilds(emp);
}

And in this method u normally do not edit or change the emp object so why u pass it by value.

So what is meant by pass by value?

The method takes a local copy from the object u have sent to it, thus copying it into memory reserving again a space in memory for this copied object and give it the associated data, although here in the previous method u did not change the status(changing props. Or fields).then I want u to sent this object by REF

So what is meant by pass by ref?

Here the method will take a reference to the outside object not the object data itself, thus the object in the method will still references the outside object

In this case it’s preferable to pass the object by ref.

But if the code inside the method will change the object state so pass it by value, thus the method will work on its local copy and any changes will never affect the outside object and thus after the CLR out from the method the object will be out of scope but it will resides until GC comes and kill it.

C# fast-food by:

Eng.Waleed abou-zaid
Senior Software Developer
Harf Information Technology
MCSD.Net,MCAD.Net,MCP,A+,N+ certified
walid.abouzaid@gmail.com



0 comments: