Coding convention guidelines need to follow in C# code.

Jitendrasoft09
Posted by in C# category on for Advance level | Points: 250 | Views : 2187 red flag
Rating: 5 out of 5  
 1 vote(s)

This article helps to understand the coding convention guidelines in C#.


 Download source code for Coding convention guidelines need to follow in C# code.

Introduction.


In daily life, most of the developer follows the rules of coding guideline in C#.  It is an important that every developer must write the code which code is understandable by any other developers.  Probably all client expects good programming with well maintained formatted code.  Our job should not be to write the code only without understanding the behavior of the code structure. So I would like to suggest to all the developers to write code clean and clear. There are some free tools like style cop which suggests writing the code under the Microsoft rule of C# coding standards.  


Types of Coding Guidelines.


We have a two ways to write the code with following approaches-

Camel case – This case starts with small letter. Probably we follow the camel case for declaring parameters, variables etc in C# code.

Example: custName

Pascal case – This case starts with Capital letter. Probably we follow the Pascal case for declaring class, interface and property etc in C# code.

Example : CustomerName


Coding Guidelines Approaches.


In below, we tried to explain how to write the code with proper naming convention in C#. We have discussed below for declaring variables, properties, classes and interfaces with examples. 

Variable declarations-  

We use camel case most of the time declaring private variables. We can approach to use Pascal for static public variables. At the time of declaring variables we can decide to implement modifiers as per our requirement. The below code shows the correct and wrong naming conventions.

Correct Naming conventions for variables-

We use below camel case. The name should not be large and complex to read. We must initialize the value with empty string. If we use public variable we can use pascal case. But it is depend how this variables will be used in the code.

 private string correspndenceAddress = string.Empty;
        protected string mailAddress = string.Empty;
        internal string officeAddress = string.Empty;
        protected internal string appointmentAddress = string.Empty;

Wrong Naming conventions for variables-

We are seeing below code which is written wrongly. We can initialize the double quote “” but as per coding standards we have to recommend the string.Empty in C# code.

        private string CorrespndenceAddress = "";
        protected string MailAddress = "";
        internal string OfficeAddress = "";
        protected internal string AppointmentAddress = "";	
        private string CorrespndenceAddress;
        protected string MailAddress;
        internal string OfficeAddress;
        protected internal string AppointmentAddress; 
Correct Naming conventions for variables-

We use below camel case. The name should not be large and complex to read. We must initialize the value with empty string. If we use public variable we can use pascal case. But it is depend how this variables will be used in the code.  

  // Declararion of variables.
            private int? custId = null;
            private string custName = string.Empty;
            private double? custSalary = null;
            private float? custGrossSalary = null;
            private char? custStartingChar = null;
            private object custDepartment = null;
            private StringBuilder custNotification = null;

Wrong naming conventions for variables-

We are seeing below code which is written wrongly. We can initialize the double quote “” but as per coding standards we have to recommend the string.Empty. We cannot assign null if variable is not defined nullable.  

  // Declararion of variables.
            private int? CustId = null;
            private string CustName = “”;
            private DOUBLE CustSalary = null;
            private float? CustGrossSalary = null;
            private char CustStartingChar = null;
            private object CustDepartment = null;
            private StringBuilder CustNotification = null;


Class declarations-

We use always Pascal case for declaring classes. At the time of declaring classes we can decide to implement modifiers as per our requirement. The below code shows the correct and wrong naming conventions.

Correct Naming conventions for class.

We use below Pascal case. The name should not be large and complex to read. Class could define public. Actually any class would be by default internal. We can avoid to use internal in the class.

public class Engineer
    {
        // code.
    }
    class Doctor
    {
        // code.
    }

Wrong Naming conventions for class.-

We are seeing below code which is written wrongly. The class name should not start from small letter. We should not follow camel case.

  public class engineer
    {
        // code.
    }
    internal class doctor
    {
        // code.
    }


Interface Declarations-

We use always Pascal case for declaring interfaces. At the time of declaring interfaces we can decide to implement modifiers as per our requirement. The below code shows the correct and wrong naming conventions.

Correct Naming conventions for interface-

We use Pascal case in interface. The name should not be large and complex to read. Interface name should start from capital letter ‘I’ and to use the Pascal case.

public interface IEngineer
    {
        // code.
    }
    internal interface IDoctor
    {
        // code.
    }

Wrong naming conventions for interface-

We are seeing below code which is written wrongly. The interface name should start from ‘I’ letter. We should not start with Inter keyword in interface declarations.

   public interface InterEngineer
    {
        // code.
    }
    internal interface InterDoctor
    {
        // code.
    }


Property declarations-

We use always Pascal case for declaring properties. At the time of declaring properties we can decide to use modifiers as per our requirement in C# code.  We can use _(underscore) to declare private variables to get and set the value for property. The below code shows the correct and wrong naming conventions.

Correct Naming conventions for property-

We use Pascal case in property. The name should not be large and complex to read. We should use the Pascal case.

public int Salary { get; set; }
        public string Doctor { get; set; }
        public double PropertyRate { get; set; }
        public float HouseRate { get; set; }
        public char NameStarts { get; set; }

private int _salary;
        private string _customerName;
        private double _propertyRate;
 
        public int Salary
        {
            get
            {
                return _salary;
            }
            set
            {
                _salary = value;
            }
        }
        public string CustomerName
        {
            get
            {
                return _customerName;
            }
            set
            {
                _customerName = value;
            }
        }
        public int Salary
        {
            get
            {
                return _salary;
            }
            set
            {
                _salary = value;
            }
        }

Wrong Naming conventions for property-

We are seeing below code which is written wrongly. We should not use Pascal case in property.

 public string doctor { get; set; }
        public double propertyRate { get; set; }
        public float houseRate { get; set; }
        public char nameStarts { get; set; }

    public string _doctor { get; set; }
        public double _propertyRate { get; set; }
        public float _houseRate { get; set; }
        public char _nameStarts { get; set; }

 

C# coding Guidelines demo code-

In the below code, we are seeing how class, method and variables are well structured wih naming convention in the code.  If some another developer reviews the code, they can understand the code easily and behavior of the code structure.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace CodingConvention
{
 
    /// 
    /// Interaction with customer.
    /// 
    class Program
    {
        // Creation of class instance.
        static void Main(string[] args)
        {
            // Object creation of customer.
            Customer customer = new Customer();
            customer.GetCustomerDetails();
            Console.ReadLine();
 
        }
        #region Customer
        /// 
        /// Creation of customer class.
        /// 
        public class Customer
        {
            // Declararion of variables.
            private int? custId = null;
            private string custName = string.Empty;
            private double? custSalary = null;
            private float? custGrossSalary = null;
            private char? custStartingChar = null;
            private object custDepartment = null;
            private StringBuilder custNotification = null;
 
            // To get customer details.
            public void GetCustomerDetails()
            {
                // Assign customer values.
                this.custId = 1001;
                this.custName = "Jitendra";
                this.custSalary = 1000001;
                this.custGrossSalary = 1000001.00f;
                this.custStartingChar = 'J';
                this.custDepartment = "Information Technology";
                this.custNotification = new StringBuilder();
                this.custNotification.Append("Office remains closed on Friday");
 
                // To display customer details.
                Console.WriteLine("Customer Id is : " + this.custId);
                Console.WriteLine("Customer Name is : " + this.custName);
                Console.WriteLine("Customer salary is : "  +  this.custSalary);
                Console.WriteLine("Customer gross salary is : " + this.custGrossSalary);
                Console.WriteLine("Customer starting character name is : " + this.custStartingChar);
                Console.WriteLine("Customer department is : " + this.custDepartment);
                Console.WriteLine("Customer notification is : " + this.custNotification);
 
            }
        }
        #endregion
 
    }
}

 

Output Screen:

Let’s see below output of the code. We are easily to understand that this is customer information. All the variables are declared with customer information in the code.


Conclusion


We have discussed how we should follow the rule of coding standards in C#.  I have attached the code convention demo class for coding standards in C# for better understanding practically. Thanks for the reading.


Page copy protected against web site content infringement by Copyscape

About the Author

Jitendrasoft09
Full Name: Jitendra Kumar
Member Level:
Member Status: Member,MVP
Member Since: 3/26/2013 2:40:53 AM
Country: India
Jitendra Kumar If my post helps you, plz mark as an answer.
http://www.dotnetfunda.com/
Jitendra Kumar

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)