Idesign C # Code Specification (3)

zhaozj2021-02-12  138

Continued two, Idesign C # coding specifications.

34. Avoid using new inheritance, but use Override. Avoid Using The New Inheritance Qualifier. Use Override INSTEAD. 35. The PUBLIC and PROTECTED methods are always tagged as Virtual. Always Mark Public And Protected Methods As Virtual In A Non Sealed Class. 36. Unless interoperability involves interoperability, do not use unsafe code. Never Use Unsafe Code Unless WHEN Using Interop. 37. Avoid explicit type conversion. Use an AS algorithm to protective transition type. Avoid Explicit Casting. Use The as Operator To Defensively Cast To a Type. DOG DOG = New Germanshepherd (); Germanshepherd Shepherd = DOG As Germanshepherd; if (Shepherd! = NULL) {...} 38. Class members have entrusted: WITH DELEGATES AS CLASS MEMBERS: a) Copy the delegated to local variables before use to avoid concurrent conflicts. Copy a delegate to a local variable before publishing to Avoid Concurrency Race Condition. B) Never check whether the delegate is empty before calling. Always check a delegate for null before invoking it public class MySource. {Public event EventHandler MyEvent; public void FireEvent () {EventHandler temp = MyEvent; if (temp = null!) {Temp (this, EventArgs.Empty);}}} 39. Do not provide public event member variables, but use event accessors. Do not provide public event member variables Use event accessors instead public class MySource {MyDelegate m_SomeEvent; public event MyDelegate SomeEvent {add {m_SomeEvent = value;} remove {m_SomeEvent - = value;}}}.. 40. Use Programming .NET Components The eventshelper class defined is safely published in the event.

Use the eventshelper class defined in programming .Net Components to Publish Events Defensively. 41. Interface is always used. Always use interfaces. A) See Programming .NET Components, first and third chapters. See Chapter .Net Components. 42. The proportion of methods and attributes in the class and interfaces is at least 2: 1. Classes and interfaces Should Have at Least 2: 1 Ratio of Methods To Properties. 43. Avoid using a member's interface. Avoid Interfaces With One Member. 44. Try to have 3-5 members per interface. Strive to Have 3-5 MEMBERS Per interface. 45. Each interface does not have to exceed 20 members. No more Than 20 MEMBERS Per interface. A) 12 It may be the limit of the actual application. 12 IS probably a practical limit. 46. Avoid events as an interface member. Avoid Events As Interface Members. 47. Avoid using an abstract method, but use the interface instead. Avoid Abstract Methods, Use Interfaces INSTEAD. 48. Exposes the interface in the class level. Expose Interfaces On Class Hierarchies. A) See ProGramming .NET Components Chapter 3. See Chapter 3 in Programming .NET Components. 49. Priority uses a clear interface implementation. Prefer Using Explicit Interface Implementation. A) See Programming .NET Components Chapter 3. See Chapter 3 in Programming .NET Components. 50. Never assume that a type supports an interface. Protective inspection does not support the interface. .. Never assume a type supports an interface Defensively query for that interface SomeType obj1; IMyInterface obj2; / * Some code to initialize obj1, then: * / obj2 = obj1 as IMyInterface; if (! Obj2 = null) {obj2.Method1 ( );} Else {// handle error in expert interface} 51. The string of the user will be rendered never useless, but the use of resources. NEVER HARDCODE STRINGS THAT WILL BE PRESENTED TO End. 52. The string that may be modified will never use, such as a connection string.

NEVER HARDCODE STRINGS That Might Change Strings. 53. When building a long string, use StringBuilder, do not use String. When Building a long string, use stringbuilder, not string. 54. Avoid providing a method of providing a structure. Avoid Providing Methods On Structures. A) The parameterized constructor is encouraged. Parameterized. B) Optimist of Octa. Can Overload Operators. 55. When providing a static member variable, a static constructor is always provided. Always Provide a static constructor when providing static member variables. 56. As long as you can use the previous binding, you can use the later binding. Do Not Use Late-Binding Invocation When Early-Binding Is Possible. 57. Logs and traces for applications. Use Application Logging and Tracing. 58. Unless jumped in the Switch statement, do not use the GOTO statement. Never Use Goto Unless THROUGH. 59. Switch statement is always used to use Default for the addition. Always Have A Default Case In A Switch Statement That Asserts. Int Number = SomeMethod (); Switch (Number) {Case 1: Trace.writeline ("Case 1:"); Break; Case 2: Trace.Writeline ("Case 2 : "); Break; default: debug.assert (false); break;} 60. Unless another constructor is called in the constructor, it is not necessary to use this. Do not use the this reference unless invoking another constructor from within a constructor // Example of proper use of this public class MyClass {public MyClass (string message) {} public MyClass (): this ( "hello") {}}. 61 Do not use Base to access the base class. Unless conflict of the member name when the base class construct function is called.

Do not use the base word to access base class members unless you wish to resolve a conflict with a subclasses member of the same name or when invoking a base class constructor. // Example of proper use of Dan ase? 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 );}} 62. DISPOSE () and FINALIZE () methods are implemented according to Templates in Chapter 4 of Programming .NET Components. Implement Dispose () Methods Based On The Template In Chapter 4 of Programming .NET Components. 63. Avoid transition to system.object in the use of generic code, but use restrictions or AS operators. Avoid Casting to and from System.Object In Code That Uses generics. Use constraints or the as operator instead: class someclass {} // Avoid: Class myclass {

Void SomeMethod (t t)

{

Object temp = t;

SomeClass Obj = (SomeClass) TEMP;

}

}

// Correct Correct:

Class myclass

WHERE T: SomeClass

{

Void SomeMethod (t t)

{

SomeClass Obj = T;

}

}

64. The generic interface does not define a limit. The limitations of the interface layer can usually be replaced with a strong type.

DO NOT Define Constraints in Generic Interfaces. Interface Level-Constraint Can Offen Be Replaced by Strong-Typing.

Public Class Customer PUBLIC CLASS CUSTOMER

{...}

/ / Avoid Avoid:

Public interface ilist

Where t: Customer

{...}

// Correct Correct:

Public interface icustomerlist: ilist

{...}

65. Do not define the limitations associated with the method in the interface.

Do Not Defender Method-Specific Constraints in Interfaces.

66. The C # generic is always prioritized in the data structure.

Always prefer using c # generics in data structures.

(to be continued)

转载请注明原文地址:https://www.9cbs.com/read-6495.html

New Post(0)