The C # programming language (Third edition) Part II

Source: Internet
Author: User

Class

A class is a data structure that can include: data members (constants and fields), function members (methods, attributes, events, indexes, operator, instance constructor, destructor, and static constructor), and Nested classes.

Abstract class: Abstract modifier can be used to indicate that a class is incomplete and can only be used as a base class. Differences between abstract classes and non-abstract classes,

The abstract class cannot be directly instantiated. Using the new operator directly on the abstract class produces a compilation error. Although the abstract class can also have fields and values during the compilation period, such fields and values are either null or contain references from non-abstract class instances inherited from the abstract type.

Abstract classes can contain abstract members.

Abstract classes cannot be sealed.

When a non-abstract class inherits an abstract class, this non-abstract class must contain the actual implementation of all inherited abstract members.

Abstract Class

{

Public abstract void F ();

}

Abstract class B:

{

Public void g (){}

}

Class C: B

{

Public override void F (){...}

}

 

Sealing class

The sealed modifier can be used to prevent a class from being inherited. The sealed class cannot be an abstract class.

 

Static class

The static modifier is used to declare a class as a static class. Static classes cannot be instantiated, used as types, and can only contain static members. Only static classes can contain expansion functions.

Both constants and nested types are classified as static members.

 

Partial Modifier

The partial modifier can be used to indicate that the class declaration is a local type declaration.

 

Type parameter

A type parameter is a simple identifier. It is used as a placeholder for provided type parameters to create a construction type.

 

Type parameter restrictions

For generic type and method declarations, you can specify the constraints of class type parameters by including the type parameter limit clause.

Each type Parameter Restriction clause consists of the where tag, the name of the type parameter, and a colon and the list of the type parameters.

 

Local Method

Local methods can be defined in one part of the type declaration and implemented in another part. This implementation is optional. If this local method is not implemented, the local method declaration and all calls to it will be removed during the combination.

Local methods can only be declared in local classes or local structures. It cannot be declared in a non-local type or interface.

Access modifiers cannot be defined for local methods. They are all implicitly private. Their return type must be void, and their form parameters cannot contain the out modifier.

Partial Class Customer

{

String name;

Public string name {

Get {return name ;}

Set {

Onnamechanging (value );

Name = value;

Onnamechanged ();

}

}

Partial void onnamechanging (string newname );

Partial void onnamechanged ();

}

Partial Class Customer

{

Partial void onnamechanging (string newname)

{

Console. writeline ("changing" + name + "to" + newname );

}

Partial void onnamechanged ()

{

Console. writeline ("changed to" + name );

}

}

 

Class Member

Constant, representing the constant value associated with the class.

Field, that is, the variable of the class.

Method To implement computing and operations that can be performed by the class.

Attribute, which defines the naming features and the operations to read and write these features.

Event, which defines the notifications that can be generated by the class.

Index, so that the instance of the class can be indexed like an array.

Operator, which defines expression operators that can act on class instances.

The instance constructor implements the operations required for the initialization class.

The Destructor allows you to perform operations before dropping a class instance.

The static constructor instance is used to initialize the class itself.

Indicates the local type of the class.

 

Members of the constructor type

A non-inherited Member of the constructor type can be obtained by replacing each type parameter in the member declaration with the corresponding type parameter in the constructor type. The replacement process is based on the meaning of the type declaration, rather than simply text replacement.

Class Gen <t, u>

{

Public T [,];

Public void g (int I, T, Gen <u, T> Gt ){...}

Public U prop {get {...} set {...}}

Public int H (double D ){...}

}

The construction type Gen <int [], icomparable <string> has the following members:

Public int [,] [];

Public void g (int I, int [] T, Gen <icomparable <string>, int []> Gt ){...}

Public icomparable <string> prop {get {...} set {...}}

Public int H (double D ){...}

When the type of member A in the generic type declaration gen is "t Two-dimensional array", the type of member A in the structure type given above is "two-dimensional array of one-dimensional int array ", or int [,] [].

 

Inheritance

A class inherits members from its direct base class type. This means that a class implicitly contains all its members of the direct base class type, except the base class instance constructor, destructor, and static constructor.

 

New Modifier

The class member declaration allows you to declare a member with the same name or signature as the inherited member. At this time, we will say that the inheritance class member hides the base class member. You can use the new modifier to tell the compiler that this hiding is intentional.

 

Nested type

Non-nested types can be public or internal declared accessible. The default value is internal. Nesting type accessibility:

The declared nested type in the class can have any of the five declared Accessability (public, protected, internal. Protected, internal, private ).

The declared nested type in the structure can have three forms of declarative accessibility (public, internal, private ).

Nested classes can also use the new modifier to clearly hide the intent.

 

This access

This in the nested type cannot be used to point to instance members in the type that contains it. When the nested type needs to access instance members of the type that contains it, this can be provided to this access as the real parameter of the constructor of the nested type.

Class C

{

Int I = 123;

Public void F (){

Nested n = new nested (this );

N. G ();

}

Public class nested

{

C this_c;

Public nested (C ){

This_c = C;

}

Public void g (){

Console. writeline (this_c. I );

}

}

}

 

Reserved member name

For each attribute, event, or index source member declaration, the implementation must retain two method signatures based on the type of the member declaration.

Name of the member retained for the attribute

For attribute P of type T, the following signature is retained:

T get_p;

Void set_p (T value );

Example

Using system;

Class
{
Public int P {
Get {return 123 ;}
}
}

Class B:
{
New public int get_p (){
Return 456;
}
New public void set_p (INT value ){}
}

Class Test
{
Static void main ()
{
B = new B ();
A A = B;
Console. writeline (A. P );
Console. writeline (B. P );
Console. writeline (B. get_p ());
}
}

(But in actual compilation, I generate here: the member "B. set_p (INT)" will not hide the inherited member. No keyword new is required. Warning)

For event e of type T, the following signatures are retained:

Void add_e (T handler );

Void remove_e (T handler );

For indexes of type T with parameter list l, the following signatures are retained:

T get_item (L );

Void set_item (L, t value );

 

Constant

A constant is a member of a class that represents a constant value, that is, a value that can be calculated during compilation. A constant declaration can introduce one or more constants of a given type.

Because the new operator cannot be used in constant expressions, null is the only possible value of a constant of the reference type other than string.

When a symbolic name of a constant value is required, but the type of the value cannot appear in the constant declaration, or the value cannot be calculated by a constant expression during compilation, the readonly field is used.

 

Field

A field is a member that represents a field associated with an object or class.

The field type must be at least the same as the field itself.

When a field Declaration contains a static modifier, the field introduced by the Declaration is a static field. If not, the instance field is included in the declaration.

When a field Declaration contains the readonly modifier, the field introduced by the Declaration is a read-only field.

Static read-only fields are used as constants and used as constants.

When the field Declaration contains the volatile modifier, the field introduced by the Declaration is called a loss-prone field.

When a class is initialized, all static fields of that class are initialized to the default value first, and then the static field initialization statement is executed in order of text.

Class Test

{

Static int A = B + 1;

Static int B = a + 1;

Static void main (){

Console. writeline ("A = {0}, B = {1}", a, B );

}

}

A = 1, B = 2

 

Method

A method is a member that implements computing or operations that can act on objects or classes.

When an instance method declaration contains a virtual modifier, this method is a virtual method. If there is no virtual modifier, this method is a non-virtual method.

The implementation of virtual methods can be replaced by inheritance classes. The process of replacing an inherited virtual method is called overwriting that method.

In the call of a virtual method, the runtime type of the instance where the call occurs determines which method is actually called. In non-virtual method calls, the instance compilation period type is the deciding factor.

Using system;

Class

{

Public void F () {console. writeline ("a.f ");}

Public Virtual void g () {console. writeline ("A. G ");}

}

Class B:

{

New public void F () {console. writeline ("B. F ");}

Public override void g () {console. writeline ("B .g ");}

}

Class Test

{

Static void main (){

B = new B ();

A A = B;

A. F (); // a.f

B. F (); // B. f

A. G (); // B .g

B. G (); // B .g

}

Here, the instance (that is, B) runtime type (rather than the compile-time type of the Instance (that is, a) determines the actual implementation of the method to be called.

When the instance method declaration contains the override modifier, this method is an overwrite method. The virtual method declaration introduces a new method, and the overwriting method declaration is to private an existing inherited virtual method by providing a new implementation for that method. The basic overwriting methods are virtual, abstract, or override.

The overwriting statement can access the overwriting basic method through basic access.

Class

{

Int X;

Public Virtual void printfields (){

Console. writeline ("x = {0}", X );

}

}

Class B:

{

Int y;

Public override void printfields (){

Base. printfields ();

Console. writeline ("Y = {0}", y );

}

}

Basic access disables the virtual calling mechanism and directly treats the base method as a non-virtual method.

Class

{

Public Virtual void F (){}

}

Class B:

{

New private void F () {}// hides a.f within Body of B

}

Class C: B

{

Public override void F () {}// okay, overrides a.f

}

The F method in B hides the virtual method F inherited from. Therefore, the new F in B has the private access permission, so its scope only contains the class body of B, and does not extend to C.

 

When the instance method declaration contains the sealed modifier, this method is a sealing method. If an instance method declaration contains the sealed modifier, it must also include the override modifier. Using the sealed modifier can prevent inheritance from further overwriting this method.

 

When the instance method declaration contains the abstract modifier, this method is an abstract method.

Abstract method declaration can override virtual methods. This behavior allows an abstract class to force its inheritance class to re-implement this method and make the original implementation of the method invalid.

 

When the method declaration contains the extern modifier, this method is an external method. The extern modifier is usually used with the dllimport feature, so that external methods can be implemented by DLL.

 

When a method declaration contains a partial modifier, this method is a local method.

 

When the first parameter of a method contains the this modifier, this method is an extension method. Extension methods can only be declared in non-generic and non-nested static classes.

 

Attribute

A property is a feature member that can access objects or classes. Unlike public fields, attributes provide an abstraction between the internal state of an object and its public interfaces.

Class label
{
Private int X, Y;
Private string Caption;
Public label (int x, int y, string Caption ){
This. x = X;
This. Y = y;
This. Caption = Caption;
}
Public int X {
Get {return X ;}
}
Public int y {
Get {return y ;}
}
Public point location {
Get {return new point (x, y );}
}
Public String caption {
Get {return Caption ;}
}
}
The label class uses two int fields X and Y to save its position. If you choose to save the location of a lable in a future version as a point,

Class label
{
Private point location;
Private string Caption;
Public label (int x, int y, string Caption ){
This. Location = new point (x, y );
This. Caption = Caption;
}
Public int X {
Get {return location. X ;}
}
Public int y {
Get {return location. Y ;}
}
Public point location {
Get {return location ;}
}
Public String caption {
Get {return Caption ;}
}
}

}

 

When an attribute is defined as an automatically implemented attribute, it automatically has a hidden field and implements the accesser for reading and writing this field.

 

Event

An event is a member that can notify objects or classes.

The event declaration type must be the delegate type and have the same accessibility.

In the program text that contains the time Declaration class or structure, some events can be used as fields.

 

Index

When indexing, a member can be accessed like an array. Implements an index that can access a single bit in the bit array.

Using system;
Class bitarray
{
Int [] bits;
Int length;
Public bitarray (INT length ){
If (length <0) throw new argumentexception ();
Bits = new int [(length-1)> 5) + 1];
This. Length = length;
}
Public int length {
Get {return length ;}
}
Public bool this [int Index] {
Get {
If (index <0 | index> = length ){
Throw new indexoutofrangeexception ();
}
Return (BITS [index> 5] & 1 <index )! = 0;
}
Set {
If (index <0 | index> = length ){
Throw new indexoutofrangeexception ();
}
If (value ){
Bits [index> 5] | = 1 <index;
}
Else {
Bits [index> 5 ~ (1 <index );
}
}
}
}

 

Operator

An operator is a member that defines the meaning of expression operators that can be applied to class instances.

The Assembly and replacement operator declaration introduces a custom conversion to supplement the predefined implicit and display conversions. The declaration of conversion operators containing implicit keywords introduces a custom implicit conversion. The conversion operator containing the explicit keyword declares a custom display replacement.

 

Instance Constructor

A strength constructor is a member of the Operation required to initialize a class instance.

The field initialization statement is converted into a value assignment statement, which is executed before the base class instance constructor is called.

Using system;
Class
{
Public (){
Printfields ();
}
Public Virtual void printfields (){}
}
Class B:
{
Int x = 1;
Int y;
Public B (){
Y =-1;
}
Public override void printfields (){
Console. writeline ("x = {0}, y = {1}", x, y );
}
}
When new B () is used to create B's instance, the output is:

X = 1, y = 0

The value of Y is 0 because the value of Y is not executed until the base class constructor returns the value.

If a class does not contain the instance constructor declaration, a default instance constructor is automatically provided for it. The default constructor calls the non-argument constructor of the direct base class. If not, a compilation error occurs.

When a class t only declares the private instance constructor, it is impossible to inherit the T or directly create the T instance outside the program text of the class T. (Singleton Mode)

A static constructor is a member that implements the operations required to initialize a closed class type.

Using system;
Class Test
{
Static void main (){
A.f ();
B. F ();
}
}
Class
{
Static (){
Console. writeline ("init ");
}
Public static void F (){
Console. writeline ("a.f ");
}
}
Class B
{
Static B (){
Console. writeline ("init B ");
}
Public static void F (){
Console. writeline ("B. F ");
}
}
The following output is generated:

Init
A.f
Init B
B. f
Here, the static constructor of A is triggered by the call of a.f, while the static constructor of B is triggered by the call of B. F.

Using system;
Class
{
Public static int X;
Static (){
X = B .y + 1;
}
}
Class B
{
Public static int y = a.x + 1;
Static B (){}
Static void main (){
Console. writeline ("x = {0}, y = {1}", a.x, B .y );
}
}
The following output is generated:
X = 1, y = 2

 

Iterator

Function members implemented by the iterator block are called iterators.

The iterator introduces additional overhead and must be used with caution.

 

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.