C # study notes (1)

zhaozj2021-02-16  51

C # study notes

1, Structure (STRUCT) and Class (CLAS)

[attributes] [Modifiers] strunt identifier [: interfaces] body [;]

Structure and classes are very similar, all indicate data structures that can include data members and function members. Different from the class, the structure is a value type and does not require a heap allocation. The variable of the struct type directly contains the structure of the structure, and the class type variable contains references to the data (this variable is called object). The STRUCT type is suitable for a lightweight object such as a point, rectangle, and colors. Although a point may be represented as classes, the structure is more effective in some scenarios. In some cases, the cost of the structure is low. For example, if an array containing 1000 points object is declared, additional memory is assigned to each object. The structure can declare the constructor, but they must have parameters. The default (non-parameter) constructor of the declaration structure is wrong. Always provide default constructor to initialize structural members to their default values. The initialization instance field in the structure is wrong. In the class, instance objects must be initialized. When you create a structural object with the New operator, the structure object is created, and the appropriate constructor is called. Different from the class, instantiation of the structure may not use the New operator. If NEW is not used, the field will remain unfaised and the object is not available before initializing all fields. For structures, there is no inheritance like classes. A structure cannot inherit from another structure or class, and cannot be used as a class of a class. However, the structure is inherited from the base class Object. The structure can realize the interface, which is the same as the same type.

[C ] Unlike C , the Struct keyword declaration cannot be used. In C #, classes and structures are semantically semantically. The structure is a value type, and the class is a reference type.

2, boxes and unpacking (unpacking)

The box is an implicit conversion of any interface type that the value type to the Object type or the value implemented by the value type. Assign a value of the value of the value to an object instance and copy the value into the new object. The keyword object. Cancel box is an explicit conversion from the Object type to value type or from the interface type to the value type that implements the interface. Cancel packing operations include: Check the object instance to make sure it is a packing value for a given value type. Copy this value from an instance to a value type variable.

example:

INT i = 123; // a value type

Object Box = i; // boxing

INT j = (int) box; // unboxing

Packing conversion

Unpack

3, implicit and explicit

The following conversion belongs to implicit conversion: example: Object o = i;

Identification conversion.

Implicit numerical conversion.

Implicit enumeration conversion.

Implicit reference conversion.

Packing conversion.

Implicit constant expression conversion.

User-defined implicit conversions.

The following conversion belongs to explicit conversion: Object 0 = (Object) i;

All implicit conversions.

Explicit numerical conversion.

Explicit enumeration conversion.

Explicit reference conversion.

Explicit interface conversion.

Cancel the box conversion.

User-defined explicit conversion

4, Delegate (delegate)

Delegate Void D (int X);

Class C {

Public Static Void M1 (INT I) {/ * ... * /}

Public Static Void M2 (INT I) {/ * ... * /}}

Class {...... .d CD1 = New D (C.M1); ............}

The delegation is a data structure that references a static method or references an object instance and the instance method of the object. In C or C , the client is closest to the function pointer, but the function pointer can only reference the static function, and the commission can simultaneously reference the static method and instance method. In the latter case, the delegate not only stores references to method entry points, but also stores references to objects that call their methods. Unlike the C function pointer, the delegate is completely facing object; unlike the C pointer to the member function, entrust the simultaneous package object instance and method. The delegation declares defines the class derived from class system.delegate. The entrusted instance encapsulates one or more methods, each method being called an adjustable entity. For example, the modifier consists of an instance and the method on the instance. For static methods, the calling entity is constructed only by one method. For a given entrustment instance and an appropriate parameter set, you can use this parameter set to call all the methods of this delegate instance. An interesting and useful attribute of the entrusted instance is that it does not understand or does not care about the way it is packaged; the truly important is just that the method is compatible with the type of delegate, which makes the entrustment very suitable for "anonymous" calls. An optional shape specifies the parameters of the delegate, and the return type indicates the returning type of the delegate. If the following two conditions are true, the method and the delegate type are compatible: (compatible concept is to delegate the method of this declaration) .1 They have the same number of parameters, and the type, the same order, Parameter modifiers are also the same.

2 They have the same type.

The delegate type in C # is the name equivalent, not the structure is equivalent. (However, please note: the instance of two different but structurally equivalent delegate types may be equal to the equal), accurately, two different entrustments with the same parameter list, signature, and return types are considered different. Entrusted type. The method set of the entrusted instance is called a call list.

5, Interface (interface)

[attributes] [Modifiers] Interface Identifier [: Base-list] {interface-body} [;]

An interface defines an agreement. The class or structure that implements the interface must comply with its agreement. The interface can be inherited from multiple base interfaces, and the class or structure can implement multiple interfaces. The interface can contain methods, attributes, events, and indexers. The interface itself does not provide the implementation of the members it defined. The interface only specifies the membership that implements the interface or interface that the interface must be provided. The interface can be a member of the namespace or class, and can include the signature of the following members: Method Properties Indexer.

An interface can be inherited from one or more base interfaces. The interface can be implemented by the class. The identifier of the implemented interface appears in the base list of the class. The inherited interface is called the explicit base reference for the interface. When the interface has one or more explicit base portions, in the interface declaration, the interface identifier follows a colon and a list of colons and the base interface identifier separated by commas. The base interface of the interface is an explicit base interface and its base reference. In other words, the base interface set is an explicit base reference, and their explicit base reference (according to this type) is completely transmitted. The interface inherits all members of its base. Interface members are accessed by I.M and I [A] forms access and index access expressions, where i is an instance of the interface type, M is the method, attribute, or event of the interface type, and a is the list of indexer parameters. The interface can be implemented by classes and structures. In order to indicate classes or structures, an interface identifier is included in the base class list of such or structures. The process of the implementation of the positioning interface member in the implementation class or structure is called an interface mapping.

6, Object

The Object class is all other types of final base classes. Each type in C # is derived directly or indirectly from the Object class type. You can give any type of value to the Object type.

7, String type

An instance of the String class represents a Unicode string. Although string is a reference type, the equal operator (== and! =) Is defined as a "value" (7.9.7 string equal) of the String object (not reference). This makes it more intuitive for the test of the character string. The string is the string type and writable in two forms, that is, use quotation marks to cause and use @. The character caused by quotation marks in dual quotation marks ("), and can contain any character strings that include @ of the characters including the excoding sequence, to start with @, and use dual quotes. Use @ caused The string begins with @, and uses a double quotation mark. To include a double quotation number in a string that is caused by @, use two pairs of double quotes: @ symbols another usage is to use it happen to become a C # keyword The referenced (/ Reference) identifier. 8, modifier

Modifier

Access modifier

public

Private

Internal

protected

Specifies the type of declaration and the accessibility of type members.

Access is not limited

Only class that contains members of this class can be accessed

Only the current project can be accessed

Only class that contains the class and inheritance of the member can be accessed

Abstract Indicates that a class can only be a base class for other classes.

Const specifies the value that cannot be modified by the field or partial variable.

Event declares an event.

Extern Indicates that this method is implemented.

Override provides a new implementation of virtual members inherited from base classes.

Readonly declares a field that can only assign a part of the declaration or in the same class constructor.

Sealed specified classes cannot be inherited.

Static declaration belongs to a type itself instead of a member belonging to a specific object.

Unsafe declares unsafe context.

Virtual declares its implementation of the method or accessor that can be rewritten in the derived class.

The volatile indicator field can be modified in the program by operating system, hardware, or concurrent thread or the like.

9, statement

The statement is the program instruction. Unless otherwise stated, statements are executed in order. C # has the statement of the following category.

Category C # keyword

Select Statement IF, Else, Switch, Case

Iterative Statement DO, For, Foreach, In, While

Jump Statement Break, Continue, Default, Goto, Return

Abnormal processing statement throw, try-catch, try-finally

Checked and uncheckedchecked, unchecked

FIXED statement fixed

LOCK statement LOCK

(1) The Foreach statement repeats an embedded statement group for each element in an array or an object collection. The Foreach statement is used to loop access sets to get the required information, but not to change the collection content to avoid unpredictable side effects. The form of this statement is as follows:

Foreach (Type Identifier In Expression) Statement

To cycle access, the collection must meet a specific requirement. Collection type:

Must be Interface, Class or Struct.

Must include an instance method called GetEnumerator, such as ENUMERATOR (see below).

Enumerator type (class or structure) must contain:

A property called Current that returns ItemType or can be converted to this type of type. Attribute Accessor returns the current element of the collection.

· A BOOL method called MoveNext, which is incremented by the counter and returns True when there is more items in the collection.

There are three ways to use the collection:

Create a collection using the above guidance. This collection can only be used for C # programs.

1. Create a general collection using the above guidance and other Ienumerable interfaces. This collection can be used in other languages ​​(such as Visual Basic). 2. Use a predefined collection in the collection class.

(2) The throw statement is used to issue a signal that appears abnormal (abnormal) during program execution. The form of the Throw statement is:

Throw [expression];

Expression: Unusual objects. It is omitted when the current exception object is raised again in the CatCH clause.

(3) TRY -CATCH statement

The TRY-CATCH statement consists of a try block and one or more CATCH clauses (for different exception specified processing procedures) thereafter. The TRY-CATCH statement uses one of the following forms:

TRY TRY-Block

Catch (Exception-Declaration-1) catch-block-1

Catch (Exception-Declaration-2) Catch-block-2

...

TRY TRY-block catch catch-block

(4) Fixed

Prevent variables from being relocated by garbage collectors.

(5) LOCK

The LOCK keyword marks a statement as a critical area.

6. Method parameters

If a parameter is declared for a method without REF or OUT, this parameter can have an associated value. This value can be changed in the method, but the change is not retained when the control is transferred backward call. This behavior can be changed by using method parameters keywords. If there is no REF, OUT is passed by default, although the value of this parameter can be modified in the method, the modified value will not also go to the program that calls the method.

Params: Params Keywords You can specify methods parameters that use parameters in parameters.

Ref: reference delivery

OUT:

7, Namespace (name space)

转载请注明原文地址:https://www.9cbs.com/read-27751.html

New Post(0)