Knowledge Share

Knowledge is NOT Power IMPLEMENTATION of knowledge is Power!!!
HomePortalGalleryCalendarFAQRegisterUsergroupsLog in

Share | 

 Coding Conventions in .net

View previous topic View next topic Go down 

Posts : 141
Points : 407
Reputation : 0
Join date : 2007-12-29
Location : Chennai

PostSubject: Coding Conventions in .net   Mon Dec 31, 2007 12:09 pm

You can correct the problem in the preceding example by making the badChars collection readonly (ReadOnly in Visual Basic). Alternately, you can clone the badChars collection before returning. The following example demonstrates how to modify the GetInvalidPathChars method to return a clone of the badChars collection.
[Visual Basic]
Public Shared Function GetInvalidPathChars() As Char()
Return CType(badChars.Clone(), Char())
End Function
public static char[] GetInvalidPathChars()
return (char[])badChars.Clone();
Do not use readonly (ReadOnly in Visual Basic) fields of arrays. If you do, the array is readonly and cannot be changed, but the elements in the array can be changed. The following example demonstrates how the elements of the readonly array InvalidPathChars can be changed.
public sealed class Path
private Path(){}
public static readonly char[] InvalidPathChars = {'\"', '<', '>','|'}'
//The following code can be used to change the values in the array.
Path.InvalidPathChars[0] = 'A';
Using Indexed Properties in Collections
You should use an indexed property only as a default member of a collection class or interface. Do not create families of functions in noncollection types. A pattern of methods, such as Add, Item, and Count, signal that the type should be a collection.
Array Valued Properties
You should use collections to avoid code inefficiencies. In the following code example, each call to the myObj property creates a copy of the array. As a result, 2n+1 copies of the array will be created in the following loop.
[Visual Basic]
Dim i As Integer
For i = 0 To obj.myObj.Count - 1
Next i
for (int i = 0; i < obj.myObj.Count; i++)

Returning Empty Arrays
String and Array properties should never return a null reference. Null can be difficult to understand in this context. For example, a user might assume that the following code will work.
[Visual Basic]
Public Sub DoSomething()
Dim s As String = SomeOtherFunc()
If s.Length > 0 Then
' Do something else.
End If
End Sub
public void DoSomething()
string s = SomeOtherFunc();
if (s.Length > 0)
// Do something else.
The general rule is that null, empty string (""), and empty (0 item) arrays should be treated the same way. Return an empty array instead of a null reference.
Guidelines for Implementing Equals and the Equality Operator (==)
The following rules outline the guidelines for implementing the Equals method and the equality operator (==):
Implement the GetHashCode method whenever you implement the Equals method. This keeps Equals and GetHashCode synchronized.
Override the Equals method whenever you implement ==, and make them do the same thing. This allows infrastructure code such as Hashtable and ArrayList, which use the Equals method, to behave the same way as user code written using ==.
Override the Equals method any time you implement the IComparable Interface.
You should consider implementing operator overloading for the equality (==), not equal (!=), less than (<), and greater than (>) operators when you implement IComparable.
Do not throw exceptions from the Equals or GetHashCode methods or the equality operator (==).
For related information on the Equals method, see Implementing the Equals Method.
Implementing the Equality Operator (==) on Value Types
In most programming languages there is no default implementation of the equality operator (==) for value types. Therefore, you should overload == any time equality is meaningful.
You should consider implementing the Equals method on value types because the default implementation on System.ValueType will not perform as well as your custom implementation.
Implement == any time you override the Equals method.
Implementing the Equality Operator (==) on Reference Types
Most languages do provide a default implementation of the equality operator (==) for reference types. Therefore, you should use care when implementing == on reference types. Most reference types, even those that implement the Equals method, should not override ==.
Override == if your type is a base type such as a Point, String, BigNumber, and so on. Any time you consider overloading the addition (+) and subtraction (-) operators, you also should consider overloading ==.
Back to top Go down
Coding Conventions in .net
View previous topic View next topic Back to top 
Page 1 of 1
 Similar topics
» open every link in a new tab?
» Banner inquiry
» XHTML - Lesson 5/5
» Pagination
» Are emails dying?

Permissions in this forum:You cannot reply to topics in this forum
Knowledge Share :: .Net-
Jump to: