Autor Beitrag
Felix2000
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 124



BeitragVerfasst: Do 27.10.11 11:11 
Hi Folks !

Muss ich bei Arrays in einer Methodenübergabe ByRef oder ByVal davorsetzen? Beispiel:

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
void methodexyz(ByRef int[] intarray){

   ...

}


oder

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
void methodexyz(ByVal int[] intarray){

   ...

}


Welche Variante ist korrekt unter der Prämisse, dass mein Array außerhalb dieser Methode deklariert und initialisiert wurde?

Greetz
Felix

Moderiert von user profile iconChristian S.: Code- durch C#-Tags ersetzt
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 27.10.11 11:15 
ByRef :?: Von welcher Sprache redest du hier? Oder meinst du eher ref?

Wenn du nur die Inhalte des Arrays in der Methode manipulieren willst brauchst du ref nicht. ref bzw. out bräuchtest du nur wenn du ein neues Array in der Methode erzeugen würdest un das via diesem Parameter zurückgeben willst.
Felix2000 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 124



BeitragVerfasst: Do 27.10.11 11:19 
Ach so sorry !

Ich habe bisher viel mit Java gemacht und dort gibt es die Optionen ByRef und ByVal.

Welche Variante wäre in C Sharp die dementsprechende?

Greetz
Felix
Ralf Jansen
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 4708
Erhaltene Danke: 991


VS2010 Pro, VS2012 Pro, VS2013 Pro, VS2015 Pro, Delphi 7 Pro
BeitragVerfasst: Do 27.10.11 11:24 
ByRef wäre wie gesagt entweder ref oder out. ByVal ist in C# der Default du musst/kannst den nicht explizit benennen.