C # Learning Notes (1)

Source: Internet
Author: User
Tags foreach constructor exception handling expression garbage collection implement instance method numeric value
Notes

1, structure (struct) and classes (class)
[Attributes] [modifiers] struct identifier [: interfaces] body [;]
Structs are similar to classes in that they represent data structures that can contain data members and function members. Unlike classes, structs are value types and do not require heap allocation. A variable of a struct type directly contains the data of the structure, whereas a variable of the class type contains a reference to the data (the variable is called an object). The struct type is suitable for lightweight objects that represent dots, rectangles, and colors. Although a point may be represented as a class, the structure is more efficient in some scenarios. In some cases, the cost of the structure is lower. For example, if you declare an array that contains 1000-point objects, additional memory is allocated for referencing each object. Structs can declare constructors, but they must take parameters. Declaring a struct's default (parameterless) constructor is incorrect. Always provide a default constructor to initialize the struct members to their default values. Initializing an instance field in a struct is an error. In a class, you must initialize the instance object. When you create a struct object by using the new operator, the struct object is created and the appropriate constructor is invoked. Unlike classes, structs can be instantiated without using the new operator. If you do not use new, the field remains unassigned and the object is unavailable until all the fields have been initialized. For structs, there is no inheritance like a class. A struct cannot inherit from another struct or class and cannot be the base of a class. However, structs inherit from the base class Object. The structure can be implemented in the same way as the interface.
[C + +] is different from C + + and cannot declare a class using the struct keyword. In C #, classes and structs are semantically different. Structs are value types, and classes are reference types.
2, boxing and unboxing (unboxing)
Boxing is an implicit conversion of a value type to an object type or to any interface type implemented by that value type. Boxing the value of a value allocates an object instance and copies the value to the new object. Keyword object. Unboxing is an explicit conversion from an object type to a value type or from an interface type to a value type that implements the interface. The unboxing operation includes checking an object instance to make sure it is a boxed value of the given value type. Copies the value from an instance to a value type variable.
Cases:
int i = 123; A value type
Object box = i; Boxing
int j = (int) box; Unboxing

Boxing conversions
Cancel Boxing
3, implicit and explicit
The following conversions belong to implicit conversions: Example: Object o=i;
Identity conversion.
An implicit numeric conversion.
An implicit enumeration transformation.
An implicit reference conversion.
Boxing conversions.
An implicit constant expression conversion.
User-defined implicit conversions.
The following conversions belong to an explicit conversion: Object 0= (object) I;
All of the implicit conversions.
An explicit numeric conversion.
An explicit enumeration transformation.
An explicit reference conversion.
Explicit interface conversions.
Cancels the boxing conversion.
User-defined explicit conversions
4, delegate (commissioned)
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 references an object instance and an instance method of that object. The closest thing to a delegate in C or C + + is a function pointer, but a function pointer can only reference a static function, while a delegate may refer to both static and instance methods. In the latter case, the delegate stores not only a reference to the method entry point, but also a reference to the object that called its method. Unlike C + + function pointers, delegates are completely confronted with objects; unlike C + + pointers to member functions, delegates encapsulate both object instances and methods. A delegate declaration defines a class derived from a class System.Delegate. A delegate instance encapsulates one or more methods, each of which is called a callable entity. For instance methods, the callable entity consists of an instance and a method on that instance. For static methods, the callable entity consists of only one method. Given a delegate instance and the appropriate set of parameters, all methods of this delegate instance can be invoked with that parameter set. An interesting and useful property of a delegate instance is a class for which it does not understand or care about the methods it encapsulates; What really matters is that the method is compatible with the type of the delegate, which makes the delegate ideal for anonymous calls. The optional formal parameter list specifies the parameters of the delegate, and the return type indicates the return type of the delegate. If all of the following two conditions are true, the method and delegate type are compatible: (the concept of compatibility is that the method can be delegated with the delegate of this Declaration).
1 They have the same number of parameters, and the same type, same order, and same parameter modifiers.
2 They return the same type.
The delegate type in C # is the name equivalent, not the structure equivalent. (note, however, that an instance of two different but structurally equivalent delegate types may be compared to equal), and that, to be exact, two different delegate types with the same argument list, signature, and return type are considered different delegate types. The collection of methods that the delegate instance encapsulates is called the invocation list.
5, Interface (interface)
[Attributes] [modifiers] interface identifier [: Base-list] {interface-body}[;]
An interface defines a contract. A class or struct that implements an interface must comply with its contract. An interface can inherit from multiple base interfaces, and a class or struct can implement multiple interfaces. Interfaces can contain methods, properties, events, and indexers. The interface itself does not provide implementations of the members that it defines. interface specifies only the members that the class or interface that implements the interface must provide. An interface can be a member of a namespace or class, and can contain the signatures of the following members: The Method property Indexer.
An interface can inherit from one or more base interfaces. Interfaces can be implemented by classes. The identifier of the implemented interface appears in the base list of the class. An inherited interface is known as an explicit base interface for that interface. When an interface has one or more explicit base interfaces, in the interface declaration, the interface identifier is followed by a colon and a comma-delimited list of base interface identifiers. 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 transitive closure of the explicit base interface, their explicit base interface (and so on). Interface inherits all members of its base interface. Interface members are accessed through member access and indexed access expressions in the form of I.M and I[a, where I is an instance of an interface type, M is a method, property, or event of that interface type, and a is a list of indexer parameters. Interfaces can be implemented by classes and structs. To indicate that a class or struct implements an interface, the interface identifier is included in the list of base classes for that class or struct. The process of locating an implementation of an interface member in an implementation class or struct is called an interface mapping.
6,object
The object class type is the final base class for all other types. Each of the types in C # derives directly or indirectly from the object class type. You can give any type of numeric value to the type object.
7,string type
An instance of the String class represents a Unicode string. Although a string is a reference type, the equality operator (= = and!=) is defined as the value (7.9.7 string equality operator) that compares a string object instead of a reference. This makes testing of string equality more intuitive. The string is of type string and can be written in two forms, in quotation marks and by @. Quoted strings are enclosed in double quotation marks ("), and can contain any character, including a newline sequence, with the @-induced string to begin with @ and double quotes. The string that is generated by @ begins with a @ and is enclosed in double quotes. To include a double quotation mark in a @-induced string, use two pair of double quotes: Another use of the @ symbol is to employ a referenced (/reference) identifier that happens to be the C # keyword.
8, modifier
Modifier action
Access modifiers
Public
Private
Internal
Protected
Specifies the accessibility of declared types and type members.

Access not restricted
Only classes that contain members of the class can access the
Only current projects can access
Only the class that contains the member and the inherited class can access the
Abstract indicates that a class can only be a base class for other classes.
const specifies that the value of a field or local variable cannot be modified.
Event declares an incident.
extern indicates that this method is implemented externally.
Override provides a new implementation of a virtual member inherited from a base class.
ReadOnly declares a field that can only be assigned as part of the declaration or in a constructor of the same class.
sealed specifies that the class cannot be inherited.
A static declaration belongs to the type itself and not to a member of a particular object.
Unsafe declares an unsafe context.
Virtual declares a method or accessor in a derived class whose implementation can be changed by an overriding member.
Volatile indicates that a field can be modified in a program by the operating system, hardware, or concurrently executing threads.

9, statement
Statement is a program instruction. Unless otherwise stated, statements are executed sequentially. C # has the following categories of statements.
Category C # keywords
SELECT statement if, else, switch, case
Iteration statement do, with, foreach, in, while
Jump statement breaks, continue, default, Goto, return
Exception handling statements 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 collection of objects. The foreach statement is used to iterate through the collection to obtain the required information, but it should not be used to change the contents of the collection to avoid unpredictable side effects. The form of this statement is as follows:
foreach (type identifier in expression) statement
To iterate through the collection, the collection must meet specific requirements. Collection type:
Must be interface, class, or struct.
You must include an instance method named GetEnumerator for the return type, such as enumerator (see below).
The enumerator type (class or struct) must contain:
A property named current that returns ItemType or can be converted to a type of this type. The property accessor returns the current element of the collection.
? A bool method named MoveNext that returns true if it increments the item counter and there are more items in the collection.
There are three ways to use collections:
Use the instructions above to create a collection. This collection can only be used for C # programs.
1. Use the instructions above to create a generic set and implement the IEnumerable interface in addition. 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 emit a signal that an anomalous condition (exception) occurs during program execution. The throw statement is in the form of:
throw [expression];
Expression: Exception object. When the current exception object is thrown again in a catch clause, it is omitted.
(3) Try-catch statement
The Try-catch statement consists of one try block and one or more catch clauses followed, which specifies handlers for different exceptions. The Try-catch statement takes 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
Prevents variables from being repositioned by garbage collection.
(5) lock
The LOCK keyword marks a statement block as a critical section.
6. Method parameters
If you declare a parameter for a method that does not have ref or out, this parameter can have an associated value. The value can be changed in the method, but the changed value is not persisted when control is passed back to the calling procedure. You can change this behavior by using the method parameter keyword. If there is no ref,out, the value is assumed to be passed, although the value of the parameter can be modified in the method, but the modified value will not also be in the program that called the method.
Params:p arams keyword can specify method parameters that take parameters in variable number of parameters
Ref: Reference Delivery
Out:
7, namespace (name space)



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.