C #2.0 features-local type-null type-static class

Source: Internet
Author: User

Local type-null type-static class

[Local type overview]

• The local type allows us to divide a type (class, structure, or interface) into several parts, which are implemented in several different. CS files.

• The local type applies to the following situations:

-The type is very large and should not be implemented in a file;

-One part of a TypeCodeThe code generated for automated tools should not be mixed with the code we have compiled.

• Local types are compiled at the pure language layer without affecting any execution mechanism. In fact, the C # compiler still combines the local types of each part into a complete class during compilation.

 

Restrictions on local types

• Local types are only applicable to classes, structures, or interfaces, and do not support delegation or enumeration.

• Each part of the same type must have a modifier partial.

• When using a local type, each part of a type must be in the same namespace.

• Each part of a type must be compiled at the same time. In other words,

C #It is not supported to compile some parts of a type first, and then compile some parts of a type.

 

Notes for local type

• The keyword partial is a context keyword. It indicates a keyword only when it is put together with class, struct, and interface.

Therefore, the introduction of partial does not affect the variables named partial in the existing code.

• The number of local types does not have to be 2 or more, but can also be 1-although there is no need for "local" at this time

• The parts of the local type are generally separated into several different. CS files, but the C # compiler allows us to put them in the same. CS file.

 

Application features on local types

• Features of local types have the "accumulative" effect. In other words, the following code:

[Attr1, attr2 ("hello")]

Partial Class {}

 

[Attr3, attr2 ("goodbye")]

Partial Class {}

 

• Similar to applying the following features on Class:

[Attr1, attr2 ("hello"), attr3, attr2 ("goodbye")]

Class {}

 

Modifier on local type

• The access protection Modifier on each part of a type must be consistent.

• If a part of a type uses the abstract modifier, the entire class will be treated as an abstract class.

• If a part of a type uses the sealed modifier, the entire class will be considered as a sealed class.

• Different parts of a class cannot use conflicting modifiers. For example, abstract cannot be used on one part, and sealed is used on the other.

 

Base class or interface of local type

• The base classes specified on each part of a type must be consistent. Some parts do not specify the base class, but if they are specified, they must be the same.

• Interfaces of the local type have the "accumulate" effect, in other words, such

The following code:

Partial Class C: IA, IB {...}

Partial Class C: IC {...}

Partial Class C: IA, IB {...}

• Equivalent to the following code:

Class C: IA, IB, IC {...}

 

[Introduction to null type]

• The null type allows a value type to have a "null" meaning, which facilitates operations in many cases, such as null fields in the database.

• Demonstration of null type:

Int I = 123;

Int? X = I;

Double? Y = X;

Int? Z = (Int ?) Y;

 

Note on null type

• The null type is actually a generic type

System. nullable <t>. The basic type of the null type is system. nullable <t>, where T must be a value type.

• If the value of the null type is not null, you can use the same basic type calculation, such as + ,-,*,/

• The hasvalue attribute of the null type is used to determine whether the type is null. If it is not null, you can use the value attribute to obtain the value of its basic type.

 

[Static class Introduction]

• A static class is only used to contain static members. It cannot be instantiated or inherited. It is equivalent to a sealed abstract class.

• Static CLASS DEMONSTRATION

Static class myutility

{

Public const int data;

Public static void Foo (){

......

}

}

 

 

Notes for static classes

• The static class cannot have an instance constructor.

• The static class cannot have any instance members.

• Abstract or sealed modifiers cannot be used for static classes.

• Static classes are inherited from the system. Object root class by default and cannot explicitly specify any other base classes.

• No interface implementation can be specified for static classes.

• Members of the static class cannot have protected or protected internal access protection Modifiers

 

 

• Local types are compiled at the pure language layer without affecting any execution mechanism. In fact, the C # compiler still combines the local types of each part into a complete class during compilation.

• The null type allows a value type to have a "null" meaning, so as to facilitate operations in many cases, such as empty fields in the database. The null type is actually a generic class system. nullable <t>.

• A static class is only used to contain static members. It cannot be instantiated or inherited. It is equivalent to a sealed abstract class.

 

It is recommended that you go to the "C #2.0 sharp experience" series by Li Jianzhong. Li Jianzhong isZhu Cheng TechnologyFounder.

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.