function----Beginning Visual C #

Source: Internet
Author: User

for more relevant articles, see my personal homepage: zhongxiewei.com

Syntax differs when function passing arguments are passed by reference

//in Cpp  void  doubleval (int  &val) {//... }    int  Main () {int  val = 20 ;    Doubleval (Val); cout << Val << Endl; //output:40 }  
// in c sharpstaticvoiddoubleVal(refint// 使用ref进行标识{    2;    }staticvoidMain(string[] args){    int20;    doubleVal(ref val);    Console.WriteLine// output: 40}

Out keyword, there are no similar keywords in C + + or usage of the Out keyword is similar to ref, but there are two points, 1) It is not legal to pass parameters that are not assigned when using the ref parameter. Failed to compile. However, it is legal to use the out parameter. 2) and when an out parameter is used, the argument is treated as if it is not assigned, and a compile error occurs on the call to that value. As follows:

staticvoiddoubleVal(outint val){    2;   // 想要使用val中的数值,                // 但是val被认为是没有赋值的,因此出现编译错误 }staticvoidMain(string[] args){    int val=3;    doubleVal(out val);    Console.WriteLine// output: 40}

For the non-quantitative parameter input keyword is: param, the specific implementation is as follows:

staticintsumint// 这里需要注意的是,param参数必须在参数列表的最后的位置{    int0;    foreach (intin num)    {        sum += n;    }    return sum;}

In C #, the detection of variable scopes is more stringent, such as code like the following can be compiled in C + + and run successfully, but in C # can not be compiled, because in the same scope called the non-initialized variable.

string text;for (int i=0; i<1; i++){    "hello";}Console.WriteLine// in cpp: cout << text << endl;

function overloading is similar in C + +, but because C # has introduced the REF keyword for reference, a function that references parameters and non-referenced arguments can be overloaded in C #, which is unreasonable in C + +. Such as:

// in c#staticvoidshowDouble(refint val) {}staticvoidshowDouble(int val) {}// in cpp is errorvoidshowDouble(int &val) {}voidshowDouble(int val) {}

In C # about functions, there are also keywords that are delegate meant to create something that can be used to represent other functions. It's kind of like a pointer function in C. Such as:

//In C #Delegate Double processdelegate(DoublePARAM1,DoublePARAM2);Static Double Multiply(DoublePARAM1,DoublePARAM2) {returnparam1 * PARAM2;}Static Double Divide(DoublePARAM1,DoublePARAM2) {returnPARAM1/PARAM2;}Static void Main(string[] args) {processdelegate process; Process =New processdelegate(Multiply); Console.WriteLine(' {0} ',Process(3,4)); Process =New processdelegate(Divide); Console.WriteLine(' {0} ',Process(3,4));}
// in cpp#include <iostream>usingnamespace std;int multi(intint b) {    return a * b;}int add(intint b) {    return a + b;}int main() {    int (*fn)(int,int);        fn = multi;    "multi " << fn(2,3) << endl;    fn = add;    "add " << add(2,3) << endl;}

function----Beginning Visual C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.