The C# programming language ranks amongst the top 5 software development languages.
Microsoft’s object-orientated programming technology encompasses static and strong typing. It fits within the .NET framework which developers use to create Windows apps, mobile apps, and games.
This article offers 5 C# programming tips to help you become a better developer.
Read on to learn the benefits of a cheat sheet, how to throw exceptions the right way, and convert your data tables into CSV files. You’ll discover the best codes of practice and will increase your C# skillset.
1. Use StringBuilder Instead of Concatenation
Visual Studio will scold you if you concatenate your strings with the + symbol while in a loop. The preferred method is to use the StringBuilder object. For example:
StringBuilder sb = new StringBuilder();
for (int i = 0; i < stringArray.Length; ++i){
sb.Append(stringArray[i]);
}
string s = sb.ToString();
By instantiating a StringBuilder you get C# to do the hard work for you. It acts like an array and the ToString() method joins the elements together in one line of code.
2. Print a C# Programming Language Cheat Sheet
Most students use a cheat sheet when cramming for an exam.
The sheet lists all the important points and helpful functions on one page. Printing it off and attaching it to the wall is the perfect way for a quick reference.
Download a range of excellent cheat sheets on cheatography.com.
The site offers help for C# from beginner level to advanced. You can also browse other programming languages like Java and C++.
3. Convert Datatables to CSV the Easy Way
Ever wanted to convert a C# datatable to CSV?
The DataTable class gives the ability to store data in columns and rows in a grid format. It’s part of C# ADO.NET and can bind to DataGridView controls for easy visual access.
A CSV or comma-separated values file is a plain text file used to store data in a spreadsheet format.
Use a third-party library to convert between the two. Simply install the package using NuGet, attach the library, and add the method calls.
4. Rethrowing Exceptions
Exceptions are the bane of a developer’s life. Don’t make things harder by explicitly throwing an exception. Call throw and not throw exc as the stack trace will reset. For example:
try {}
catch(ExceptionTypeA exc)
{ throw exc; // Stacktrace is reset }
catch(ExceptionTypeB exc)
{ throw; // Retains stacktrace}
The second method enables you to check the stack trace if an error occurs while the first will drive you crazy!
5. Don’t Comment Out Old Code
Precious hours spent on writing code are difficult to let go of when you refactor. Most developers don’t have the heart to delete dozens of lines and comment them out instead.
Don’t keep old code. Delete it.
As long as you have a backup saved in your repository you can remove outdated lines. This will make your code easier to read and won’t bloat the production file.
More Help Coding in C#
The C# programming language is a powerful yet complex medium for building applications.
Unlike JIT languages, programming in C# requires knowledge of memory allocation and optimization. Increase your knowledge by reading more C# coding tutorials and tips to improve your apps.
Read more articles on our blog to learn C# programming and develop your skillset.