This website utilizes cookies to enhance user experience. Kindly provide your consent for cookie usage. Accept
C# Coding Standard: Classes and Interfaces

01 August, 2023

READ TIME - 5 MINUTES

Thank you to our sponsors who help keep this blog post free for the reader:

This month's issue is proudly sponsored by Saddam Hossain .NET.

The website is owned by none other than Md. Saddam Hossain, who holds the esteemed title of Senior Software Engineer and is known for his unwavering passion for .NET development . To learn more about Md. Saddam Hossain, You are invited to visit the website for comprehensive details and insights.

Introduction:

I consistently strive to adhere to the highest coding standards and adopt a pragmatic approach in my software development endeavors. Presented below are the guidelines for classes and interfaces as per the C# coding standards that I rigorously apply in my projects. I keep all the classes' modifiers public intentionally, but it depends on the specific requirements.

Classes :

Model Name should be Singular.

Models :

                       public class Employee
                       {
                          ...
                          ...
                       }
                   

Repositories :

                       public class EmployeeRepository
                       {
                          ...
                          ...
                       }
                   

Services :

                       public class EmployeeService
                       {
                          ...
                          ...
                       }
                   

Interfaces :

Every Interface should be start with capital letter I

                       public interface IEmployeeService
                       {
                          ...
                          ...
                       }
                   

Controllers :

In a plural fashion, to reflect endpoints such as /api/employees to expose our logic via RESTful operations.

                       public class EmployeesController
                       {
                          ...
                          ...
                       }
                   

Fields :

A field is a variable of any type that is declared directly in a class or struct. Fields are members of their containing type.

Naming :

Class fields are named in a camel cased fashion.

                       public class EmployeesController
                       {
                          private readonly string EmployeeName;
                       }
                   

Referencing :

When referencing a private field, we can use the following approach.

                       public class EmployeesController
                       {
                            private readonly string _employeeName;

                            public EmployeesController(string employeeName)
                            {
                              _employeeName = employeeName;
                            }
                       }
                   

                        public class EmployeeRepository : IEmployeeRepository
                        {
                            private readonly ApplicationDbContext _context;
                            public EmployeeRepository(ApplicationDbContext context)
                            {
                                _context = context;
                            }
                        }
                   

Instantiations :
Input Params Aliases :

If the input variable names match the input aliases, then we can use them; otherwise, we must use the aliases, especially with values passed in.

                        int score = 95;
                        string name = "Saddam Hossain";

                        var employee = new Employee(name, score);
                   

Property Order :

When instantiating a class instance - make sure that our property assignment matches the properties order in the class declarations.

                        public class Employee
                        {
                            public Guid Id {get; set;}
                            public string Name {get; set;}
                        }

                        var employee = new Employee
                        {
                            Id = Guid.NewGuid(),
                            Name = "Rana"
                        }
                   

Alternatively, we can define it this way.

                        public class Employee
                        {
                            private readonly Guid _id;
                            private readonly string _name;

                            public Employee(Guid id, string name)
                            {
                                _id = id;
                                _name = name;
                            }
                        }

                        var employee = new Employee (_id: Guid.NewGuid(), _name: "Rana");
                   


About the Blogs

As a dedicated .NET developer, I maintain a Patreon account where I share exclusive content related to .NET development. There, you will also gain access to the codebase of this blog post. By becoming a Patreon member, you will have the opportunity to explore and learn from my projects firsthand.

If you have found my contributions helpful in any way, I kindly ask you to consider becoming a Patreon supporter. Your support enables me to continue producing high-quality content, empowering developers like yourself to enhance their skills and stay up to date with the latest developments in the .NET ecosystem. Thank you for considering joining my Patreon community!


Recent Posts

Confidently Build Production-Ready CRUD Using N-Layer Architecture

25 February, 2024

Confidently Build Production-Ready CRUD Using Clean Architecture

25 February, 2024


Share This Article On:
An error has occurred. This application may no longer respond until reloaded. Reload 🗙