C # is a component-Oriented Language Programming Language trend: self-contained self-description
Garbage collection Exception Handling type security
C # integration of inheritance with C ++ and Java
Version Control virtual override
. CS as extension
Namespace program type member Assembly
The extension of an assembly is. dll (Library) or. EXE (application ). It mainly depends on whether it contains the main entry function.
C # No class library
X () method and delegate call
X is t
X as t
Int [] One-dimensional array int [] [] One-dimensional array int [,] Two-dimensional array
The C # type system is uniform, and any type of value can be processed as an object.
The value type can be processed as an object through the boxing and unboxing operations.
For example, int I = 0; object xx = I; Int J = (INT) xx;
Heavy Operator
Statement: block (multiple statements can be written in the context where only one statement can be used), statement declaration, expression statement, and select statement (if, switch), iteration Statement (while, do, for, foreach), jump Statement (break, continue, Goto, throw, return)
Try-catch: catch exceptions during BLOCK statement execution
Try-finally specifies a termination code block. Execution is required no matter whether it is abnormal or not.
Checked and unchecked are used to control the overflow of integer arithmetic elements through and conversion.
Lock is used to obtain the mutex lock of a given object, execute the statement, and then release the lock.
// Using is used to obtain a resource, which is equivalent to # include or import
// Namespace is equivalent to package
Foreach and freemarker have similar languages: foreach (int x in Nums ){......}
Note that foreach only references the value of the element in the set and cannot change the value of the element in the set.
The try throw catch clause determines the throw content, which is slightly different from Java.
Using obtains a resource, executes a statement, and processes the resource.
For example, using (textwriter W = file. createtext ("test.txt ")){
W. writeline ("line one ");
}
Class declaration:
Specifies the features and modifier of the class. The base class of the class name is implemented by the class.
Create an instance (object) using the new operator)
Class Member: static member (class) instance Member (object)
Class access permissions: public, protected, internal (only limited to the current Assembly), protected internal, private
Class declaration, you can add ":" After the class name, write the base class name. If the base class name is omitted, the object base class is derived by default.
Static Field: static modified. It indicates only one storage location. No matter how many instances are created, the static field has only one copy. (This feature can be used as a message interface)
Instance field: each object has its own independent copy.
Method:
Static Method: can only be accessed through classes
Instance method: access through objects
Method Signature: Method Name, number of parameters, modifier and type of each parameter
Parameters:
Value parameter: equivalent to an independent variable. The initial value is obtained from the independent variable passed by this parameter. modification to the value parameter does not affect the passed independent variable.
Reference parameters: use Yahoo Input and Output Parameters for transmission. The independent variable that affects the transmission of drinking parameters. (REF)
Output Parameters: used to pass output parameters, similar to referenced parameters. The difference is that the initial value of the independent variable provided by the caller is always irrelevant. (Out)
Parameter array: the list of variable-length independent variables can be passed to the method. Only the last parameter of the method can be declared as an array parameter, and the array parameter must be one-dimensional. (Params) with array parameters
Pass reference type parameters
Variables of the reference type do not directly contain their data; they contain references to their data. When a parameter of the reference type is passed through a value, it is possible to change the data pointed to by the reference, such as the value of a certain type of member. However, you cannot change the reference value (pointer value). That is, you cannot use the same reference to allocate memory for the new class and keep it out of the block. To do this, useRef(OrOut. For simplicity, the following example usesRef.
In a method call, you can either pass a single variable of the parameter array type or several variables of the element type of the parameter array. In the latter case, the array is automatically created and initialized by the given independent variable.
Method body and local variable: Like java, you must assign a value to the method body before obtaining the local variable value.
Static methods and instance methods: static methods do not operate on specific instances and can only access static members. The instance method skill can also be used to access static members. You can use this to access the instance itself on the instance that calls the instance method. This is incorrect in static methods.
Virtual Methods, override methods, and abstract methods: in a virtual method call, the runtime type of the Instance involved in the call determines which implementation of the method should be called, non-virtual methods mainly depend on the selection of methods for determining the type during compilation. The virtual method is used to introduce new methods, while the virtual method declaration is used to make the existing method inheritance dedicated, virtual
Abstract method: Virtual methods that are not implemented. Abstract: only abstract methods can be used in abstract classes.
Delegate: function pointer has a similar effect and is used to call some method bodies. This avoids repeated compilation of the same method body and can be directly used in different classes.
An interface is an implementation class that does not contain operation implementations.
Abstract class
Message:
Class modifier:
Abstract: abstract class. It cannot be used to create instances and can be used to generate inheritance classes.
Internal: This class can be seen in the same assembly.
New: used only for nested classes, indicating that the class hides an inherited member with the same name.
PRIVATE: Nested class. It can only be accessed by the defined class.
Protected: Nested class. It can only be accessed from the derived class of the defined class.
Public: Anyone can declare the reference of this class object or access its public members.
Sealed: cannot be used as the base class of other classes (a bit similar to the final modifier in Java)
Class member:
Field: member variable. Modifier: Public, private, internal, protected. Static domain and instance domain
Attribute: A class member that provides access to the characteristics of an object or class. Attribute Declaration includes a specific set: 1 new modifier and 4 Access modifiers (static, virtual, override, abstract)
Method: one new modifier and four access modifiers (static, virtual, override, abstract) Note: defining another method in one method is a syntax error. However, you can call another method in one method body.
Nested classes and nested interfaces:
Constructor: If the constructor is modified by public, internal, and other modifiers (protected, protected internal, and private), The Destructor must be displayed.
System. String is an unchangeable string. System. stringbuilder stores a variable string and provides some methods to modify the string.
The const keyword is used to declare the compilation frequency, and readonly is used to declare the runtime frequency.
Non-static methods cannot be written as static methods or virtual methods. The signature of the two methods must be consistent during method overwriting. The only exception is to replace virtual with override.
Indexer: allows you to index the array elements of an object based on the array amount. Attributes With Parameters
Attribute: A class member that accesses the properties of an object or a class. The get accessors do not require a parameter, but the type of the returned value must be the same as that specified. Set does not require a parameter either. The Compiler assumes that it carries a parameter and its type is the same as the attribute value, which is expressed as value. Attributes are defined as domain members in the class.
It is easy to implement a delegate. You can use the following three steps to implement a delegate:
- Declare a delegate object. It should have the same parameter and return value type as the method you want to pass.
2. Create a delegate object and pass in the functions you want to pass as parameters.
3. Use the object created in the previous step to call the method where an asynchronous call is to be implemented.
With the implementation of Delegate, We can summarize the implementation of custom events into the following steps:
1. Define the delegate object type. It has two parameters. The first parameter is the event sender object, and the second parameter is the event parameter class object.
2. Define the event parameter class, which should be derived from the system. eventargs class. This step can be omitted if the event does not contain parameters.
3. Define the event processing method. It should have the same parameter and return value type as the delegate object.
4. Define the event object with the event keyword. It is also a delegate object.
5. Add the event to the event queue with the + = Operator (the-= operator can delete the event from the queue ).
6. Call the delegate method to write the event trigger method where an event needs to be triggered. In general, this method should be a protected access restriction. It cannot be called in public mode, but can be inherited by the quilt class. The name is oneventname.
7. Call the event trigger method to trigger the event where appropriate.
Using system;
Public class eventtest
{
// Step 1, define the delegate object
Public Delegate void myeventhandler (Object sender, system. eventargs E );
// Skip step 2
Public class myeventcls // class for monitoring and Processing Event execution
{
// Step 3: Define the event processing method. It has the same parameters and return value type as the delegate object. //
Public void myeventfunc (Object sender, system. eventargs E)
{
Console. writeline ("My event is OK! ");
}
}
// Step 4: Use the event keyword to define the event object
Private event myeventhandler myevent;
Private myeventcls myecls;
Public eventtest ()
{
Myecls = new myeventcls ();
// Step 5: add the event to the queue with the + = Operator
This. myevent + = new myeventhandler (myecls. myeventfunc );
Note that + = is used here, rather than. Add, which is common in Java.
}
// Step 6: Write the event trigger function by calling delegate
Protected void onmyevent (system. eventargs E)
{
If (myevent! = NULL)
Myevent (this, e );
}
Public void raiseevent ()
{
Eventargs E = new eventargs ();
// Step 7: trigger the event
Onmyevent (E );
}
Public static void main ()
{
Eventtest ET = new eventtest ();
Console. Write ("Please input 'A ':");
String S = console. Readline ();
If (S = "")
{
Et. raiseevent ();
}
Else
{
Console. writeline ("error ");
}
}
}
The event attribute supports the add and remove keywords to replace get and set
In C #, class inheritance can only be a single inheritance, but multiple inheritance can be implemented through interfaces. (Java)
Therefore, there is no concept of a virtual base class.
Constructors and destructor in the base class cannot be inherited. The rest are unconditionally inherited. The modifiers only affect the access permissions of the members in the derived class to the members in the base class.
Only accessible methods can be reloaded.
Base access base class member sealing class: sealed is generally used with override
Abstract classes can implement attributes, but attributes and indexes in interfaces are only declarations and are implemented by related classes.
An interface is a design expression, and a class is a set of design and implementation.
If a class implements an interface, it inherits all its parent interfaces.
Hide interface properties, but you can access the parent interface with a fully qualified name.
Interface: If you know in advance that a class implements this interface, you can directly access this interface through the class object.
Array: composed of elements of the same type
Type name [] or type [] Name
C # array initialization using new
Declaring arrays does not actually create them. In C #, arrays are objects and must be instantiated. If you provide an initial value, you can also omitNew Operator.
Staggered array: int [] [] numbers = {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
Int [] [] numbers = new int [] [] {New int [] {2, 3, 4}, new int [] {5, 6, 7, 8, 9 }};
The members of the member number can be directly performed, similar to those of the member number in C/C ++.
C # also providesForeachStatement. This statement provides a simple and clear method to cyclically access the elements of the array. With multidimensional arrays, you can use the same method to cyclically access elements.
In C #, arrays are actually objects. System. array is the abstract base type of all array types. You can use the attributes of system. array and other class members.
Common array methods:
Length attribute: Total number of elements in all dimensions of the array.
Rank attribute: indicates the dimension in the array.
Sort method: sorts one-dimensional arrays. It is a static method of the array class. (It means that it can be accessed only through the array class)
The reverse method is used to reverse a one-dimensional array.
Getlowerbound and getupperbound methods: the array specifies the lower limit and upper limit of the dimension.
The clear method re-initializes all elements in the array and sets a series of elements in the array to zero,FalseOr empty reference.
The array size in C # is fixed. Similar to Java.
Set:
List: Declaration: List <type> the list name contains hundreds of types, or user-defined types.
Get the size: List. capacity can be
Search element: indexof (): used to search the index number of the element in the list
Lastindexof (): used to search for the last index number of the specified Element in the list.
Findindex (): used to retrieve the first position of an element in the list.
Findlastindex (): used to retrieve the last position of an element in the list.
Find (): The first matching object in the search list.
Findlast (): The last object matched in the search list
Exists (): Check whether the element exists
Delete A set element: removeat (): deletes the specified element according to the index key.
Removeall (): deletes all elements in the list.
Removerange (): deletes multiple elements from a collection.
Clear (): Clear all elements in the list
Collection classes that can be directly used in. system. collections include:
Arraylist
Implemented interfaces: ilist, icollection, and ienumerable
As long as the set is not modified, arraylist supports multiple readers at the same time safely.
With the addition of elements to the arraylist, the capacity increases automatically by reallocation as needed (2 times increase)
If you need to create an object array but do not know the size of the array in advance, you can use arraylist
Arraylist references all elements as object objects. Therefore, type conversion is required when accessing the elements of arraylist.
Advantages: dynamic resizing, flexible and convenient insertion and deletion of elements, and sorting
Disadvantage: The insertion performance is not as good as that of an array or a strong type.
Bitarray
Implemented interfaces: icollection and ienumerable
Manage the compressed array of bits.
3) hashtable
Implemented interfaces: idictionary, icollection, and ienumerable
You can add or delete elements to or from hashtable. Some elements are like arraylist, but they do not have such high performance overhead.
4) sortedlist
Implemented interfaces: idictionary, icollection, and ienumerable
Sortedlist combines the advantages of arraylist and hashtable, and can be sorted by pressing values.
5) queue
Implemented interfaces: icollection and ienumerable
Queque is a queue for first-in-first-out access to various elements.
You can call the getenumerator () method of the queque object to obtain the ienumerator object and traverse each element in the queue.
6) Stack
Implemented interfaces: icollection and ienumerable
A stack is a stack, followed by first-in-first-out access to each element.
You can call the getenumerator () method of the stack object to obtain the ienumerator object and traverse each element in the stack.
Queue: FIFO. Insert a segment to another end and delete it.
Non-generic; system. Collections. Queue
Generic: system. collection. Generic. Queue <t>
Enqueue adds an element to the end of the queue.
Dequeue reads and deletes a data at the other end of the queue.
Peek () reads a data from the head of the team, but does not delete it.
Count: returns the number of elements in the queue.
Trimexccess (): reset the queue capacity. Because dequeue reads and deletes data, it will leave an empty element to continue occupying the space.
Contains: determines whether an element is in the queue
Copyto: copy an element from the queue to an existing array.
Toarray: returns a new array containing queue elements.
STACK: first-in-first-out push ()-out stack POP ()-in-Stack
Linked List: physically scattered, logically continuous
List: returns the list of nodes.
Next: returns the next node.
Previous: returns the previous node.
Value: return the elements related to the node. Its type is T.
Cout first last addafter () addbefore () addfirst () addlast ()
Remove () removefirst () removelast ()
Clear () contains ()
Find () searches for the elements from the beginning of the linked list
Findlast () is searched from the end of the linked list.
Sortedlist: a set of key and value attributes for efficient index Retrieval
Containskey (), containsvalue is used to check whether it contains the specified key active Value
Trygetvalue