C #2.0 local type, null type, static class

Source: Internet
Author: User

Local type

Introduction of local types

When no local type exists (C #1.0)

Class app
{
Public void Foo (){...}
...
Public void bar (){...}
...
}

With the local type (C #2.0)

Partial class app
{
Public void Foo ()
{...}
...
}
Partial class app
{
Public void bar ()
{...}
...
}

Introduction to local types

Local types allow 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 on the language-only layer without affecting any execution mechanism. In fact, the C # compiler will still combine the local types of each part into a complete class during compilation.

Restrictions on local types

The local type is only applicable to classes, structures, or interfaces, and does 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 # does not support compiling some parts of a type before compiling some parts of a type.

Features of local types

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

The features on the local type have the "accumulate" effect. In other words, the following code:

[Attr1, attr2 ("hello")]
Partial Class {}

[Attr3, attr2 ("goodbye")]
Partial Class {}

It is equivalent to applying the following features on Class:
[Attr1, attr2 ("hello"), attr3, attr2 ("goodbye")]
Partial 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 is considered as a sealed class.

Each part of a class cannot use conflicting modifiers. For example, you cannot use abstract on one part or sealed 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.

The interface on the local type has the "accumulate" effect, in other words, the following code:

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

The Code is as follows:

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

Null type

Introduction to null type

The null type allows a value type to have the meaning of "null", so as to facilitate operations in many cases, such as empty 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 . The basic type of the null type is system. nullable. T must be a value type.

Null type if the value 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 type

Static class Introduction

Static classes are only used to contain static members. They cannot be instantiated or inherited. It is equivalent to a sealed abstract class.

Static class myutility
{
Public const int data;
Public static void Foo ()
{
...
}
}

Notes for static classes

The static class cannot have an instance constructor.
Static classes cannot have any instance members.
Static classes cannot use abstract or sealed modifiers.
Static classes are inherited from the system. Object root class by default, and cannot explicitly specify any other base classes.
Static classes cannot specify any interface implementation.
A static class member cannot have a protected or protected internal access protection modifier.

Summary

Local types are compiled on the language-only 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 the meaning of "null", which facilitates operations in many cases. For example, the null field in the database, the null type is actually a generic class system. nullable .

A static class only refers to the type of the Jingtai member. It cannot be instantiated or inherited. It is equivalent to a sealed abstract class.

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.