Parameter passing playground

Getting used to how parameter passing works isn't really simple. Especially if you didn't program so often, every now and then you have to check back how things works. If you really want to understand how things work I recommend you CLR via C# by Jeffrey Richter.

First thing to remember is the difference between value and reference types. The first lives in the stack(int, short, struct), the second on the heap (string, your class types).

   1: class Program
   2: {
   3:     static void Main(string[] args)
   4:     {
   5:         string callerString1 = "CallerValue1";
   6:         string callerString2 = "CallerValue2";
   7:         string callerString3 = callerString1;
   8:         string callerString4 = callerString2;
   9:  
  10:         string stringBefore = "callerString1=" + callerString1 + " callerString2=" + callerString2 + " callerString3=" + callerString3 + " callerString4=" + callerString4;
  11:  
  12:         int int1 = 8;
  13:         int int2 = 9;
  14:         int int3 = int1;
  15:         int int4 = int2;
  16:         string intBefore = "int1=" + int1 + " int2=" + int2 + " int3=" + int3 + " int4=" + int4;
  17:  
  18:         MyType objTest1 = new MyType();
  19:         MyType objTestTemp1 = new MyType();
  20:         objTest1.X = 10;
  21:         objTestTemp1 = objTest1;
  22:  
  23:         string objectBefore1 = "objTest1.X=" + objTest1.X + " objTestTemp1.X=" + objTestTemp1.X;
  24:  
  25:         MyType objTest2 = new MyType();
  26:         MyType objTestTemp2 = new MyType();
  27:         objTest2.X = 11;
  28:         objTestTemp2 = objTest2;
  29:  
  30:         string objectBefore2 = "objTest2.X=" + objTest2.X + " objTestTemp2.X=" + objTestTemp2.X;
  31:  
  32:         prova(callerString1, ref callerString2, int1, ref int2, objTest1, ref objTest2);
  33:  
  34:         string stringAfter = "callerString1=" + callerString1 + " callerString2=" + callerString2 + " callerString3=" + callerString3 + " callerString4=" + callerString4;
  35:         string intAfter = "int1=" + int1 + " int2=" + int2 + " int3=" + int3 + " int4=" + int4;
  36:         string objectAfter1 = "objTest1.X=" + objTest1.X + " objTestTemp1.X=" + objTestTemp1.X;
  37:         string objectAfter2 = "objTest2.X=" + objTest2.X + " objTestTemp2.X=" + objTestTemp2.X;
  38:  
  39:         Console.Out.WriteLine(stringBefore);
  40:         Console.Out.WriteLine(stringAfter);
  41:         Console.Out.WriteLine(intBefore);
  42:         Console.Out.WriteLine(intAfter);
  43:         Console.Out.WriteLine();
  44:         Console.Out.WriteLine(objectBefore1);
  45:         Console.Out.WriteLine(objectAfter1);
  46:         Console.Out.WriteLine();
  47:         Console.Out.WriteLine(objectBefore2);
  48:         Console.Out.WriteLine(objectAfter2);
  49:  
  50:     }
  51:  
  52:     private static void prova(string string1, ref string string2, int methodInt1, ref int methodInt2, MyType methodObjProva1, ref MyType methodObjProva2)
  53:     {
  54:         string string3 = "MethodValue5";
  55:         string1 = "MethodValue1";
  56:         string1 = string3;
  57:         string1 = "MethodValue2";
  58:  
  59:         string string4 = "MethodValue6";
  60:         string2 = "MethodValue3";
  61:         string2 = string4;
  62:         string2 = "MethodValue4";
  63:  
  64:         methodInt1 = 1;
  65:         int internalInt = 10;
  66:         methodInt1 = internalInt;
  67:  
  68:         methodInt2 = 1;
  69:         methodInt2 = internalInt;
  70:  
  71:         MyType internalMyType1 = new MyType();
  72:         internalMyType1.X = -3;
  73:  
  74:         methodObjProva1.X = -4;
  75:  
  76:         methodObjProva1 = internalMyType1;
  77:         methodObjProva1.X = -5;
  78:  
  79:         MyType internalMyType2 = new MyType();
  80:         methodObjProva2.X = -6;
  81:         methodObjProva2 = internalMyType2;
  82:         methodObjProva2.X = -7;
  83:  
  84:     }
  85: }
  86: public class MyType
  87: {
  88:     int x = 0;
  89:  
  90:     public int X
  91:     {
  92:         get { return x; }
  93:         set { x = value; }
  94:     }
  95: }

 

tbc

posted @ sabato 9 febbraio 2008 02:38

Print
Comments have been closed on this topic.
«luglio»
domlunmarmergiovensab
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678