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
How to Compare Strings in C#?
A string is a collection of characters and is used to store plain text. Unlike C or C++, a string in C# is not terminated with a null character. The maximum size of a string object depends on the internal architecture of the system. A variable declared followed by "string" is actually an object of s
13 min read
How C# Code Gets Compiled and Executed?
C# is a general-purpose, strongly typed, lexically scoped, functional, object-oriented, and component-oriented programming language. In this article, we are going to learn how C# code gets compiled and executed. Step-by-step process of C# code compilation: Step 1: Write a C# code. Step 2: Compile th
4 min read
C# Tutorial
C# (pronounced "C-sharp") is a modern, versatile, object-oriented programming language developed by Microsoft in 2000 that runs on the .NET Framework. Whether you're creating Windows applications, diving into Unity game development, or working on enterprise solutions, C# is one of the top choices fo
4 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
Concatenating Two Strings in C
Concatenating two strings means appending one string at the end of another string. In this article, we will learn how to concatenate two strings in C. The most straightforward method to concatenate two strings is by using strcat() function. Let's take a look at an example: [GFGTABS] C #include <s
3 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
Convert String to int in C
In C, we cannot directly perform numeric operations on a string representing a numeric value. We first need to convert the string to the integer type. In this article, we will discuss different ways to convert the numeric string to integer in C language. Example: Input: "1234"Output: 1234Explanation
6 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 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 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