Comparison between C ++ and C #: inline functions, anonymous Functions

Source: Internet
Author: User

Why use inline functions)
 

In C ++, the stack environment needs to be established for function calls to replicate parameters to protect the call site. When the return value is copied, the call site is restored. these operations require additional costs. how can we avoid frequent function calls,

The first thought may be to use the code in the function directly, instead of having to get a function somewhere and then call it. Of course, it is only feasible when the function is relatively simple. however, if you use that function in multiple places, replacing it all will cause code redundancy and lower readability. As a result, you think of using macros. Some simple functions are basically a combination of some expressions, you can use a macro to replace it all. in this way, the amount of code is less and the readability is stronger. however, macros are simply replaced and no extra checks are performed, such as checking whether function functions match. in this case, the inline function will be used. inline Functions combine the advantages of macros and functions. on the one hand, it will replace the function with the content in the function body, which does not need to call the extra overhead of the function, which is like a macro. on the other hand, it is in the compilation phase rather than the pre-compilation phase, so that the compiler can perform some security checks, such as parameter type checks.

 

Inline Function usage
 

Inline functions are easy to use. Before calling a function, declare the function as an inline function. For example

Inline void Print (); // declare the inline function. Of course, you can put together the Declaration and definition so that you do not need to define it elsewhere.

Int main ()

{

Print ();

 

}

Inline void Print () // The inline keyword can also be removed.

{

Cout <"this is inline function .;

}

In addition, it is said that if the function has complex control structures such as while and switch, or there are recursive calls. the inline function automatically fails. it will be called as a common function. however, this is not a good test. I don't know when it is actually inline, but when it is handled only by general functions.

Anonymous Functions
C # does not have the concept of inline functions, but there is a similar concept called anonymous functions.

A simple example


Delegate void MyDelegate (string name );

Class Program

{

Static void Print (string name)

{

Console. WriteLine ("hello," + name );

}

Static void Main (string [] args)

{

MyDelegate arwen = Print;

Arwen ("arwen ");

}

}

The proxy is used here, which is similar to a function pointer. when defining a proxy, You need to specify a function. We have to define another function somewhere. if the function is relatively simple, we do not need to define it elsewhere, but simply expand it in the local area. If you use an anonymous function, OK. for example

MyDelegate weiwen = delegate (string name) {Console. WriteLine ("hi," + name) ;}; // This is an anonymous Function

Weiwen ("weiwen ");

Lambda (lambda) expressions appear in C #3.0. You can further simplify the anonymous method.

MyDelegate weiwenhp = (xxx) =>{ Console. WriteLine ("hi," + xxx );};

Weiwenhp ("weiwenhp ");

This is even better. The function parameters can be inferred, so you can use xxx or yyy to represent the parameters.

Lambda expressions are borrowed from functional programming languages such as Lisp and Scheme. They are mainly used in Linq in C.

Related Article

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.