C # Study Notes (1)

Source: Internet
Author: User

1. structure (struct) and class (class)
[Attributes] [modifiers] struct identifier [: interfaces] body [;]
Similar to a class, the structure indicates the data structure that can contain data members and function members. Different from the class, the structure is a value type and does not require heap allocation. A variable of the structure type directly contains the data of the structure, and a variable of the class type contains references to the data (this variable is called an object ). The struct type is applicable to lightweight objects such as dots, rectangles, and colors. Although a vertex may be represented as a class, the structure is more effective in some schemes. In some cases, the structure cost is low. For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object. The structure can declare constructors, but they must contain parameters. The default (No parameter) constructor of the declared structure is incorrect. The default constructor is always provided to initialize the structure members as their default values. It is incorrect to initialize the instance field in the structure. In the class, you must initialize the instance object. When you use the new operator to create a structure object, this structure object is created and an appropriate constructor is called. Different from the class, the new operator can be used for structure instantiation. If new is not used, the field remains unassigned and the object is unavailable before all fields are initialized. For structures, there is no inheritance like classes. A structure cannot inherit from another structure or class, and cannot be the base of a class. However, the structure inherits from the base class Object. The structure can implement interfaces in the same way.
[C ++] is different from C ++. You cannot use the struct keyword to declare a class. In C #, classes and structures are different in semantics. The structure is the value type, and the class is the reference type.
2. binning and unboxing (unboxing)
Packing is an implicit conversion of the value type to the object type or to any interface type implemented by this value type. Binning a value will allocate an object instance and copy the value to the new object. Keyword object. unboxing is an explicit conversion from the object type to the value type or from the interface type to the value type that implements this interface. Unboxing includes checking the object instance to make sure it is a boxed value of the given value type. Copy the value from the instance to the value type variable.
Example:
Int I = 123; // A value type
Object box = I; // Boxing
Int j = (int) box; // Unboxing

Packing and conversion
Unboxing
3. implicit and explicit
The following conversions are implicit conversions: for example, object o = I;
Id conversion.
Implicit numeric conversion.
Implicit Enumeration conversion.
Implicit conversion.
Packing and conversion.
Implicit constant expression conversion.
User-Defined implicit conversion.
The following conversions are Explicit conversions: object 0 = (object) I;
All implicit conversions.
Explicit numeric conversion.
Explicit enumeration conversion.
Explicit reference conversion.
Explicit Interface Conversion.
Cancel packing conversion.
User-Defined Explicit conversions
4. 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 );.............}
A delegate is a data structure that references a static method or an object instance and an instance method of the object. In C or C ++, the function pointer is closest to the delegate, but the function pointer can only reference static functions, and the delegate can reference both static methods and instance methods. In the latter case, the delegate not only stores the reference to the method entry point, but also stores the reference to the object that calls its method. Unlike C ++ function pointers, delegation fully targets objects. Unlike C ++ pointers pointing to member functions, delegation encapsulates object instances and methods at the same time. The Delegate Declaration defines the class derived from the class System. Delegate. The delegated instance encapsulates one or more methods. Each method is called a callable object. For instance methods, the callable entity is composed of an instance and the methods on the instance. For static methods, the callable object consists of only one method. Given the delegated instance and the appropriate parameter set, you can use this parameter set to call all methods of the delegated instance. An interesting and useful attribute of the delegated instance is the class of the method that it does not understand or care about it's encapsulated. What really matters is that the method must be compatible with the delegate type, this makes the delegate very suitable for "anonymous" calls. The optional parameter table specifies the delegate parameter, and the return type indicates the delegate return type. If both of the following conditions are true, the method and the delegate type are compatible: (the concept of compatibility is that the delegate of this statement can be used to delegate the method ).
1. They have the same number of parameters, the same type, the same order, and the same parameter modifier.
2. they return the same type.
The delegate type in C # is equivalent to the name rather than the structure. (However, note that two instances with different but equivalent Delegate types may be equal.) to be precise, two Delegate types with the same parameter list, signature, and return types are considered different Delegate types. The method set encapsulated by the delegated instance is called the call list.
5. interface)
[Attributes] [modifiers] interface identifier [: base-list] {interface-body} [;]
An interface defines a protocol. Classes or structures that implement interfaces must comply with their agreements. Interfaces can be inherited from multiple basic interfaces, while classes or structures 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 defines. The interface only specifies the class that implements the interface or the members that the interface must provide. An interface can be a namespace or a member of a class and contain the signature of the following members: Method property indexer.
An interface can be inherited from one or more basic interfaces. Interfaces can be implemented by classes. The identifier of the implemented interface appears in the base list of the class. The inherited interface is called the explicit basic interface of this interface. When an interface has one or more explicit basic interfaces, in the interface declaration, the interface identifier is followed by a colon and a list of basic interface identifiers separated by commas. The base interface of an interface is an explicit base interface and its base interface. In other words, the base interface set is a fully writable closure of the explicit base interface and their explicit base interface (and so on. The interface inherits all the members of its base interface. The interface members are accessed by members in the form of I .M and I [A] and the access expression of the index, where I is an interface-type instance, M is the method, attribute, or event of this interface type, and A is the list of parameters of the indexer. Interfaces can be implemented by classes and structures. To indicate the class or structure implementation interface, the interface identifier is included in the base class list of the class or structure. The process of locating an interface member in an implementation class or structure is called interface ing.
6. object
Object class is the final base class of all other types. Each type in C # is derived directly or indirectly from the object class type. Any type of value can be assigned 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 operators (= and! =) Is defined to compare the "value" (7.9.7 Equal string operator) of a string object (rather than a reference ). This makes the test of string equality more intuitive. The string is of the string type and can be written in two forms, that is, it is enclosed by quotation marks and. The string enclosed by quotation marks is enclosed in double quotation marks ("), and can contain any character including the code-changing sequence. The string enclosed by @ starts with @ and is enclosed by double quotation marks. The @-induced string starts with @ and is enclosed in double quotation marks. To include a double quotation mark in a string caused by @, use two pairs of double quotation marks: another use of the @ symbol is to use the referenced (/reference) identifier that happens to be the C # keyword.
8. Modifier
Modifier
Access Modifier
Public
Private
Internal
Protected
Specify the declared type and the accessibility of type members.

Unrestricted access
Only the class containing the class members can access
Only the current project can access
Only the classes that contain this member and the inherited classes can be accessed.
Abstract indicates that a class can only be the base class of other classes.
Const specifies that the value of a field or local variable cannot be modified.
Event declares an event.
Extern indicates external implementation of this method.
Override provides a new implementation of virtual members inherited from the base class.
Readonly declares a field. This field can only be assigned as part of the Declaration or in the same class of constructor.
The specified sealed class cannot be inherited.
The static declaration belongs to the type rather than to the members of a specific object.
Unsafe declares the insecure context.
Virtual declares in the derived class its implementation of methods or accessors that can be changed by override members.
Volatile indicates that the field can be modified in the program by the operating system, hardware, or threads that execute concurrently.

9. Statements
A statement is a program instruction. Unless otherwise stated, the statements are executed in sequence. C # has the following types of statements.
Category C # keywords
Select statement if, else, switch, case
Iteration statement do, for, foreach, in, while
Jump statement break, continue, default, goto, return
Exception Handling 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 object set. The foreach statement is used to cyclically access the set to obtain the required information, but should not be used to change the set content to avoid unpredictable side effects. The statement format is as follows:
Foreach (type identifier in expression) statement
To access a set cyclically, the set must meet specific requirements. Set Type:
It must be an interface, class, or struct.
The GetEnumerator instance method of the return type must be included, for example, Enumerator (see below ).
The Enumerator type (class or structure) must contain:
A property named Current, which returns ItemType or can be converted to this type. The property accessors return the current element of the set.
· A bool method named MoveNext, which increments the item counter and returns true if more items exist in the set.
There are three methods to use a set:
Use the preceding instructions to create a set. This set can only be used for C # programs.
1. Use the preceding instructions to create a general set and implement the IEnumerable interface. This set can be used in other languages (such as Visual Basic ).
2. Use a predefined set in the Collection class.
(2) The throw statement is used to send an abnormal (abnormal) signal during program execution. The format of the throw statement is:
Throw [expression];
Expression: exception object. When the current exception object is thrown again in the catch clause, it is omitted.
(3) try-catch statement
A try-catch statement consists of a try block and one or more catch clauses followed by it (specify a handler for different exceptions. Try-catch statements take one of the following forms:
Try-block
Catch (exception-declaration-1) catch-block-1
Catch (exception-declaration-2) catch-block-2
...
Try-block catch-block
(4) fixed
Prevents variables from being relocated by the garbage collector.
(5) lock
The lock keyword marks a statement block as a critical section.
6. method parameters
If a parameter is declared for a method without ref or out

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.