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:
public class Employee
{
public Employee GetDetails()
{
//...
}
public double GetBonus()
{
//...
}
}
public class Employee
{
public void PrintDetails(int employeeId, String firstName)
{
int totalSalary = 2000;
// ...
}
}
// Correct
int employeeId;
string employeeName;
bool isActive;
// Avoid
Int32 employee_id;
String employee_name;
Boolean is_active;
// Correct
public interface IEmployee
{
}
public interface IShape
{
}
public interface IAnimal
{
}
// Avoid
public interface Employee
{
}
public interface Shape
{
}
public interface Animal
{
}
// Correct
class Employee
{
static void PrintDetails()
{
}
}
// Avoid
class Employee
{
static void PrintDetails()
{
}
}
using(var conn = new SqlConnection(connectionString))
{
// use the connection and the stream
using (var dr = cmd.ExecuteReader())
{
//
}
}
// Correct
String firstName = "Shubham";
Console.WriteLine(firstName);
//--------------------------
ExampleFunction();
AnotherFunction();
AnotherBusinessLogic();
// Avoid
String firstName = "Shubham";
ExampleFunction();
AnotherFunction();
AnotherBusinessLogic();
Console.WriteLine(firstName);
// Correct
private int employeeId { get; set; }
// Avoid
public int employeeId { get; set; }
// Correct
class Employee
{
private int employeeId { get; set; }
public void PrintDetails()
{
//------------
}
}
// Avoid
class Employee
{
private int employeeId { get; set; }
public void PrintDetails()
{
//------------
}
}
// 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;
// Correct
public enum Color
{
Red,
Green,
Blue,
Yellow,
Magenta,
Cyan
}
// Exception
[Flags]
public enum Dockings
{
None = 0,
Top = 1,
Right = 2,
Bottom = 4,
Left = 8
}
// Avoid
public enum Cars //Plural
{
Bugatti,
Ferrari,
Ford,
Audi,
BMW
}
Language element | Casing | Example |
---|---|---|
Namespace | Pascal | System.Drawing |
Type parameter | Pascal | TView |
Interface | Pascal | IBusinessService |
Class, struct | Pascal | AppDomain |
Enum | Pascal | ErrorLevel |
Enum member | Pascal | FatalError |
Resource key | Pascal | SaveButtonTooltipText |
Constant field | Pascal | MaximumItems |
Private static readonly field | Pascal | RedValue |
Private field | Camel | listItem |
Non-private field | Pascal | MainPanel |
Property | Pascal | BackColor |
Event | Pascal | Click |
Method | Pascal | ToString |
Local function | Pascal | FormatText |
Parameter | Camel | typeName |
Tuple element names | Pascal | (string First, string Last) name = ("John", "Doe"); var name = (First: "John", Last: "Doe"); (string First, string Last) GetName() => ("John", "Doe"); |
Variables declared using tuple syntax | Camel | (string first, string last) = ("John", "Doe"); var (first, last) = ("John", "Doe"); |
Local variable | Camel | listOfValues |
Note: in case of ambiguity, the rule higher in the table wins.
string displayName = $"{nameList[n].LastName}, {nameList[n].FirstName}";
var phrase = "lalalalalalalalalalalalalalalalalalalalalalalalalalalalalala";
var manyPhrases = new StringBuilder();
for (var i = 0; i < 10000; i++)
{
manyPhrases.Append(phrase);
}
//Console.WriteLine("tra" + manyPhrases);
public Form2()
{
// You can use a lambda expression to define an event handler.
this.Click += (s, e) =>
{
MessageBox.Show(
((MouseEventArgs)e).Location.ToString());
};
}
var localDistributors =
from customer in customers
join distributor in distributors on customer.City equals distributor.City
select new { Customer = customer, Distributor = distributor };