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. C#
  4. C# Parameter

C# Parameter

Scheduled Pinned Locked Moved C#
csharpdata-structureshelpquestion
6 Posts 5 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.
  • P Offline
    P Offline
    parth p
    wrote on last edited by
    #1

    Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e. private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString(); So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!

    - Stop thinking in terms of limitations and start thinking in terms of possibilities -

    Q B L P 4 Replies Last reply
    0
    • P parth p

      Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e. private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString(); So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!

      - Stop thinking in terms of limitations and start thinking in terms of possibilities -

      Q Offline
      Q Offline
      qyy
      wrote on last edited by
      #2

      C# parameters are passed by value. But there are some sutle things here. C# has reference-base objects and value-based objects. For the reference-based object, you can think it as an object holding an address. So, when you pass them to a function, only the address will be copied. For your code, the last line passed arNum to iAnswer. iAnswer copy the address holded by arNum to it's own parameter, arData. When you sort arData, the real object that is pointed by arData and arNum will be changed.

      1 Reply Last reply
      0
      • P parth p

        Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e. private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString(); So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!

        - Stop thinking in terms of limitations and start thinking in terms of possibilities -

        B Offline
        B Offline
        Bert delaVega
        wrote on last edited by
        #3

        The ArrayList is a object, so it's allocated to the heap (a reference type). So in your case it is by ref. Some further reading: http://msdn2.microsoft.com/en-us/library/3ewxz6et(VS.71).aspx[^]

        G 1 Reply Last reply
        0
        • P parth p

          Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e. private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString(); So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!

          - Stop thinking in terms of limitations and start thinking in terms of possibilities -

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, if you are familiar with older languages such as C, it is very similar: 1. simple things (int, double) get copied (pass by value); 2. for complex things (strings, class instances) a pointer is passed (so there is no object copied, it is the same object you get). Whether you call this by value or by ref however is a bit confusing (you can optionally add a ref keyword to increase the level of indirection by one) :)

          Luc Pattyn [Forum Guidelines] [My Articles]


          This month's tips: - before you ask a question here, search CodeProject, then Google; - the quality and detail of your question reflects on the effectiveness of the help you are likely to get; - use PRE tags to preserve formatting when showing multi-line code snippets.


          1 Reply Last reply
          0
          • P parth p

            Hi, Can anyone tell me how the normal parameter in C# are passed? is it by Ref? or just value? Cuz i'm getting a bit problem with my code. I have this code that calculates statistics and for that i need to sort() my array. but this method also (somehow) is being applied to the array that i passed into the function... for i.e. private int iAnswer(ArrayList arData) { arData.Sort(); } text1.Text = iAnswer(arNum).ToString(); So what i'm trying to say is that when i do arData.Sort() it also sorts arNum somehow??!!

            - Stop thinking in terms of limitations and start thinking in terms of possibilities -

            P Offline
            P Offline
            parth p
            wrote on last edited by
            #5

            Thank you all for your reply. It really helped me to understand this concept. :)

            - Stop thinking in terms of limitations and start thinking in terms of possibilities -

            1 Reply Last reply
            0
            • B Bert delaVega

              The ArrayList is a object, so it's allocated to the heap (a reference type). So in your case it is by ref. Some further reading: http://msdn2.microsoft.com/en-us/library/3ewxz6et(VS.71).aspx[^]

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              Bert delaVega wrote:

              The ArrayList is a object, so it's allocated to the heap (a reference type). So in your case it is by ref.

              That is not correct. It's a reference that is passed as a parameter, but it's not passed by reference. The reference itself is copied and sent as a parameter, it's not a reference to the reference that is sent as a parameter.

              Despite everything, the person most likely to be fooling you next is yourself.

              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