Here’s what PRO C# developers do to become super productive and avoid unnecessarily debugging codes
The biggest secret that has helped me throughout my career in becoming proficient in over a dozen of programming languages and frameworks is through the use of snippets and templates rather than typing everything from scratch.
Here are the technologies I’ve learned professionally throughout the years:
- PHP: CodeIgniter & Symfony
- HTML & CSS: including templating languages and SASS / Stylus
- Ruby on Rails
- C# & .NET WCF & Core
- Java EE
- Selenium & Appium
It’s essential for you to master the fundamentals of programming first such as variables, conditions, data structures etc… It’s also recommended to read and understand the official documentation of the technology you want to learn.
I spent lots of time surfing different websites such as StackOverflow just to get snippets of codes. This took me additional time and effort.
Whenever I need a piece of code:
- I simply open a single document,
- search for what I want
- copy and paste the structure of the codes
- Adapt them as needed
No more time-wasting and unnecessary headaches.
You can add the snippets directly in your IDE of choice. Or use autocompletion tools such as AutoHotKey.
Here’s a list of my most-used snippets.
1. How to read from a config file
var builder = new ConfigurationBuilder()
.AddJsonFile("config.json", optional: false);
IConfiguration config = builder.Build();
2. Read from yaml file
public static Dictionary<string, string> ReadYaml(string filePath)
var yaml = new Dictionary<string, string>();
var file = new FileInfo(filePath);
throw new FileNotFoundException("File not found", filePath);
var input = new StringReader(File.ReadAllText(filePath));
var yamlConfig = new YamlStream();
var mapping = (YamlMappingNode)yamlConfig.Documents.RootNode;
foreach (var entry in mapping.Children)
3. How to read a file by name?
Make sure you copy the file to output, otherwise the file won’t be detected in the build directory.
public string ReadFile(string fileName)
var path = Directory.GetCurrentDirectory();
var filePath = Path.Combine(path, fileName);
4. How to read args passed in parameters?
public static string ReadArgs()
var args = Environment.GetCommandLineArgs();
5. How to read Environment Variables?
public static string GetEnvironmentVariable(string name)
return Environment.GetEnvironmentVariable(name, EnvironmentVariableTarget.User);
6. How to take screenshot of screen?
How to take a screenshot using Selenium?
void TakeScreenshot(string fileName)
Screenshot ss = ((ITakesScreenshot)driver).GetScreenshot();
7. How to connect to a SQL Database?
public MySqlConnection connection;
public MySqlCommand command;
connection = new MySqlConnection("server=localhost;userid=root;password=root;database=test");
command = connection.CreateCommand();
8. How to connect to a MongoDB instance?
public MongoClient client;
public IMongoDatabase database;
public IMongoCollection<BsonDocument> collection;
client = new MongoClient("mongodb://localhost:27017");
database = client.GetDatabase("test");
collection = database.GetCollection<BsonDocument>("test");
9. How to send an email?
public class SmtpClientHelper
public static void SendEmail(string to, string subject, string body)
var smtpClient = new SmtpClient
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(Env.GetString("EMAIL_USERNAME"), Env.GetString("EMAIL_PASSWORD"))
using (var message = new MailMessage(Env.GetString("EMAIL_USERNAME"), to)
Subject = subject,
Body = body
10. How to generate a unique string?
Bonus: How to generate a random number between a custom range?
public static int RandomNumber(int min, int max)
Random random = new Random();
return random.Next(min, max);