C# // Pass by value (in, default), reference
//(in/out), and reference (out)
void TestFunc(int x, ref int y, out int z) {
x++;
y++;
z = 5;
}
int a = 1, b = 1, c; // c doesn't need initializing
TestFunc(a, ref b, out c);
System.Console.WriteLine("{0} {1} {2}", a, b, c); // 1 2 5
// Accept variable number of arguments
int Sum(params int[] nums) {
int sum = 0;
foreach (int i in nums)
sum += i;
return sum;
}
int total = Sum(4, 3, 2, 1); // returns 10
/* C# doesn't support optional arguments/parameters.
Just create two different versions of the same function. */
void SayHello(string name, string prefix) {
System.Console.WriteLine("Greetings, " + prefix + " " + name);
}
void SayHello(string name) {
SayHello(name, "");
}
Exception Handling
VB.NET
'Deprecated unstructured error handling
On Error GoTo MyErrorHandler
...
MyErrorHandler: System.Console.WriteLine(Err.Description)
Dim ex As New Exception("Something has really gone wrong.")
Throw ex
Try
y = 0
x = 10 / y
Catch ex As Exception When y = 0 'Argument and When is optional
System.Console.WriteLine(ex.Message)
Finally
DoSomething()
End Try
上一页 [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] 下一页
|