Skip to main content

Naming Conventions In C#.Net

For any developer, Naming Conventions is a best practice when working on any development project. Let’s discuss more about  Naming Conventions in this article.
Why Naming Conventions?
Naming Conventions are very important to identify the usage and purpose of a class or a method and to identify the type of variable and arguments.
Types of Naming Conventions 
Below are the two major parts of Naming Conventions.
  • Pascal Casing (PascalCasing)
  • Camel Casing (camelCasing)
Pascal Casing (PascalCasing)

Use PascalCasing for class names, method/function names, and constants or read-only variables. 
  1. //Use PascalCasing for Class Names  
  2. public class Products {  
  3.     //Use PascalCasing for Constant Variable or Read only Variables  
  4.     public  
  5.     const string ProductType = "General";  
  6.     //Use PascalCasing for Method Names  
  7.     public void GetProductDetails() {  
  8.         //Your logic here...  
  9.     }  
  10. }  
Camel Casing (camelCasing)
Use camelCasing for variable names and method arguments.
  1. public class Products {  
  2.     public  
  3.     const string ProductType = "General";  
  4.     //Use camelCasing for Method Arguments  
  5.     public void GetProduct(int productCategory) {  
  6.         //Use camelCasing for Variables Name  
  7.         int productCount;  
  8.         //Your logic here...  
  9.     }  
Below are the best practices to follow in your projects. Do not use Hungarian notation or any other type of identification. 
  1. //Use  
  2. string userName;  
  3. int counter;  
  4. //Avoid  
  5. string strUserName;  
  6. int iCounter; 
Prefix “I” letter for any Interface.
  1. public interface IProduct {  
  2.     //Interface logic here...  
Do not use underscores (_) in between words in variables.
  1. //Use  
  2. string userName;  
  3. string departmentName;  
  4. //Avoid  
  5. string user_Name;  
  6. string department_Name; 
Avoid using abbreviations for variable names.
  1. // Use  
  2. string departmentName;  
  3. string employeeName;  
  4. // Avoid  
  5. string deptName;  
  6. string empName; 
Use undersocre (_) as prefix for private static or global variables.
  1. public class Products {  
  2.     //User underscore for Private & Global Variables  
  3.     private static string _productGroup = "Packed";  
  4.     public List < Product > _products;  
  5.     public void GetProduct(int productCategory) {  
  6.         //Your logic here...  
  7.     }  
Hope this helps you with your projects.

Comments

Popular posts from this blog

Top 10 ASP.NET Web API Interview Questions

What is ASP.NET Web API? ASP.NET Web API is a framework that simplifies building HTTP services for broader range of clients (including browsers as well as mobile devices) on top of .NET Framework. Using ASP.NET Web API, we can create non-SOAP based services like plain XML or JSON strings, etc. with many other advantages including: Create resource-oriented services using the full features of HTTP Exposing services to a variety of clients easily like browsers or mobile devices, etc. What are the Advantages of Using ASP.NET Web API? Using ASP.NET Web API has a number of advantages, but core of the advantages are: It works the HTTP way using standard HTTP verbs like  GET ,  POST ,  PUT ,  DELETE , etc. for all CRUD operations Complete support for routing Response generated in JSON or XML format using  MediaTypeFormatter It has the ability to be hosted in IIS as well as self-host outside of IIS Supports Model binding and Validation Support for OD...

Extension methods in C#

Consider the class C# 1 2 3 4 5 6 7 8 9 10 11 12 13          namespace ExtensionMethod      {          public class testClass {              public string sayHello ( ) {              return "Hello" ;            }        }      }     Invoke the above from your form using C# 1 2 3 4 5 6          testClass test = new testClass ( ) ;      MessageBox . Show ( test . sayHello ( ) ) ;     This will show “Hello” in message box. Consider the scenario where you don...

What is cookie? Advantages and disadvantages of cookies?

What is cookie? A cookie is a small piece of text file stored on user's computer in the form of name-value pair. Cookies are used by websites to keep track of visitors e.g. to keep user information like username etc. If any web application using cookies, Server send cookies and client browser will store it. The browser then returns the cookie to the server at the next time the page is requested. The most common example of using a cookie is to store User information, User preferences, Password Remember Option etc.It is also one of the common and mostly asked interview questions. Some facts about Cookie Here are a few facts to know about cookies: · Cookies are domain specific i.e. a domain cannot read or write to a cookie created by another domain. This is done by the browser for security purpose. · Cookies are browser specific. Each browser stores the cookies in a different location. The cookies are browser specific and so a cookie created in one browser(e.g in Google Chrome...