VS2005 Common shortcut keys
debugging shortcut keys
F6: Build Solution
CTRL+F6: Build Current project
F7: View Code
Shift+f7: Viewing the form Designer
F5: Start Debugging
Ctrl+f5: Start execution (without debugging)
SHIFT+F5: Stop Debugging
Ctrl+shift+f5: Restart debugging
F9: Toggle Breakpoint
CTRL+F9: Enable/Stop breakpoints
CTRL+SHIFT+F9: Delete all breakpoints
F10: Process-by-step
CTRL+F10: Run to Cursor
F11: Per-statement
Edit shortcut Keys
Shift+alt+enter: Toggle Full Screen editing
Ctrl+b,t/ctrl+k,k: Toggle Bookmark Switch
Ctrl+b,n/ctrl+k,n: Move to Next Bookmark
Ctrl+b,p: Move to previous bookmark
Ctrl+b,c: Clear All labels
Ctrl+i: Incremental Search
Ctrl+shift+i: Reverse Incremental Search
Ctrl+f: Find
Ctrl+shift+f: Finding in Files
F3: Find the next
SHIFT+F3: Find Previous
Ctrl+h: Replace
Ctrl+shift+h: Replace in file
ALT+F12: Find Symbol (List all find results)
Ctrl+shift+v: Clipboard Looping
CTRL + LEFT and RIGHT arrow keys: One word can be moved at a time
CTRL + UP and DOWN ARROW keys: Scrolls the code screen, but does not move the cursor position.
Ctrl+shift+l: Delete When moving forward
Ctrl+m,m: Hides or expands the currently nested collapsed state
CTRL+M,L: Set all procedures to the same hidden or expanded state
Ctrl+m,p: Stopping outline display
Ctrl+e,s: View Blank
Ctrl+e,w: Wrap Line
Ctrl+g: Go to the specified line
shift+alt+ arrow key: Select Rectangle text
ALT + left mouse button: Select rectangle text
Ctrl+shift+u: All Uppercase
Ctrl+u: All turns lowercase
Code shortcut keys
Ctrl+j/ctrl+k,l: List Members
ctrl+shift+ SPACEBAR/ctrl+k,p: Parameter information
Ctrl+k,i: Quick Info
Ctrl+e,c/ctrl+k,c: Comment Selection
Ctrl+e,u/ctrl+k,u: Deselect Comment content
Ctrl+k,m: Generate Method Stub
Ctrl+k,x: Inserting Code Snippets
Ctrl+k,s: Inserting the outer code
F12: Go to the definition of the called procedure or variable
Window shortcut keys
Ctrl+w,w: Browser window
Ctrl+w,s: Solution Manager
Ctrl+w,c: Class View
Ctrl+w,e: Error List
Ctrl+w,o: Output View
Ctrl+w,p: Properties Window
Ctrl+w,t: Task List
Ctrl+w,x: Toolbox
Ctrl+w,b: Bookmark window
Ctrl+w,u: Document Outline
Ctrl+d,b: Breakpoint Window
Ctrl+d,i: Immediate Window
Ctrl+tab: Active form Toggle
Ctrl+shift+n: New Project
Ctrl+shift+o: Open Project
Ctrl+shift+s: Save All
Shift+alt+c: New Class
Ctrl+shift+a: New Item
3, variables. ToString ()
Character conversion to string
12345.ToString ("n"); Generate 12,345.00
12345.ToString ("C"); Generate ¥12,345.00
12345.ToString ("E"); Build 1.234500e+004
12345.ToString ("F4"); Build 12345.0000
12345.ToString ("X"); Build 3039 (16 binary)
12345.ToString ("P"); Generate 1,234,500.00%
1.9 Chinese date display--month and day
String Stry=currenttime.tostring ("F"); Do not display seconds
1.10 Chinese Date Display _ Month
String strym=currenttime.tostring ("Y");
1.11 Chinese Date Display _ Month Day
String strmd=currenttime.tostring ("M");
1.12 Chinese Month Day
String strymd=currenttime.tostring ("D");
1.13 Take the current time, in the format: 14:24
String strt=currenttime.tostring ("T");
1.14 Take the current time, in the format: 2003-09-23t14:46:48
String strt=currenttime.tostring ("s");
1.15 Take the current time, in the format: 2003-09-23 14:48:30z
String Strt=currenttime.tostring ("U");
1.16 Take the current time, in the format: 2003-09-23 14:48
String strt=currenttime.tostring ("G");
1.17 Take the current time, in the format: Tue, SEP 2003 14:52:40 GMT
String strt=currenttime.tostring ("R");
1.18 Date time to get the current time n days
DateTime newday = DateTime.Now.AddDays (100);
string variable. Replace ("sub-string", "Replace with")
String substitution
Such as:
String str= "China";
Str=str. Replace ("Country", "central"); Change the word of the country to the central word
Response.Write (str); The output is "central"
C # Programming specification
It is extremely necessary to develop a good coding style, and no one wants to see a bunch of messy code, how painful it will be for you or others to maintain in the future, so, from now on, develop good coding habits, including variable naming, annotations, code indentation ...
1. Define types, method names, and constants by using Pascal
public class SomeClass { const int defaultsize=100; Public SomeMethod () { } }
|
2. Use camel nomenclature for local variables and method parameters
int number; void MyMethod (int somenumber) {}
|
3. Before the name of the interface, add I
Interface IMyInterface {...}
|
4. Precede the private member variable with M_. Use the Camel naming method for variable names after m_
public class SomeClass { private int m_number; }
|
5. Add a suffix to the custom attribute class attribute
6. Add a suffix to the custom exception class exception
7. Method naming uses verbs----object pairs, such as ShowDialog ()
8. The name of the method with the return value should have a description of the return value, for example getobjectstate ()
9. Use a descriptive variable name
A) Avoid variable names of single characters, such as I or T. Use a meaningful name similar to index or temp.
b) Avoid using Hungarian notation for variables of public or protected type.
c) do not abbreviate words (for example, replace number with num).
10. Always use C # pre-defined instead of using aliases in the System namespace, for example:
Use object instead of object
Use string instead of string
Use int instead of Int32
11. When using generics, the first letter of the type is capitalized. When processed. NET, the type suffix is preserved. (c#2.0 new Features)
That's right public class Linkedlist<k,t> {...}
Avoid public class Linkedlist<keytype,datatype> {....}
|
12. Define namespaces using meaningful names, such as product name or company name
13. Avoid using the type name in fully qualified mode and use the Using keyword.
14. Avoid the use of the Using keyword in a namespace
15. Organize the namespaces provided by all system frameworks together and place the namespaces provided by the third party under the System namespace
Using System; Using System.Collection.Generic; Using System.ComponentModel; Using System.Data; Using MyCompany; Using MyControls;
|
16. Use proxy derivation instead of explicitly instantiating a proxy (c#2.0 new feature)
delegate void Somedelegate (); public void SomeMethod () {...} Somedelegate Somedelegate=somemethod;
|
17. Maintain strict code indentation. Do not use tabs or nonstandard indents, such as a space. The recommended indentation is between 3 and 4 spaces.
18. Add a comment to the line code at the same level as your code indentation.
19. All comments should be checked by the spelling. Incorrect spelling in comments means a delay in development progress.
20. All class member variables should be declared at the top of the class and separated by a blank line from the declaration of the method and the property.
public class MyClass { int m_number; String M_name; public void SomeMethod1 (); public void SomeMethod2 (); }
|
21. Declare the local variable where it is closest to where a local variable is used.
22. A file name should reflect the class name it corresponds to
23. When using a partial class and distributing the class to a different file, at the end of each file name, add the part of the file implementation to the class as a whole. For example:
In MyClass.cs public partial class MyClass {...} In MyClass.Designer.cs public partial class MyClass {...}
|
24. Always put curly braces "{" On a new line
Coding Practices :
1. Avoid placing multiple classes in the same file
2. A file should only define the type within a namespace. Avoid using multiple namespaces in one file
3. Avoid writing more than 500 lines of code within a file (except for machine-generated code)
4. Avoid writing more than 25 lines of code
5. Avoid writing methods with more than 5 parameters. If you want to pass multiple arguments, use the structure.
6. Do not exceed 80 characters in one line
7. Do not manually modify any machine-generated code
A) If you modify the machine generated code, modify your encoding to accommodate this coding standard
b) Use the partial classes feature as much as possible to improve maintainability. (c#2.0 new Features)
8. Avoid commenting on the very intuitive content. The code itself should be able to interpret its own meaning. High-quality code consisting of a readable variable name and method name should not require annotations.
9. Comments should only describe some of the assumptions of the operation, internal information of the algorithm, and so on.
10. Avoid commenting on methods
A) Use sufficient external documentation to explain the API
b) It is only necessary for those other developers to put the information in the method-level comments
11. In addition to 0 and 1, do not hard-code numeric values, by declaring a constant to replace the value
12. Use the const keyword only for values that are immutable, such as the number of days in a week.
13. Avoid using the CONST keyword for read-only (read-only) variables. In this case, use the ReadOnly keyword directly
public class MyClass { public const int daysinweek=7; Pubic readonly int number; Public MyClass (int somevalue) { Number=somevalue; } }
|
14. Assert each hypothesis. On average, there should be an assertion for every 5 lines.
Using System.Diagnostics; Object GetObject () {...} Object Someobject=getobject (); Debug.Assert (Someobject!=null);
|
15. Each line of code should be checked in the form of a white box test.
16. Catch only those exceptions that you can explicitly handle yourself.
17. If an exception is required in a catch statement block, only the exception caught by the catch (or other exception created based on the exception) is thrown, which maintains the stack location where the original error resides.
catch (Exception Exception) { MessageBox.Show (Exception. Message); throw;//or throw exception; }
|
18. Avoid using the return value as the error code for the function.
19. Avoid custom exception classes.
20. When customizing the exception class:
A) Let your custom exception class inherit from the Exception class
b) Provide a customized serialization mechanism
21. Avoid defining multiple main () methods in one assembly (assembly).
22. Only those methods that are absolutely needed are defined as public, while other methods are defined as internal.
23. Avoid friend assemblies, as this increases the coupling between assemblies.
24. Avoid making your code dependent on assemblies that run in a particular place.
25. Minimize the amount of code in application assembly (EXE client assemblies). Use class libraries to contain business logic.
26. Avoid explicitly specifying the value of an enumeration
That's right public enum Color { Red,green,blue }
Avoid public enum Color { Red=1,green=2,blue=3 }
|
27. Avoid specifying a type for enumerations
Avoid public enum Color:long { Red,green,blue }
|
28. For the IF statement, always use a pair of {} to enclose the following block of statements, even if there is only one statement.
29. Avoid using ternary conditional operators.
30. Avoid using the Boolean value returned by the function as a conditional statement. Assigns the return value to a local variable and then detects it.
Bool Iseverythingok () {...}
Avoid if (Iseverythingok ()) {...}
That's right BOOL Ok=iseverythingok (); if (OK) {...}
|
31. Always use a zero-based array.
32. Always use a For loop to explicitly initialize an array of reference members:
public class MyClass {} const int arraysize=100; Myclass[] Array=new myclass[arraysize]; for (int index=0;index<array. length;index++) { Array[index]=new MyClass (); }
|
33. Use attributes to override member variables of public or protected types.
34. Do not use the inherited new operator to overwrite the implementation of new with the override keyword.
35. In a non-sealed (non-sealed) class, always define the methods of public and protected as virtual.
36. Never use unsafe code unless you are interacting with other languages.
37. Avoid display type conversions. Use the AS keyword to safely convert to another type.
Dog dog=new germanshepherd (); Germanshepherd Shepherd=dog as Germanshepherd; if (shepherd!=null) {...}
|
38. Always check whether it is null before invoking an agent.
39. Do not provide the event member variable for public. Use Event Accessor instead.
public class MyPublisher { MyDelegate m_someevent; Public event MyDelegate Someevent { Add { M_someevent+=value; } Remove { M_someevent-=value; } } }
|
40. Avoid defining event handling agents. Use Eventhandler<t> or GenericEventHandler.
41. Avoid displaying triggering events. Use Eventshelper security to publish events.
42. Always use the interface.
43. The ratio of methods and properties in interfaces and classes should be around 2:1.
44. Avoid interfaces with only one member.
45. Strive to ensure that an interface has three members.
46. Do not let the number of members in one interface exceed 20, while 12 is a more practical limitation.
47. Avoid including events in the interface.
48. When using an abstract class, provide an interface.
49. Expose the interface in the class inheritance structure.
50. It is recommended to use an explicit interface implementation.
51. Never assume that a type supports an interface. Always ask before you use it.
SomeType obj1; IMyInterface Obj2; /*some code to initialize obj1,then:*/ Obj2=obj1 as IMyInterface; if (obj2!=null) { Obj2. Method1 (); } Else { Handle Erro in expected interface }
|
52. Do not hard-code the string displayed to the user. The resource to use.
53. Do not hardcode strings that may change with the release environment, such as database connection strings.
54. Replace "" with String.Empty
Avoid String Name= ""; That's right String Name=string.empty;
|
55. Use StringBuilder instead of string when using a long string.
56. Avoid providing methods in the structure
A) parameterized constructors are encouraged to use the
b) You can overload the run character
57. When declaring a member, always provide a statement constructor.
58. Try not to use late binding (late-binding) when early binding (early-binding) is possible.
59. Let your application support tracing and logging.
60. Do not use the GOTO keyword except to implement code jumps in the switch statement block.
61. Always provide an assertion in the default case of the switch statement.
int Number=somemethod (); Swith (number) { Case 1: Trace. WriteLine ("Case 1:") Break Case 2: Trace. Writeline ("Case 2:"); Break Default Debug. Assert (FALSE); Break }
|
62. Do not use the This keyword in addition to calling other constructors in one constructor.
Example of proper use of ' this ' public class MyClass { Public MyClass (String message) { } Public MyClass (): This ("Hello") { } }
|
63. Do not use the base keyword to access the members of the base class unless you are calling a base class constructor to have a subclass name conflict resolved
Example of proper use of ' base ' public class Dog { Public Dog (string name) { } Virtual public void Bark (int howlong) { } } public class Germanshepherd:dog { Public Germanshepherd (string name): Base (name) { } Override public void Bark (int howlong) { Base. Bark (Howlong) } }
|
64. Do not use Gc.addmemorypressure ()
65. Do not rely on Handlecollector
66. The Disponse () and Finalize () methods are implemented based on the content of chapter Fourth of programming. NET Components 2/e.
67. Always run the code in the unchecked state (for performance reasons), but in order to prevent overflow or underflow, use checked mode decisively.
int calcpower (int number,int power) { int result=1; for (int count=1;count<=power;count++) { Checked { Result*=number; } } return result; }
|
68. Use conditional methods to replace code that explicitly makes method call exclusions (#if ... #endif)
public class MyClass { [Conditional ("Myspecialcondition")] public void MyMethod () {} }
|
69. Do not define constraints in the generic interface. Interface-level constraints can often be overridden by strong types.
public class Customer {} Avoid: public interface ilist<t> where T:customer {}
That's right: public interface icustomerlist:ilist<customer>
|
70. Do not define method-related constraints on the interface.
71. Do not define constraints on the agent.
72. If a class or method provides generic and non-generic versions, select the generic version first