We help IT Professionals succeed at work.



I'm checking out a web application developped by an outsorcing company.. well that proyect doesn't work fine for many reasons . It's writting in vb.net and suddenly I realized that they pass ALL Objects by val (let's say that an object is a List of 10000 objects , where every object is a query record) . I remember the times when I read from the first (Visual Basic 5) about passing parameters that you've got to pass objects as ref and primitive variables as by value.. but now I see this .... and all methods pass objects as byval.. there might be about 200 methods in that proyect

Can anyone tell me that I'm not that crazy
Watch Question

IT Pro/Developer
Top Expert 2008
Hi edwbear1976a,

ByVal it's the way dy default. You can use ByVal or ByRef. Check in here the diferences http://msdn2.microsoft.com/en-us/library/ddck1z30(vs.71).aspx

From my understanding, .net differs from c++ in that byref in C++ passes only a reference (read:  pointer) to the object, and .net passes a copy of the object.  The copy is destroyed on completion of the funciton call if passed byval and copied back if passed byref.  That's why byref is slower than byval.
You don't really need to be worried.

.NET has 2 different "types" of variables / data, value types and reference types.
value types are basic values (int, float, double, etc..), and references are classes, objects, or basically anything more complex.

When you declare a reference type (ie a list), you actually only get a pointer or "reference" to the object, and not the object itself.  So when you pass a list into a function as byval, the "value" you're passing in is still just a pointer to the list, and not the data in the list itself.
You can still modify / add / remove items from the list, and the changes will affect the original object.

THe only time you need to specify byref for reference types, is if you're going to actually change the object itself, and not just it's contents.  For example, if you passed in a null list, and set it to "new List()" inside the function, then those changes will not propogate.


Hi you guys and thanx for the comments
well I'm worried for things that I always knew and andrewneely says : -- That's why byref is slower than byval--. well I'm trying to improove that web application performance . I also now that There are 2 types of object no matter if you pass it it by val or by reference (Arrays,Classes : byRef) simple variables (Int32, Int64).. well what I have always understood is that when you pass by Ref and Object "byRef type" you store  in the stack a reference or "pointer" to the heap, but when you pass an object byVal  the value of that object is copied in the stack (Int32:640,char: "A" and so) so I've got to understand that no matter I pass an object (type by ref) as by val or by ref its behavior will be the same.
The Fact is that the proyect I'm checking out has got a lot of methods and they only pass objects byVal...I have to face that now things are different with .NET FRAMEWORK  and maybe for the best.
Jorge PaulinoIT Pro/Developer
Top Expert 2008
I think that the most important here is not the performance that you will achive changing one thing to another. The most important is, like as you can read in the posts, what you can do with a method and another. The most important thing regarding with performance are the database handling over the web page.
It is one thing to be aware of the trade-offs in a programming language as it pertains to performance.  It's quite another to try to refactor code to be faster when it's "fast enough" already.  jpaulino is right to say "The most important thing regarding with performance are the database handling over the web page"

There are a few areas where optimizaton for speed is critical.  DB access, routines that need to deal with real world events as they happen, and inside of loops that have hunderds or thousands of iterations.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.