कोई भी ref
कीवर्ड के बिना निम्न कोड मानें, जो स्पष्ट रूप से वैरिएबल को प्रतिस्थापित नहीं करेगा, क्योंकि यह मान के रूप में पारित किया गया है।सी # में गुजरने वाले ऐरे पैरामीटर: संदर्भ के आधार पर यह स्पष्ट रूप से क्यों है?
class ProgramInt
{
public static void Test(int i) // Pass by Value
{
i = 2; // Working on copy.
}
static void Main(string[] args)
{
int i = 1;
ProgramInt.Test(i);
Console.WriteLine(i);
Console.Read();
// Output: 1
}
}
अब जब कि समारोह काम कर के रूप में उम्मीद है करने के लिए, एक हमेशा की तरह ref
कीवर्ड जोड़ सकते हैं:
class ProgramIntRef
{
public static void Test(ref int i) // Pass by Reference
{
i = 2; // Working on reference.
}
static void Main(string[] args)
{
int i = 1;
ProgramInt.Test(ref i);
Console.WriteLine(i);
Console.Read();
// Output: 2
}
}
अब मैं क्यों सरणी सदस्यों के रूप में लाजवाब करना जब कार्यों परोक्ष द्वारा पारित कर रहे हैं में पारित कर रहा हूँ संदर्भ। सरणी मूल्य प्रकार नहीं हैं?
class ProgramIntArray
{
public static void Test(int[] ia) // Pass by Value
{
ia[0] = 2; // Working as reference?
}
static void Main(string[] args)
{
int[] test = new int[] { 1 };
ProgramIntArray.Test(test);
Console.WriteLine(test[0]);
Console.Read();
// Output: 2
}
}
इसे विकी क्यों बनाते हैं? – jsmith