IDesign C # Coding Specification (III)

Source: Internet
Author: User
Tags abstract constructor data structures goto implement inheritance variables
Code | Code cont. Second, idesign C # code specification three.

34. Avoid using the new inheritance modifier, but use override.
Avoid using the new inheritance qualifier. Use override instead.
35. For unsealed classes, the public and protected methods are always marked as virtual.
Always mark Public and protected methods as virtual in a non sealed class.
36. Never use unsafe code unless it involves interoperability.
Never use unsafe the code unless when using Interop.
37. Avoid explicit type conversions. Use the as algorithm to protect the type from being converted.
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. When a class member has a delegate:
With delegates as class members:
A the delegate is copied to a local variable before use to avoid concurrent conflicts.
Copy a delegate to a local variable before publishing to avoid concurrency race.
(b) Always 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 instead use event accessors.
Don't 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 the Eventshelper class defined in programming. NET components to publish events securely.
Use the Eventshelper class defined in programming. NET components to publish events defensively.
41. Always use an interface.
Always use interfaces.
A) See chapters I and III of programming. NET components.
Chapters 1 and 3 in programming. NET components.
42. The ratio of methods and properties in classes 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. Efforts to make each interface have 3-5 members.
Strive to have 3-5 members per interface.
45. No more than 20 members per interface.
No more than per interface.
A 12 may be the limit of actual application.
The is probably a practical limit.
46. Avoid the event as an interface member.
Avoid events as interface members.
47. Avoid using abstract methods instead of using interfaces instead.
Avoid abstract methods, use interfaces instead.
48. Exposes the interface in the class hierarchy.
Expose interfaces on class hierarchies.
a) See Programming. NET Components Chapter Iii.
Chapter 3 in programming. NET components.
49. Give priority to using explicit interface implementations.
Prefer using explicit interface implementation.
a) See Programming. NET Components Chapter Iii.
Chapter 3 in programming. NET components.
50. Never assume that a type supports an interface. Verify that the interface is supported in a defensive manner.
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 expected interface
}
51. The string that will be presented to the user is never hard-coded, but uses resources.
Never HardCode strings that would be presented to end users. Use the resources instead.
52. A string that may be modified at publication is never hard-coded, such as a connection string.
Never HardCode strings that might change based on deployment such as connection.
53. When building a long string, use StringBuilder instead of string.
When building a long string, use StringBuilder, not string.
54. Avoid providing a structured approach.
Avoid providing methods on structures.
A parameterized constructors are encouraged to use.
Parameterized constructors are encouraged.
b) The operator can be overloaded.
Can overload operators.
55. When providing static member variables, a static constructor is always provided.
Always provide a static constructor when providing the static member variables.
56. Do not use late binding as long as you can use early binding.
Do isn't use late-binding invocation when early-binding is possible.
57. Log and track the application.
Use application logging and tracing.
58. Never use a goto statement unless you jump in a switch statement.
Never use goto unless in a switch statement fall-through.
In the switch statement, default is always used to add assertions.
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. Do not use this unless you invoke another constructor in the constructor.
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 access the members of the base class using base unless you want to resolve the conflict of member names when invoking the base class constructor.
Do don't use the "base Word to access base class" Unless you wish to resolve a conflict with a subclasses member of th E same name or when invoking a base class constructor.
Example of proper use of 抌 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. Implement the Dispose () and Finalize () methods according to the templates in chapter Fourth of programming. NET components.
Implement Dispose () and Finalize () methods based on the template in Chapter 4 of programming. NET components.
63. Use generic code to avoid type conversions with System.Object instead of using a constraint or as operator.
Avoid casting to and from System.Object into code that uses generics. Use constraints or the AS operator instead:
Class SomeClass
{}
Avoid avoid:
Class MyClass
{
void SomeMethod (t)
{
Object temp = t;
SomeClass obj = (someclass) temp;
}
}
Correct correct:
Class MyClass where T:someclass
{
void SomeMethod (t)
{
SomeClass obj = t;
}
}
64. Generic interfaces do not define restrictions. The limitations of the interface layer can usually be replaced with a strong type.
Don't define constraints in generic interfaces. Interface Level-constraint can often is replaced by strong-typing.
public class Customer
{...}
Avoid avoid:
public interface IList where T:customer
{...}
Correct correct:
public interface Icustomerlist:ilist
{...}
65. Do not define method-related restrictions in the interface.
Don't define method-specific constraints in interfaces.
66. C # Generics are always preferred in data structures.
Always prefer using C # generics in data structures.



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.