asp.net Naming Conventions Best Practices
Tags | c# naming conventions pdf,c# variable naming conventions,microsoft coding standards best practices,c# naming conventions private fields, c# naming conventions private variables, c# coding standards and best programming practices, c# namespace naming best practice
Asp.net Naming Conventions Best Practices
Objective:
There is always we have need to do best naming conventions and follow coding standard.As per this requiremetns I decided to write this article especially focusing on dot net basic beginners and also developers.Standards and Conventions
The name of any new form, class or interface should follow the "Hungarian" notation convention with no prefixes. For example, a form, class, or interface could have the nameWe will use the following three conventions for capitalizing identifiers.
Pascal case
The first letter in the identifier and the first letter of each subsequent concatenated word are capitalized. You can use Pascal case for identifiers of three or more characters.
Camel case
The first letter of an identifier is lowercase and the first letter of each subsequent concatenated word is capitalized.
Uppercase
All letters in the identifier are capitalized. Use this convention only for identifiers that consist of two or fewer letters. For example:
System.IO;
System.Web.UI;
The following table lists the various code objects that require names and, where naming conventions apply, describes the naming conventions.
Identifier | Case | Example |
Class | Pascal | OrderDetails |
Enum type | Pascal | ErrorLevel |
Enum values | Pascal | FatalError |
Event | Pascal | ValueChange |
Exception class | Pascal | WebException Note: Always ends with the suffix Exception. |
WebExceptionNote: Always ends with the suffix Exception. |
ReleaseType |
IDisposable Note: Always begins with the prefix I. |
UserAccountInfo |
System.Drawing |
empName |
ReleaseType |
releaseType |
_activeDBConnection Note: Class level Private variables should start with _ and then Camel case naming convention. |
ReleaseType |
strFullMessage |
Namespaces
All namespace should use Pascal casing and be prefixed with your company and module. For example: UM. Emerge.DALThe general rule for naming namespaces is to use the company name followed by Product Name and the feature as follows.
For example:
CompanyName.Product.Feature
Example of using Namespace:
C#
namespace N1.N2
{
Class A
{
}}
- Prefixing namespace names with a company name or other well-established brand avoids the possibility of two published namespaces having the same name. For example Microsoft.Office is an appropriate prefix for the Office Automation Classes provided by Microsoft.
- Do not use the same name for the namespace and class. For example, do not provide both a Debug namespace and Debug class.
Base Class and Sealed Class
A class can be abstract or sealed. An abstract class requires a derive class to provide an implementation. A sealed class does not allow derived class. It is recommended that uses classes over other types.. Base class can provide default set of functionality, while allowing customization though extension.Example : Use of Sealed Class
C#
Public sealed class Runtime
{
//private constructor prevent the class from being created
private Runtime();
//static method
Public static string GetCommandLine()
{
//implementation code goes here
}
}
Value Type
A value type describes a value that is represented as a sequence of bits stored on the stack. There is two value type structure (struct) and enumeration (enum).Struct
- Act like primitive type.
- Have an instance size under 16 bytes
- Are immutable
- Value semantic are desirable
Enum
The enumeration(Enum) value type inherits from Enum Class. An enum is a named constant whose underlying type is an integer type except char. Use an enum a strongly type parameter, properties and return type. Always define enumerated values using an enum if they are used parameter or property.
- Use Pascal case for Enum types and value names.
- Use abbreviation sparingly.
- Do not use Enum suffix on Enum type names.
- Use singular name for most Enum type , but use a plural name for Enum type that are bit field.
Delegate
A delegate is a powerful tool that allows the manage code object model designer to encapsulate method calls. Delegate is useful for event notifications and callback function.Convention
Name end Delegate function with a suffix Delegate.
C#
Public delegate void SimpleDelegate()
Void F()
{
System.Console.WriteLine(“Test F”);
}
Main()
{
SimpleDelegate d= New SimpleDelegate(F);
d();
}
Public void MultiCall (SimpleDelegate d, int count)
{
int I;
for(i=0;i
{
d()
}
}
Attribute
The .Net framework enable developer to invent new kind of declarative information, to specify declarative information for various program entities and to retrieve attribute information in a runtime environment. For example, a framework might define a HelpAttribute attribute that can be placed on a program element such as classes and methods to provide a mapping from program element to their documentation.
- Add the attribute suffix to custom attribute classes, as shown in the following example
public class ObsoleteAttribute {}
Nested Type
A nested type is a type defined within the scope of another type. Nested types are very useful for encapsulating implementation details of a type, such as enumerator over a collection, because they can have access to private state.
- The following example illustrates how to define type with and without nested types.
ListBox.SelectedObjectCollection
//without nested types
ListBoxSelectedObjectCollection
Class Members
Property Naming
- The following rules outline the naming guideline for properties:
- Use noun or noun phrase to name properties.
- Use Pascal case
- Do not use Hungarian notation
public readonly string UserName
{
get
{
return UserName;
}
}
public int Age
get
{
return Age;
}
Indexed property
public class SampleClass
{
public Color BlackColor
{
//Code for Get and Set accessors goes here
}
}
Event Naming
- Use an EventHandler suffix on event handler name.
- Name an event argument class with the EventArgs suffix.
- Consider naming events with a verb.
- Use a gerund to create an event name that expresses the concept of pre-event, and a past-tense verb to represent the post event.
- Do not use a prefix and suffix on the event declaration on the type. For example use Close instead of OnClose.
{
int x;
int y;
public MouseEventArgs(int x, int y)
{
this.x=x;
this.y=y;
}
Public int X{get{return x;}}
Public int Y{get{return y;}}
}
Method naming
- Use verb and verb phrases to name methods.
- Use Pascal case.
RemoveAll()
GetCharArray()
Invoke()
Method Overloading
Method overloading occurs when a class contains two methods with the same name but different signature.
- Use method overloading instead of allowing default arguments.
- Use method overloading to provide different methods that do same thing.
- Use noun, noun phrases or abbreviation of noun to name static field.
- Use Pascal case.
- Use Hungarian notation prefix on static field name.
- It is recommended that you use static properties instead of public static field whenever possible.
- Use descriptive parameter name.
- Use camel case.
- Do not use reserved parameter.
- Do not use prefix parameter name with Hungarian type Notation.
Interface
An interfaces is an entity which behaves much like a class with the exception that- It cannot implement the constructor.
- It cannot implement the destructor.
- It cannot define the variables.
- It can only define the prototype of the function.
Array
Array vs. collectionUse collection in a following situation:
- When Add, Remove and other methods for manipulating the collection are supported.
- To add read only wrappers around internal arrays.
- You should use collections to avoid code efficiencies. In the following example each call to the myObj property creates a copy of the array.
DoSomething(obj.myObj[i]);
- Array and string property should not return a null reference. the general rule is that null. empty string(“ ”), and empty(0 item) array should be treated as the same way. Return an empty array instead of the null reference.
Operator
The following rules guide for operator overloading:- Define operator of value type that are logical build in language type, such as System. Decimal structure.
- Provide operator overloading method only in the class in which methods are defined.
- Overload operator in a symmetric manner. For example if you overload the equality operator(==), you should also overload the non equality operator(!=).
- Use operator overloading in cases where it is immediately obvious what the result of the operation will be. For example. It makes sense to be able to subtract one Time value from another Time value and get a TimeSpan.
TimeSpan operator-(Time t1, Time t2){}
TimeSpan difference(Time t1, Time t2){}
}
Commenting Code
There are two general styles of comments:- Line comments: Where the comment starts at some point on a line and goes to the end of the line.
- Block comments: Where the comment starts at some point and ends at another point, possibly many lines further along.
While writing code in C# the first comment should start with
#region Method Name
and should end with
#endregion
The method and class should have sufficient comments inside for future reference, like what the method is, param list and return type etc.
For example:
////// Gets a list with Address objects for the requested contact person.
///
///The ID of the ContactPerson for whom
the addresses should be returned.
/// A list with address objects when the database contains
addresses for the contact person, or an empty list
otherwise.
public static AddressList GetList(int contactPersonId)
{
///rest of the code here…
}
- Every Code block should have a Try, Catch, Finally block.
- Connection and Resources should be released when not in use.
///**********************************************************
///System :
///Class Name :
///Version :
///Purpose :
///Created By :
///Creation Date :
///Revision Version :
///Revision History : - < dd MMM YYYY > -
< Modified Purpose >
///**********************************************************
Purpose:
For more details about handler you can take reference of MSDN.
Go for Client side Multiple parameter refer my post:
asp.net Naming Conventions Best Practices
Reviewed by Vikas Kumar Singh
on
January 11, 2018
Rating:
Nice article
ReplyDeleteDOT NET training in Marathahalli
dot net training institute in Marathahalli
dot net course in Marathahalli
best dot net training institute in Marathahalli
This comment has been removed by the author.
ReplyDelete