(3). C # several key words static,ref&out,get&set,readonly

Source: Internet
Author: User

Static

Static fields, which belong to a class, can be used by all instances of the class.
Static fields are sometimes used as simple caches.
A static class is a constraint, and the methods and fields inside the constraint class must be static, such as some commonly used mathematical formulas such as sin (), Cos (), which can be encapsulated in a static class.

Ref&out

The REF keyword enables parameters to be passed by reference. The effect is that when control is passed back to the calling method, any changes to the parameters in the method are reflected in the variable. To use the ref parameter, both the method definition and the calling method must explicitly use the REF keyword. And the variables must be initialized before the arguments are passed with ref.

The Out keyword causes parameters to be passed by reference. This is similar to the REF keyword, except that the ref requires that the variable be initialized before it is passed. To use an out parameter, both the method definition and the calling method must explicitly use the Out keyword.

Although ref and out are handled differently at run time, they are handled the same way at compile time. Therefore, if one method takes a ref parameter and the other method takes an out parameter, the two methods cannot be overloaded.

Get&set

C # Get set does not advocate setting the domain's protection level to public and allowing the user to do anything outside of the class-too oo, or too insecure! For all fields that are necessary to be visible outside the class, C # recommends using attributes to express. Property does not represent a storage location, which is the fundamental difference between a property and a domain. And the two must be declared under the same property name, and they cannot be detached

ReadOnly

You need to initialize using the static constructor method, but you don't want the keyword to readonly when the value is changed.

ReadOnly members can only be assigned in the following two cases: 1. Static Constructor 2. A variable initializer

(3). C # several key words static,ref&out,get&set,readonly

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.