none
Array of References RRS feed

  • Frage

  • Hallo,

    I'm looking for a way to pass an array of objects to a function to initialize these objects, but it seems as if I'm only passing the values of these objects:

    class Program
    {
        static void Main(string[] args)
        {
            string a = null;
            string b = null;
            string c = null;
    
            string[] arr = new string[]{a,b,c};
    
            Console.WriteLine(a + b + c);
            Console.ReadLine();
        }
    
        private static void initValues(string[] arr)
        {
            for(int i = 0; i < arr.Length; i++)
            {
                arr[i] = "Hello World!\n";
            }
        }
    }
    This programm prints an empty line, but I would like to have it printing "HelloWorld!" three times.
    Is there a way to achieve that without using the "unsafe"-Tag?
    I need a way to pass an array with references, so that the original variable gets updated.

    Thanks for your help in advance.

    Greetings

    Yampersand
    Donnerstag, 6. August 2009 16:32

Antworten

  • Hello Yampersand,
    Is there a way to have an array filled with "out"-references?
    No there isn't. CLR Arrays only store references of objects.
    An out (or ref ) would mean to store the address location of a variable,
    which is an unsupported operation, because it could lead to corruption.

    See also Common Type System

    HTH Elmar



    • Als Antwort markiert Jan Handrich Freitag, 7. August 2009 09:27
    Freitag, 7. August 2009 08:19
    Beantworter

Alle Antworten

  • Hallo Yampersand,

    as the three variables a, b, c are of no real use,
    i think you are looking for something like:
    	string[] arr = new string[3];
    	initValues(arr);
    
            Console.WriteLine(String.Concat(arr));
    
    For a articles about parameter passing in C# programming have a look at:
    http://www.yoda.arachsys.com/csharp/parameters.html
    http://rapidapplicationdevelopment.blogspot.com/2007/01/parameter-passing-in-c.html

    HTH Elmar
    Donnerstag, 6. August 2009 21:56
    Beantworter
  • Hello Elmar,

    thank you for your answer. I guess my example code is a little bit missleading. I used the strings only as an example for any kind of custom object und the "Console.WriteLine"-Methode as an example for a later use of this object.

    Your second article was quite helpful. I found the keyword "out" which seems to do exactly what I want. Is there a way to have an array filled with "out"-references?

    Thanks

    Yampersand
    Freitag, 7. August 2009 07:37
  • Hello Yampersand,
    Is there a way to have an array filled with "out"-references?
    No there isn't. CLR Arrays only store references of objects.
    An out (or ref ) would mean to store the address location of a variable,
    which is an unsupported operation, because it could lead to corruption.

    See also Common Type System

    HTH Elmar



    • Als Antwort markiert Jan Handrich Freitag, 7. August 2009 09:27
    Freitag, 7. August 2009 08:19
    Beantworter
  • Hello Elmar,

    thx alot for this information.

    Greetings

    Yampersand
    Freitag, 7. August 2009 09:27