Csharp/C Sharp/Language Basics/Function Parameters — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 18:31, 26 мая 2010
Содержание
Arrays as Function Returns and Parameters
<source lang="csharp"> using System; public class Starter {
public static void Main() { int[] zArray = { 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }; string[] xArray = { "a", "b", "c", "d" }; Console.WriteLine("List Numbers"); MyClass.ListArray(zArray); Console.WriteLine("List Letters"); MyClass.ListArray(xArray); Console.WriteLine("Total Numbers"); MyClass.Total(zArray); }
}
public class MyClass {
public static void ListArray(Array a) { foreach (object element in a) { Console.WriteLine(element); } } public static void Total(int[] iArray) { int total = 0; foreach (int number in iArray) { total += number; } Console.WriteLine(total); }
}
</source>
creates instances of a value and a reference type
<source lang="csharp"> using System; class Starter {
static void Main() { int localvalue = 5; MyClass objZ = new MyClass(); DisplayType(localvalue); DisplayType(objZ); } static void DisplayType(object parameterObject) { Type parameterType = parameterObject.GetType(); string name = parameterType.Name; Console.WriteLine("Type is " + name); if (name == "MyClass") { ((MyClass)parameterObject).Display(); } }
} class MyClass {
public void Display() { Console.WriteLine("MyClass::Display"); }
}
</source>
Passing parameters to methods
<source lang="csharp"> using System; class ParameterTest {
static void SomeFunction(int[] ints, int i) { ints[0] = 100; i = 100; } public static void Main() { int i = 0; int[] ints = { 0, 1, 2, 4, 8 }; Console.WriteLine("i = " + i); Console.WriteLine("ints[0] = " + ints[0]); Console.WriteLine("Calling SomeFunction..."); SomeFunction(ints, i); Console.WriteLine("i = " + i); }
}
</source>
Pass integer by reference
<source lang="csharp"> using System; public class Starter {
public static void MethodA(ref int parameter) { parameter = parameter + 5; } public static void Main() { int var = 2; MethodA(ref var); Console.WriteLine(var); // 7 }
}
</source>
Pass valuel by pointer
<source lang="csharp"> using System; public class Starter {
public unsafe static void Main() { int val = 5; int* pA = &val; int* pB; pB = MethodA(pA); Console.WriteLine("*pA = {0} | *pB = {0}", *pA, *pB); } public unsafe static int* MethodA(int* pArg) { *pArg += 15; return pArg; }
}
</source>
ref pointer parameter
<source lang="csharp"> using System; public class Starter {
public unsafe static void Main() { int val = 5; int* pA = &val; Console.WriteLine("Original: {0}", (int)pA); MethodA(pA); Console.WriteLine("MethodA: {0}", (int)pA); MethodB(ref pA); Console.WriteLine("MethodB: {0}", (int)pA); } public unsafe static void MethodA(int* pArg) { ++pArg; } public unsafe static void MethodB(ref int* pArg) { ++pArg; }
}
</source>
Use out to mark an object parameter
<source lang="csharp"> using System; class XInt {
public int iField = 5;
} class Starter {
public static void MethodA(out XInt alias) { XInt inner = new XInt(); alias = inner; } public static void Main() { XInt obj; MethodA(out obj); Console.WriteLine(obj.iField); // 5 }
}
</source>
Use ref to mark an object parameter
<source lang="csharp"> using System; public class XInt {
public int iField = 2;
} public class Starter {
public static void MethodA(ref XInt alias) { XInt inner = new XInt(); inner.iField = 5; alias = inner; } public static void Main() { XInt obj = new XInt(); MethodA(ref obj); Console.WriteLine(obj.iField); // 5 }
}
</source>