Avoiding Common C# Coding Mistakes: Do’s and Don’ts to Remember
Published in
6 min readJul 9, 2023
C# is a widely used programming language that has gained popularity for its simplicity, efficiency, and versatility. However, even experienced programmers can fall into common pitfalls that lead to poorly written code. To help you avoid these mistakes and write better code in C#, we have compiled a list of do’s and don’ts.
Do’s:
- Use meaningful variable and method names: The names of variables and methods should clearly indicate their purpose. This not only makes it easier for you to understand your own code but also helps other developers who may need to work with your code in the future.
using System;
public class FactorialCalculator
{
public static int CalculateFactorial(int number)
{
if (number == 0)
{
// Base case: factorial of 0 is 1.
return 1;
}
else
{
// Recursive case: factorial of number is number multiplied by factorial of (number - 1).
return number * CalculateFactorial(number - 1);
}
}
public static void Main(string[] args)
{
int inputNumber = 5;
int factorialResult = CalculateFactorial(inputNumber);
Console.WriteLine($"Factorial of {inputNumber} is {factorialResult}");
}
}