C ා is purely object-oriented, which is different from C + +. For example, the method declaration format of C ා is as follows:
[public] static return value type method name (parameter list)
{
Method body
}
public access modifier, which can be omitted
There are more modifiers than C + +, and they have to be decorated with static. Static functions can only access static members. Others are basically the same as C + +.
C ා has reference values similar to C + +, which can be realized by out and ref
(1) Use of out modifier parameters
using System; namespace out keyword { class Program { //Adding an out keyword before a function parameter indicates that the parameter will also be returned, which is similar to the C + + reference value passing. A parameter can have multiple out parameters static int testOut(int num, out string str) { str = "success"; return num; } static void Main(string[] args) { string str; //When parameters are passed to formal parameters, out must also be added int ret = testOut(12, out str); Console.WriteLine("ret = {0}, str = {1}", ret, str); Console.ReadKey(); } } }
(2) The usage of ref modifying parameter
using System; namespace ref keyword { class Program { static void setMoney(ref int money) { money += 1000; } static void Main(string[] args) { int salary = 2000; //When using ref, the parameter must be assigned outside the method, and the argument must be decorated with ref setMoney(ref salary); Console.WriteLine("salary = " + salary); Console.ReadKey(); } } }
(3) params
using System; namespace params Use of keywords { class Program { static void Main(string[] args) { int max = getMax(12, 45, 11, 89, 6); Console.WriteLine("The largest number is: " + max); Console.ReadKey(); } //params means that parameters can be passed into any number of int types to form an array //When params is used, the parameter list of a function has only one parameter. When there are multiple parameters, it can only be used as the last parameter static int getMax(params int[] array) { int max = 0; foreach(var it in array) { if (max <= it) max = it; } return max; } } }