C# is a general-purpose, modern and object-oriented programming language pronounced as “C Sharp”. It was developed by Microsoft led by Anders Hejlsberg and his team within the .NET initiative and was approved by the European Computer Manufacturers Association (ECMA) and International Standards Organization (ISO). C# is among the languages for Common Language Infrastructure. C# is a lot similar to Java syntactically and is easy for users who have knowledge of C, C++, or Java.
Below are some of the best practices which all the .Net Developers should follow:
1. Class and Method names should always be in Pascal Case
public class Employee
{
public Employee GetDetails()
{
//...
}
public double GetBonus()
{
//...
}
}
2. Method argument and Local variables should always be in Camel Case
public class Employee
{
public void PrintDetails(int employeeId, String firstName)
{
int totalSalary = 2000;
// ...
}
}
3. Avoid the use of underscore while naming identifiers
// Correct
public DateTime fromDate;
public String firstName;
// Avoid
public DateTime from_Date;
public String first_Name;
4. Avoid the use of System data types and prefer using the Predefined data types.
// Correct
int employeeId;
string employeeName;
bool isActive;
// Avoid
Int32 employeeId;
String employeeName;
Boolean isActive;
5. Always prefix an interface with letter I.
// Correct
public interface IEmployee
{
}
public interface IShape
{
}
public interface IAnimal
{
}
// Avoid
public interface Employee
{
}
public interface Shape
{
}
public interface Animal
{
}
6. For better code indentation and readability always align the curly braces vertically.
// Correct
class Employee
{
static void PrintDetails()
{
}
}
// Avoid
class Employee
{
static void PrintDetails()
{
}
}
7. Always use the using keyword when working with disposable types. It automatically disposes the object when program flow leaves the scope.
using(var conn = new SqlConnection(connectionString))
{
// use the connection and the stream
using (var dr = cmd.ExecuteReader())
{
//
}
}
8. Always declare the variables as close as possible to their use.
// Correct
String firstName = "Shubham";
Console.WriteLine(firstName);
//--------------------------
// Avoid
String firstName = "Shubham";
//--------------------------
//--------------------------
//--------------------------
Console.WriteLine(firstName);
9. Always declare the properties as private so as to achieve Encapsulation and ensure data hiding.
// Correct
private int employeeId { get; set; }
// Avoid
public int employeeId { get; set; }
10. Always separate the methods, different sections of program by one space.
// Correct
class Employee
{
private int employeeId { get; set; }
public void PrintDetails()
{
//------------
}
}
// Avoid
class Employee
{
private int employeeId { get; set; }
public void PrintDetails()
{
//------------
}
}
11. Constants should always be declared in UPPER_CASE.
// Correct
public const int MIN_AGE = 18;
public const int MAX_AGE = 60;
// Avoid
public const int Min_Age = 18;
public const int Max_Age = 60;
Similar Reads
C# Interview Questions and Answers
C# is the most popular general-purpose programming language and was developed by Microsoft in 2000, renowned for its robustness, flexibility, and extensive application range. It is simple and has an object-oriented programming concept that can be used for creating different types of applications. He
15+ min read
Getting started with C
C language is a popular programming language that was developed in 1970 by Dennis Ritchie at Bell Labs. The C programming language was developed primarily to build the UNIX operating system. It is widely used because it is simple, powerful, efficient, and portable. Features of C Programming Language
5 min read
String C/C++ Programs
C program to swap two StringsC Program to Sort an array of names or stringsC Program to Check if a Given String is PalindromeC/C++ Program for Return maximum occurring character in the input stringC/C++ Program for Remove all duplicates from the input string.C/C++ Program for Print all the duplicate
3 min read
C/C++ Programs
sArray C/C++ Programs C Program to find sum of elements in a given arrayC program to find largest element in an arrayRecursive C program to linearly search an element in a given arrayC program to multiply two matricesC/C++ Program for Given an array A[] and a number x, check for pair in A[] with sum
15+ min read
C Multiple Choice Questions
C is the most popular programming language developed by Dennis Ritchie at the Bell Laboratories in 1972 to develop the UNIX operating systems. It is a general-purpose and procedural programming language. It is faster than the languages like Java and Python. C is very versatile it can be used in both
4 min read
C Programs
To learn anything effectively, practicing and solving problems is essential. To help you master C programming, we have compiled over 100 C programming examples across various categories, including basic C programs, Fibonacci series, strings, arrays, base conversions, pattern printing, pointers, and
8 min read
C Library Functions
The Standard Function Library in C is a huge library of sub-libraries, each of which contains the code for several functions. In order to make use of these libraries, link each library in the broader library through the use of header files. The actual definitions of these functions are stored in sep
10 min read
C File Handling Programs
C Program to list all files and sub-directories in a directory C Program to count number of lines in a file C Program to print contents of file C Program to copy contents of one file to another file C Program to merge contents of two files into a third file C program to delete a file
1 min read
C/C++ Program for String Search
C/C++ Program for Naive Pattern SearchingC/C++ Program for KMP AlgorithmC/C++ Program for Rabin-Karp AlgorithmC/C++ Program for A Naive Pattern Searching QuestionC/C++ Program for Finite AutomataC/C++ Program for Efficient Construction of Finite AutomataC/C++ Program for Boyer Moore Algorithm â Bad
1 min read
Misc C Programs
C Program to print environment variablesC Program to Swap two NumbersC program swap two numbers without using a temporary variableC Program to check if a given year is leap yearC Program to sum the digits of a given number in single statement?C program to print numbers from 1 to 100 without using lo
1 min read