Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Design and Architecture
  4. Arguments

Arguments

Scheduled Pinned Locked Moved Design and Architecture
csharpperformancequestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodingYoshi
    wrote on last edited by
    #1

    There are two ways to pass argument: byVal or byRef. (C# and VB.Net) However, I also know that all objects are passed by reference. So what happens when we pass an object byval--Does it make a difference? Also, when arguments are passed byVal it hinders performance since a copy needs to be made. ByVal also means the called method can modify the argument and it will not affect the original data. If a method only needs to read an argument but does not need to modify it, does it make sense to send it byref? But then how can you guarantee the data will not be modified in the called method; specially when the called method is being coded by another programmer? Does it come down to simply making a choice between integrity and performance? Or is there a convention or am I just confused?

    R 1 Reply Last reply
    0
    • C CodingYoshi

      There are two ways to pass argument: byVal or byRef. (C# and VB.Net) However, I also know that all objects are passed by reference. So what happens when we pass an object byval--Does it make a difference? Also, when arguments are passed byVal it hinders performance since a copy needs to be made. ByVal also means the called method can modify the argument and it will not affect the original data. If a method only needs to read an argument but does not need to modify it, does it make sense to send it byref? But then how can you guarantee the data will not be modified in the called method; specially when the called method is being coded by another programmer? Does it come down to simply making a choice between integrity and performance? Or is there a convention or am I just confused?

      R Offline
      R Offline
      Robert C Cartaino
      wrote on last edited by
      #2

      CodingYoshi wrote:

      However, I also know that all objects are passed by reference.

      That is not correct. By default, objects references are passed by value. Do not confuse variable data types (value types vs. reference types) with argument passing mechanisms (pass-by-value vs pass-by-reference). For example, arrays are reference types. When you create an array, a block of memory is allocated and your array variable contains a "reference" the to the memory area (typically a four-byte address). For example: In Visual Basic:

      Dim values(10) As Integer

      In C#:

      int[] values = new int[10];

      The variable, values, contains a reference to a block of ten integers. If you pass your array to a method as an argument, the reference to the array is passed by value (important). You can modify the elements of the underlying array but you cannot assign a new array to it. To illustrate what I am talking about, take a look at this C# code:

      using System;
      class Program
      {
      static void Main()
      {
      int[] values = { 1, 2, 3 };
      TestMethod(values);
      Console.WriteLine("{0}, {1}, {2}", values[0], values[1], values[2]);
      }

      static void TestMethod(int\[\] values)
      {
          int\[\] newArray = new int\[\] { 4, 5, 6 };
          values = newArray;
      }
      

      }

      This code will print "1, 2, 3". Why? First, an array containing { 1, 2, 3 } is created. The TestMethod() creates a new array with { 4, 5, 6 } and attempts to point your array reference to it. But when TestMethod() returns, the reference is not changed because the reference was passed by value. However, you can change the underlying data pointed to by the reference. If TestMethod() did this:

      static void TestMethod(int[] values)
      {
      values[0] = 4;
      values[1] = 5;
      values[2] = 6;
      }

      ... the values would be changed (i.e., it prints "4, 5, 6") because you passed a reference to the data contained in values, which TestMethod() is free to change.

      CodingYoshi wrote:

      when arguments are passed byVal it hinders performance since a copy needs to be made

      Nah. Performance differences are usually insignificant. Maybe if you need to pass really large value types (like large structures) and you really need to t

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups