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. beginner: array --> function

beginner: array --> function

Scheduled Pinned Locked Moved C#
questioncsharpc++data-structureslearning
4 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.
  • D Offline
    D Offline
    dkoder
    wrote on last edited by
    #1

    hello!!! looked on the net but couldn't find an answer yet.... in c++ i used pointers to hand an array over to a function. what is the equivalent in c#? say i want to pass a simple int[]. how does it work with >return<. so far i declered an array in the class as private, similar to a global variable. however i don't think this is the way forward. many thanks in advance, Dominik

    D 1 Reply Last reply
    0
    • D dkoder

      hello!!! looked on the net but couldn't find an answer yet.... in c++ i used pointers to hand an array over to a function. what is the equivalent in c#? say i want to pass a simple int[]. how does it work with >return<. so far i declered an array in the class as private, similar to a global variable. however i don't think this is the way forward. many thanks in advance, Dominik

      D Offline
      D Offline
      Daniel Turini
      wrote on last edited by
      #2

      Since C# is garbage-collected, you don't need to worry about passing pointers and dealing with memory, like you did in C++: simply pass the array as an argument or return it on a function and the framework will clean it automatically when you're not using it anymore. See this sample function:

      	int[] doubleValues(int[] values)
      	{
      		int[] retval = new int[values.Length];
      		for (int i = 0; i < values.Length; i++)
      			retval[i] = values[i] * 2;
      		return retval;
      	}
      

      Yes, even I am blogging now!

      D 1 Reply Last reply
      0
      • D Daniel Turini

        Since C# is garbage-collected, you don't need to worry about passing pointers and dealing with memory, like you did in C++: simply pass the array as an argument or return it on a function and the framework will clean it automatically when you're not using it anymore. See this sample function:

        	int[] doubleValues(int[] values)
        	{
        		int[] retval = new int[values.Length];
        		for (int i = 0; i < values.Length; i++)
        			retval[i] = values[i] * 2;
        		return retval;
        	}
        

        Yes, even I am blogging now!

        D Offline
        D Offline
        dkoder
        wrote on last edited by
        #3

        thanks Daniel, i am already aware of the garbage collection and memory management. however, is there not a more efficient way to pass arrays to a function??? a pointer takes 4 bytes, whilst my 1k array of a class using doubles takes much more memory. thanks, Dominik

        D 1 Reply Last reply
        0
        • D dkoder

          thanks Daniel, i am already aware of the garbage collection and memory management. however, is there not a more efficient way to pass arrays to a function??? a pointer takes 4 bytes, whilst my 1k array of a class using doubles takes much more memory. thanks, Dominik

          D Offline
          D Offline
          Daniel Turini
          wrote on last edited by
          #4

          dkoder wrote: however, is there not a more efficient way to pass arrays to a function??? a pointer takes 4 bytes, whilst my 1k array of a class using doubles takes much more memory. Actually, you're not passing a copy of the array: just like in C++, you're passing a pointer to the array. In C#, everything, but the native types (int, char, double, float, decimal) are pointers under the hood. Try changing the array inside the function and you'll see that what you're passing is actually a pointer. Yes, even I am blogging now!

          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