شنبه, ۲۸ مرداد ۱۳۹۶، ۱۲:۰۶ ب.ظ
پارامتر ref
همان طور که می دانید وقتی یک value type به عنوان آرگومان به متد فرستاده می شود، اگر تغییری در پارامتر ایجاد شود، تغییری در آرگومان ایجاد نمی شود.
اما اگر بخواهیم این تغییر ایجاد شود باید پارامتر ref را استفاده کنیم.
اما اگر بخواهیم این تغییر ایجاد شود باید پارامتر ref را استفاده کنیم.
class RefTest
{
// This method changes its argument. Notice the use of ref.
public void Sqr(ref int i)
{
i = i * i;
}
}
class Program
{
static void Main()
{
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine("a before call: " + a);
ob.Sqr(ref a); // notice the use of ref
Console.WriteLine("a after call: " + a);
}
}
{
// This method changes its argument. Notice the use of ref.
public void Sqr(ref int i)
{
i = i * i;
}
}
class Program
{
static void Main()
{
RefTest ob = new RefTest();
int a = 10;
Console.WriteLine("a before call: " + a);
ob.Sqr(ref a); // notice the use of ref
Console.WriteLine("a after call: " + a);
}
}
۹۶/۰۵/۲۸