C #2.0 classic Reading Notes (Part 1: C # getting started and improving)

Source: Internet
Author: User
Tags define abstract

Http://hi.baidu.com/xiaoyear/blog/item/2f52e91310ad8422dc5401ba.html (RPM)

Part 1: C # getting started

1. You can specify an alias for a class or namespace.
For example (namespace alias): Using myalias = mynamespace1.mynamespace2. space3

2. Other development tools of C #: sharpdevelop/snippet Compiler
C # software for recompilation:. Net Reflector
C # unit test tool: nunit
C # code generation tool: codesmith
C # code standard detection tool: fxcop

3. programming style suggestions
A. There is a blank line between the code blocks.
B. The left braces count as one line.
C. The nested layers of parentheses must be indented (4 spaces)
D. variable naming rules (that is, the first letter, followed by the first letter of the word, such as mydata)

The four numeric types of 4, C # are not supported by other languages on the. NET platform. The four numeric types are (sbyte, ushort, uint, ulong)

5. Two Methods of explicit data type conversion:
A, system. Convert class provides various types of data conversion
B, in parentheses, for example, (INT)

6. solution to global variables in C #
Use static classes and static variables, for example, Private Static string name = "Wang ";

7. Differences between a ++ and ++
A ++ first participates in the operation and then the self-addition ++ a first and then participates in the operation

8, ^ operator number
Represents an exclusive or in two bool operations (the result is true only when one operand is true)
Used in two int-type operations to indicate bitwise XOR or (5 ^ 6 = 101 ^ 110 = 011 (3 ))

9. C # language data type detection
Check Data Type: int A = 5; A is int returns true
Capacity of the detection type: sizeof (INT );

Logical operators in 10, C #
"&" Indicates "and", both of which are true and true.
"&" Indicates "and". Unlike &, it calculates the second operand only when necessary.
"|" Indicates "or". Either of them is true and the result is true.
"|" Indicates "or". Unlike |, it calculates the second operand only when necessary.
"^" Indicates "XOR". Only one of the two is true and the result is true.
"!" Indicates "not", involved in variable Inversion

11. If the case content in the switch statement is null, directly execute the content in the next case of case.

12. Switch statements support enumeration-type branch processing

13. The ternary operator replaces the simple if else statement.
For example:
If (A> B)
{C = a-B}
Else {c = a + B} is written as c = A> B? A-B: A + B

14. Four types of loop statements
A: For () {}: the number of executions is known. It can be a forward or backward step. The step size is not set to 1.
B: foreach () {} traverses the set object
C: While () {}; execute 0 or more times, and change the cycle judgment condition in the loop body
D: Do {} while (); execute at least once to change the cycle judgment condition in the loop body.

15. Change the Four keywords executed by the loop body:
A: goto jumps from the cyclic body to the tag position. It is not recommended to skip from the cyclic body to the circulating body.
B: Return jumps out of the current process, not the loop body.
C: The Break jumps out of the current body (skip to the exclusive body of the current cycle)
D: Continue skips the current cycle and enters the next cycle.

16. A classic example of the Bubble Sorting Algorithm

Static void main (string [] ARGs)
{

Int [] myarray = new int [5] {12, 9, 35, 68, 51 };
Int A = 0;

For (INT I = 0; I <myarray. length; I ++)
{
Console. writeline (myarray [I]);
}
Console. Readline ();

// The outer loop is used to control the end value of the inner loop, because the maximum number in the array is pushed to the end of the array every time it runs.
For (INT I = myarray. Length-1; I> = 0; I --)
{
For (Int J = 0; j <I; j ++) // The inner loop is used to compare the adjacent two numbers and push the large numbers backward.
{
If (myarray [J]> myarray [J + 1])
{
A = myarray [J];
Myarray [J] = myarray [J + 1];
Myarray [J + 1] =;
}
}
}

For (INT I = 0; I <myarray. length; I ++)
{
Console. writeline (myarray [I]);
}
Console. Readline ();

}

17. Return in a method means that the control is handed over to the method caller. The method description can be automatically generated by using the // annotation in the method line. When the caller uses this method, vs2005 displays the comments (smart notification ).

18. Use of ref in the input parameter in the method (so that the parameter is passed by reference, the ref parameter must be initialized)

19. The out usage in the input parameters in the method is the same as that in the ref. The difference is that the out input parameters do not need to be initialized.

20. Three Methods of overloading
A: The number of parameters varies.
B: different parameter types
C: The parameter type is the same, but the parameter reference and non-reference are also heavy loads (for example, static int add (ref int A, ref int B) and static int add (int A, int B ))

21. The system. Environment class obtains a lot of information in the current running environment.

Bytes ------------------------------------------------------------------------------------
Part 2 C # Improvement

1. The use of the class design diagram in vs2005 uses the graphical interface design class to automatically generate class code

2. Composition of A Class (a member of the class ):
A: Constructor (reloaded)
B: destructor (a class can have only one destructor and cannot be called). When the window, file, or network connection is closed to a certain extent

You should use the destructor to release these resources.
C: field (used inside the class)
D: attribute (used to obtain and modify the value of fields in the class, implemented with get set, get set)
E: method.

3. Access Control modifier for class members (private (default) Public protected internal)
Specifically, protected can only be accessed within the class and in its derived class, between private and public
Internal is an access modifier for type and type members. It is used to restrict class members so that only classes in the current namespace or to a certain extent can access it.

4. The Enumeration type is a lightweight value type and is declared using enum. enumeration is used to express the set behavior of a specific set of values, such as the optional status of Windows Forms.

5. The structure is declared by struct. The difference between the structure and the class:
A: The structure is a value type and an address is allocated on the stack. The disadvantage is that resources are limited and it is not suitable for processing large logical complex objects. The structure does not support inheritance, but can

To implement the interface.
B: The class is a reference type and the address is allocated on the stack.
C: The structure execution efficiency is higher than the class, but it is only suitable for small objects.

6. Pay attention to the differences between multi-dimensional arrays and staggered arrays (arrays of arrays/sawtooth arrays.
Multi-dimensional array: int [,] intarray = new int [2, 2] {10, 11}, {12, 13 }}
Staggered array (elements are arrays with different dimensions and sizes ):
Int [] [] intarray = new int [3]
Int [0] = new int [] {10, 20 };
Int [1] = new int [] {15, 30, 33 };
Int [2] = new int [] {15, 22, 25, 29, 65 };
Or:
Int [] [] intarray = new int [] []
{
New int [] {10, 20 },
New int [] {15,30, 33 },
New int [] {15, 22, 25, 29, 65}
}

7, A: array traversal foreach,
B: Clear the array system. Array (myarray, index, length ),
C: Search for array. indexof (myarray, object) and advanced search for four parameters to narrow the search range
D: array sorting array. Sort (myarray) sequential Sorting/array. Reverse (myarray) note that this is a reverse array and not a reverse order.

8. The biggest difference between arraylist and array is that the capacity of arraylist is automatically expanded,
Note that arraylist. Capacity shows the capacity, and arraylist. Count shows the number of elements.
Add, insert
Clear, remove, removeat
Conversion from arraylist to array
Myarraylist. trimtosize () to reduce the capacity to the current capacity and reduce memory waste.

9. The Dictionary of the set of hashtable key-value pairs cannot be accessed through its index. The table value can only be accessed through the key, that is, hashtable [Key].
Add Element Method: add (Key, value)
Element deletion method: clear ()/remove (key)
Traversal: foreach (dictionaryentry D in h1)
Check whether hashtable. Contains/hashtable. containskey contains the specified key.
Whether hashtable. containsvalue contains the specified value

10. The queue class is a first-in-first-out set.
Three main operations of queue
Enqueue adds an element to the end of the queue
Dequeue extracts the oldest element from the beginning of the queue and removes this element
Peek returns the oldest element from the queue but does not delete it.

11. The stock Stack class is a back-to-first-out set.
Three major operations of stock
Push adds an element to the top of the stock.
Pop extracts the latest element from the top of the stock and removes it.
Peek returns the latest element from the top of the stock but does not delete it.

12. sortedlist is a combination of arraylist and hashtable. When both key-value pairs and sequential indexes are required, sortedlist

13. dictionary class (generic class)

14. Interface interface (the interface only defines the functions, but does not implement the functions)
The member implementing the interface cannot be static or non-public
C # multi-inheritance of classes is not allowed. A class can inherit both classes and interfaces.
The interface can be passed as a parameter to the method. The interface can be used as a return value.
Interface implementation is divided into general implementation and explicit implementation. Explicit implementation is to solve the following problem: If a class inherits two interfaces. If the two interfaces have a method name with the same name, a name conflict occurs. You can use an explicit interface to solve this problem.

15. Abstract abstract class (an abstract class is similar to an interface, but a specific implementation of a method is allowed. It is a combination of an interface and a common class)
Abstract classes can define abstract methods, but abstract (attributes) cannot be implemented in detail,
The derived class of the abstract class must implement all abstract members. (Using override to implement abstract members)

16, sealed sealing class
The sealed class is used to restrict other classes from inheriting from it. It is not recommended to use it. Except that it cannot be inherited, it is used in the same way as that of a common class. It is used: when the class contains sensitive information, you can consider using a sealed class.

17. Advanced Conversion
A: Is is used to check whether the object is compatible with the specified type
B: () used to force type conversion
C: as is used for conversion between compatible types (compared with forced conversion, it does not cause exceptions when conversion fails)

18, virtual and override
Virtual is used to modify methods, attributes, indexers, and event declarations. Override modifiers must be used to extend or modify the inherited methods, attributes, indexers, and abstract or virtual implementations of event declarations.

19. attributes and fields in the class members:
The attribute is external and defined as public protected. The field is inside the class and can only be private, reflecting the encapsulation of the class.

20. The iterator (new knowledge of. net2.0) can traverse the data structure in the class through the iterator. The definition method is as follows:
Public System. Collections. ienumerator getenumerator (){}

21. segment class (. new knowledge of net2.0) class, structure, and interface definition can be split into multiple source files. Each source file contains a part of the class, and the partial class is defined by partial. for example, form1.cs and form1.designer in Windows Forms applications. CS is used in two files.
Note:
A: For a single class, do not use the partial class definition Keyword: Partial.. NET will look for another part, increasing the burden on the system.
B: The accessibility of each branch class should be consistent.
C: each segment class should be defined using the partial keyword.
D: when different branch classes inherit different classes and interfaces,. NET will combine them well.
E: the partial keyword should appear in the previous position of class, interface, and struct.

22. indexer: provides indexing methods similar to Arrays for classes, similar to attributes. The indexer can also be used in structures and interfaces. If an empty indexer is defined in an interface, the class that implements this interface must implement its interface indexer.

23. Generic (New. net2.0 knowledge)

 

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.