View Full Version : value و reference
donya100
دوشنبه 29 بهمن 1386, 13:59 عصر
تفاوت valueو reference را کسی می تونه توضیح بده ؟
hassan razavi
دوشنبه 29 بهمن 1386, 14:07 عصر
منظورتون Value Param & Reference Parameters هست یا Value Type & Reference Type ها؟
SabaSabouhi
دوشنبه 29 بهمن 1386, 14:19 عصر
تفاوت valueو reference را کسی می تونه توضیح بده ؟
با سلام
اگر منظورت در پارامترها هست:
پارامترها رو مىتونى به دو صورت به رویهها ارسال کنى، byvalue و byreference فرقشون اینه که در حالت اول تغییر دادن مقدار پارامتر فقط در حوزهى تعریف رویه است و هنگام بازگشت به رویهى صدا زننده مقدار پارامتر همان مقدار اولیه است. در حالى که اگر به پارامتر را به صورت reference ارسال کرده باشى، تغییر دادن مقدار پارامتر، مقدار آن را در رویهى صدا زننده را نیز تغییر مىدهد.
زبان C استاندارد ( و حتا Ansi C ) فقط Call by value را پشتیبانى مىکرد. در #C یک نوع جدید پارامتر هم داریم به صورت out که مشخص مىکند این پارامتر فقط مقدار برگشتى دارد.
و اگر منظورت ValueType و ReferenceType هست، این یک تقسیمبندى در انواع گونهها هست. گونههاى ساده مانند int, char, bool, string و ساختارها از نوع ValueType و کلاسها از نوع ReferenceType هستن. اگه C کار کرده باشى درکش خیلى راحته، انواع Reference در واقع به نوعى آدرس و نشانى رو حمل مىکنن. تفاوتش بیشتر هنگام ارسال متغیرهایى از این انواع به رویهها خودش رو نشون مىده.
صبا صبوحى
hassan razavi
دوشنبه 29 بهمن 1386, 14:31 عصر
Value Type
Value types store the data on the stack, STACK is a place where data stores in fixed length such as int, float, etc. Every program has its own stack and no other program shares it, and it allocates on program execution. You can imagine Stack as a block acquired in memory and when the function of the program is called, all local variables to the calling function are pushed onto the program stack, to be latter popped off and retrieved. I remember one of my experiences in creating an COM in VB. I was using so many recursive functions and got the error "Out of Stack Space". This was due to Stack overflow, because in recursive functions there were so many variables which popped up onto the stack that the Stack overflowed so one must careful using Variables in recursive functions.
If we are assigning one value variable to the other variable, it creates two distinct copies of the same data on the stack. So now in the Stack there are two spaces allocated. The value type as it name implies copies the value to the other variable not the reference of the other value. In Value type if the first variable modifies in the program it doesn't modify the other variable value. This is the main theme of Value type that it copies the value and doesn't makes effect on the other variable if the first variable got changed.
Reference Type
Reference types stores the data on the heap. This is the second place where we can store the data in the .NET Framework. Reference type stores the reference of the type, not the value. So if the value of the first object modified it will also effect the other type. The type of reference type can be determined from values of self describing types, pointers, or interfaces types. Self describing types are further classified into array and class types.
vBulletin® v4.2.5, Copyright ©2000-1403, Jelsoft Enterprises Ltd.