Value, ref
Passing by Value
لما بنعمل Pass ل Argument داخل اى method بطريقة إفتراضية بيتم اخذ copy من ال Argument وعمل الشغل عليها لكن مش الأصلية !
كود:
static void Inc(int x) { x++; //increase the value of a copy !! } static void Main(string[] args) { int param = 10 ; Console.WriteLine(“The param equals : {0}”, param); Inc(param); Console.WriteLine(“Now the param equals : {0}”, param); Console.WriteLine(param); }
ليه ثابته ؟ لأن الزيادة كانت على مجرد Copy منها مش هى نفسها !
Passing by ref
كود:
static void Inc(ref int x) { x++; //it's like pointers here . } static void Main(string[] args) { int param = 10 ; Console.WriteLine(“The param equals : {0}”, param); Inc(ref param); //passing the address of param . Console.WriteLine(“Now the param equals : {0}”, param); Console.WriteLine(param); }
صراحة ال passing by ref كأنك بتستخدم Pointer . لأنك بتعمل Pass ل Memory address والتغيير بيحصل على القيمة المخزنة فى ال Memory Address .
ارجو إنه يكون واضح ، وشكرا .
تعليق