4th Lecture: Miscellaneous technologies and future development of C # Language

Source: Internet
Author: User

2005.9.12 Li Jianzhong

Agenda

Attribute accessors Protection Level

Namespace alias qualifier

Pragma indicator

Conditional features

Fixed-Length buffer

C #3.0 and Future Development

 

Changes in the protection level of the property accessors

C #2.0 allows us to use different access level modifiers for get and set accessors of an attribute:

Here, get is the public modifier, and set is the internal modifier. Name is the attribute access modifier, and get and set are the attribute accessors. This difference will bring about inconsistency. Therefore, pay attention to modifiers.

 

Notes

The access modifier of the application on the get or set must be "less than" the access modifier of the application on the property; "less than" means "more strict ", for example, protected is smaller than public.

For example, the Name modification in the above example cannot be protected, because internal and protected are of the same level and there is no relationship between them.

You can only specify an access modifier that is "smaller" than the access modifier on an attribute accessor (get or set.

For example, in the above example, we cannot add internal modification to get. Since get and set are both internal, the public modifier of the attribute Name is meaningless, which is a reasonable inference of the compiler.

You cannot specify any access modifier for the property accessor (get or set) for the attribute declaration in the interface. The default value is public.

The change rules at the protection level of the property accessors apply completely to the C # indexer.

 

Introduction of namespace alias qualifiers

C #2.0 allow us to use the namespace alias qualifier (: :) to avoid conflicts of types in different namespaces:

We 'd better not use the DoT number in the middle, although it can be used, but the introduction of the double colon in C # is to avoid using the DoT number.

 

Notes

When the namespace alias qualifier (: :) is used, if ZC: ArrayList is used, the compiler can ensure that this is a qualifier only applicable to the namespace alias, it is not classified as another type or a member qualifier (.).

If a ZC class happens to be used, the compiler will identify whether ZC. ArrayList is a member of ZC if ArrayList is used. This will be the result of a morining statement. Therefore, the efficiency of using double colons is improved a little. Instead of looking for the type, we can directly find the namespace alias. However, this is only a problem during compilation. It is more important to avoid ambiguous judgment and facilitate analysis.

The keyword global can be placed on the left of the namespace alias qualifier (: :), which allows the compiler to only search for all namespaces, rather than other types or members.

Use the namespace alias qualifier (: :) as much as possible, and reduce the use of the generic qualifier such as the dot.

 

Introduction of Pragma indicators

C #2.0 allows us to use the # pragma directive to specify the compiler's processing of warning information:

The method marked with the Obsolete label indicates that the method is Obsolete, prompting you to stop using this method. However, for example, when writing a database program and considering compatibility, we want to remove the warning information. # Pragma contains the content to be disable. The following numbers can be specified or not. If not, all warning messages of disable are displayed. If there is a number, only the warning information of this number is disabled.

 

Notes

Currently, The Pragma indicator only supports # pragma warning.

# Pragma warning disable can disable any compiler warning information.

# Pragma warning restore can restore any compiler warning information that is disable.

You can follow the specific warning code after disable and restore to disable or recover specific warning information.

# Pragma is a compilation preprocessing function that does not affect any code running mechanism.

 

Introduction of Conditional feature classes

C #2.0 allows us to use the Conditional feature class to tell the compiler to apply features on the class based on the "Specific predefined directive condition:

C #1.0 can only be defined on the method. If this symbol is defined, this method will be called. If it is not defined, this method will not be used. C #2.0 can also be extended to class with this symbol definition.

In this example, we first wrote a predefined indicator at the beginning, and then wrote a feature class testattrial, which uses a Conditional feature in the feature class, this feature transmits a string parameter, indicating that this feature class will become a conditional feature class. If the indicator of input parameters is defined, the Test feature will be applied to the MyClass, that is, the compiler will generate a feature metadata on the MyClass class when compiling the MyClass class, feature metadata is instantiated during compilation. If we comment out # define DEBUG, the Test tag is also commented out, and the code will not apply this feature to MyClass.

 

Notes

If a condition indicator is defined, such as # define DEBUG, the compiler applies the TestAttribute feature to the MyClass class.

If no conditional indicator is defined, such as # undef DEBUG, The MyClass class can still be used, but the TestAttribute feature is no longer applied.

Note the differences between the Conditional feature class in C #2.0 and the Conditional feature method in C #1.0.

 

Introduction of fixed-length buffer

C #2.0 introduces a fixed-length buffer so that we can declare a C-style array in the structure to facilitate the interoperability of hosted code and unmanaged code:

Note: You need to add a fixed keyword before the array. The fixed statement is different from the fixed Statement of C #1.0. The fixed Statement of 1.0 is to fix the things in the managed heap, in this way, pointer access is used to prevent the spam collector from moving the memory and interoperability with non-hosted code. In 2.0, the fixed is to specify a buffer with a fixed length. In this structure, an int4 byte has a total of 4*115 = 460 bytes.

 

Notes

Fixed-length buffer can only be used in the context of unsafe code, but not in non-unsafe code.

The fields defined by the fixed-length buffer are used for memory layout in the declared order of the Instance objects of the structure type.

Note the difference between the fixed length buffer in the unsafe code and the managed array we usually use.

There is a big difference between the two. Generally, the declaration of the normally hosted array is int [] myArray, the position of the brackets, and the fixed length array must specify the length of the array. The fixed-length buffer does not inherit from System. Array, and the managed Array inherits from System. Array by default.

Fixed-length buffer is mainly used for hosting code and non-hosting code interoperability. In addition, we generally use hosted arrays.

 

C #3.0 and Future Development

The core mechanism of C #2.0 lies in the introduction of generic programming. It gives type parameter-type polymorphism and will have an important impact on the construction of C # code in the future.

C #3.0 introduced XML and SQL data processing technologies into a strong language such as C #, which greatly enriched the data processing capabilities of C, is a far-sighted innovation.

The development of C # Language increasingly reflects the idea of integrating the "design mode + Database. "Language development is the development of libraries !"

2010.11.2

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.