Most projects these days incorporate products that are built using C#. So, if you’re a modern-day developer, it’s quite imperative that you learn C# and more importantly understand C# best practices that will help you sail through your projects and help you write some awesome code. Undertaking a C# course may be a great idea if you’re not familiar with C# or if your skills are a bit rusty. You’ll not only get the necessary skills to do good work, but you’ll also see different ways in which your skills could be applied!
Let’s look at some of the tips you could use to make your code more readable and fail proof:
- Use proper naming conventions
Using an appropriate naming convention throughout your code will help you maintain consistency, and it’ll be easier to scout out bugs. Some of the common conventions used by C# developers are:
- Try to avoid all uppercase or lowercase names for variables, method names or properties. It’s usually good practice to use only uppercase whenever declaring const variables.
- Don't use the same name from your .NET framework. Developers who are new to your code will have difficulty in understanding it quickly.
- Adding “Exception” as a suffix for your custom exception class will often give better visibility to your exception class, and this makes it easier to spot bugs during debugging.
2. Decide between reference types and value types
If all you desire is to store your data, you should prefer to use value types. On the other hand, when you want to create an instance of your type by defining the behavior type, using reference types would be more appropriate. Reference types can be polymorphic, whereas value types are not. Value types are better than reference types in terms of memory utilization and could help produce less fragmentation and garbage. Using a reference type variable will actually change the original value but the use of the value type variable will make a copy of the original variable and pass it across the method. Hence, it protects your value from accidental changes. Keep these differences in mind the next time you need to create a type.
3. Use properties instead of public variables—always!
There’s a simple reason behind this: It ensures proper encapsulation of the code in the OOPs environment. By using getters and setters, restricting the user from directly accessing the member variable becomes easy. Restricting the setting of the values explicitly could be done by you too, thus protecting your data from unintentional changes. Also, the properties get you validation for your data more quickly.
4. Don’t forget to use nullable data types
Sometimes, you’ll need to store null as the value of an integer, double or boolean variable. So how can you do this? Since the normal declaration just doesn't allow you to store the null as a value, C# has introduced the feature of nullable data types. It just needs a small change in your declaration—you would only need to use the “?” modifier, and you would have to place it just after the datatype. For example:
int index = 0; // this is a declaration of int
To convert this to a nullable data type, you would simply need to modify the declaration like this:
int? index = null; // and this, is a nullable data type declaration
5. Runtime constants over compile time constants
So, why would you want to prefer readonly over const variables? It’s because compile-time constants (const) have to be initialized right at the time of declaration and they can’t be altered later. They’re also limited to only strings and numbers. The IL simply replaces the const variable with its value for the entire block of code, and as a result, it’s often faster. On the other hand, a Runtime constant (readonly) is initialized in the constructor itself and can be altered at another initialization time. The IL only references the readonly variables and not the original values. Bottom line is, when you need faster processing speeds, use const to make the code run faster. But when you need reliable code, going for readonly variables is your best bet.
6. While casting, prefer “is” and “as” operators
You must use the “is” and “as” operators while casting. And instead of explicit casting, use the implicit casting. The problem with the “is” operator is that it sometimes returns null but does not show any exceptions.
A Seamless C# Development Experience
These tips are just the tip of the iceberg. There are lots of other things you could do to make sure that your C# development goes on track. For example, taking a C# certification would not only help you learn the fundamentals of C#, but it would also give you a glimpse of industry standards and the practices that most seasoned developers follow.
Usha Sunil is currently head of content at KnowledgeHut, a leading provider of new-age learning programs for workforce transformation in today’s fast-paced industries. With a keen eye on technology, innovation and design, she brings over 15 years of experience to the content marketing domain.