Software development is a term used to describe creating software by development. Usually, it involves a programming language like C#, Java, Python, or others. It also describes other processes that need to be done. Processes like testing, documenting, designing, bug fixing, and more.
Software development
Like most C# developers I use Visual Studio. One of the things I love about this IDE is that it contains a lot of shortcuts and key combinations, which make it easier and faster to finish your code. In this blog, I want to tell you about my favorite and most used Visual Studio shortcuts and key combinations. That doesn’t mean you need to use them, but I hope some might sound helpful to you.
Yes, more and more people are using Visual Studio Code, but I prefer Visual Studio since its sole purpose is C# and other Microsoft languages, frameworks, and techniques. Visual Studio Code is more of an I-know-a-bit-of-everything type of application. But this is my personal opinion.
Keyboard Setting
The Visual Studio shortcuts you can use and how to use them depend on your Visual Studio keyboard settings. There are different keyboard mappings. I use Visual C# 2005. Some key (combinations) are the same in different mappings, but some differences exist. Visual C# 2006 uses F6 to build the solution, and another mapping uses CTRL + B.
To change the keyboard mapping, open Visual Studio and access Tools -> Options. In the category Environment find Keyboard. Change the scheme to Visual C# 2005 and press the button Reset.

This is also where you can change some mappings to your liking. I would not recommend that. If you work in different locations with different accounts, you have different key mappings everywhere. You can keep changing them, but it could get a nuisance.
But, if you use an account with Visual Studio, your settings are stored in the cloud. These settings are downloaded when you log into a different Visual Studio.
F5 – Run
The most used key I use in Visual Studio is the F5 key. This one runs the current startup application. Sure, you could move your mouse to the run button, but pressing a key on your keyboard is way quicker.
If Visual Studio is running your application and you want to stop it, press SHIFT + F5. This combination will stop the debugger. If you want to run your application without debugging, press CTRL + F5.
Or press the green play arrow at the top of Visual Studio.
F6 – Build Solution
The F6 key builds the solution. So each project that has been changed will be (re)built and compiled to the debug DLL, EXE, or whatever type. It’s a good way to see if there are any compile errors. Especially when you just changed code in different files.
If you only want to build the solution you are currently focused on (the file you are working on), then press SHIFT + F6.
Or go to the menu item Build and select Build Solution. Or right-click on the solution in the Solution Explorer and select Build Solution.
CTRL + . – Quick Actions and Refactorings
The most helpful key combo that has ever been invented in Visual Studio: Quick Actions and Refactorings. When you see that annoying red line under some code you know there is something wrong. While you have two hands on your keyboard, move the cursor to the error with the arrow keys and press CTRL + . (dot). This will show Quick Actions and Refactorings and it gives suggestions on how to continue.
It can help by fixing problems, refactoring code, installing NuGet packages if needed, clean up code… A lot.
Or right-click with your mouse on the error, problem, whatever, and select Quick Actions and Refactorings.
CTRL + SPACE – IntelliSense
While writing code, you can use the CTRL + SPACE to call IntelliSense. A great little tool that helps you discover methods and classes, finish pieces of code (non-AI), or fix typos.
Or if you just type behind a property or variable and you lost the IntelliSense by pressing ESC, you can recall it by using the CTRL + SPACE.
CTRL + Q – Quick Search
I personally love the search option in applications. I remember the days when I had to scroll through a lot of menu items to find that one item, which was usually located in the first menu I already looked at. This option usually helps you discover and find the item you are looking for.
Visual Studio has this option too. If you press CTRL + Q you will see a popup window with two options: Code search and feature search. While the first one is self-explanatory, the second needs some more info.
The feature search can find any feature inside Visual Studio. An example; I always turn on the Active tracking, which means that when I click on an open file in the editor, that file gets highlighted in the Solution Explorer. The Active tracking is located in the Options window -> Projects and Solutions -> General. But if you use the feature search you have found it pretty quick.

Or click on the Search button on top of Visual Studio, right of the Help menu item.
F9 – Add/Remove Breakpoint
Debugging is important. So it’s a good thing it’s even easier to place breakpoints in code. If you are working on a line that needs a breakpoint, press F9, then F5, and hit that breakpoint. If you want to remove the breakpoint, focus on the same line and press F9 again.
Or click on the gray, vertical bar left of the line numbers. You will see the red dot when you added the breakpoint.
F10 and F11 – Step Over And Step Into
While you have hit a breakpoint you might want to walk through the code while running the application. With F10 you can jump to the next line in the file. This way you can follow what your application and code are doing.
The F11 jumps into a method. Then you can use theF10 again to walk through the code of that method. At the end of the method, F10 will bring you back to the location where you entered the method.
Or press the buttons with your mouse in the toolbar, located somewhere right of the play button.
F12 – Go To Definition
When you are debugging and hit a breakpoint or you are working on your code and you want to jump to a definition of a class, method, or property, simply hit F12 while the cursor is on that particular class, method, or property.
If you just want a peek (a quick view of the code) of the definition, press ALT + F12. This will show a blue-ish screen below the name and show you the code.
If you use this on an interface and press F12, you go to the interface. If you press CTRL + F12 you will jump to the implementation of the interface. A list will be shown if the interface has more than one implementation.
Or right-click on the name and select Go To Definition.
CTRL + R, CTRL + R – Rename
Another powerful feature and key combo in Visual Studio. Do you know that feeling when you have been writing this cool code, with interfaces, classes, methods, and more, but then you realize you used the wrong name for a property that is being used throughout 10 files? Well, I do!
You could just rename it and change it wherever there is a compile error. Or you could just rename the property by putting your cursor on the name of the property (could be everywhere, doesn’t have to be in the class itself) and press CTRL + R, CTRL + R. You can now rename the property and when you press enter, every reference of that property will be changed.
With rename you can also rename classes, interface, enums… Well, everything. You can also press F2. No idea why there are two options to quickly rename something.

Or you can right-click on the name of something and choose Rename.
CTRL + K, CTRL + E – Clean up code
I missed this in the early days of Visual Studio, which made ReSharper the best addon EVER. Indentation, unused usings, unused variables, unwanted spaces, and more. Things you had to clean up before you committed your code. Something you had to do by hand.
But Visual Studio has Clean up code, a powerful feature that will check your code and it will try to fix the format and clean up a lot. Every time I am done with a method or class, I use this key combo to make sure everything is in order and cleaned up.
I highly recommend this one, because it helps keep your code files clean and organized.
Conclusion On Visual Studio Shortcuts
These are my most used Visual Studio shortcuts. Maybe you use others as well? Share them in the comments. Not everyone is a fan of key combos or shortcuts. Which is fine, but I do think using them makes you work way faster. When I am coding I hardly touch my mouse.
There are far more key combos in this big IDE. Not only to make your coding better, but also for unit testing, GIT, and more. You can find most of them in the keyboard setting where you can take a look at the mappings.
Microsoft also created a list of shortcuts in Visual Studio that show a few even I didn’t know. You can find it here.
I wrote a few articles about design patterns. A few examples are the Repository Pattern (or the generic version) and the Strategy Pattern. Both are very effective when writing software. But I have also mentioned SOLID; a collection of design principles. But what is the difference between design patterns and design principles? When do we talk about a design pattern and when about a design principle?
Design patterns and design principles are here to help us make better code. One focuses more on the architecture and guidelines, the other focuses more on solutions in code. It’s important to know that we – developers – have some sort of guidelines and solutions as you go. But remember we are working with software: Expect the unexpected and not all environments are the same.
In my years as a developer, I have come across a lot of design patterns and design principles. Some are thought of by a team member and it works for that solution and some are defined by great minds, still maintained, and are used to this day. This article is about the latter.
Definition
Both design patterns and design principles have a different definition that already shows the big difference between them. A design pattern is a solution to a particular problem. Most definitions can be shown in code. Because it can be translated to code, the solution is usually reusable and helps to solve recurring design issues.
A design principle is a guide that explains certain solutions or guidelines to developers, making it easier to solve problems that might occur in code. These guidelines are here to help us prevent problems. Design problems help us create robust, maintainable, and flexible software. A design principle is not code, only theory.
Design Patterns
One of the most well-known design patterns is the Singleton Pattern. This pattern states and shows that a class should be initiated only once and this instance should be used throughout the application’s lifecycle. This pattern can be used in all programming languages that use classes and initiates classes.
Another example is the Strategy Pattern. This pattern is a behavioral pattern that enables dynamic selection and interchangeability of algorithms or strategies at runtime. You can simplify complex if-statements or make dynamic choices at runtime.
Other examples are the Observer pattern and the Factory pattern. But there are way more. You can divide them into 3 categories: Creational design, structural design, and behavioral design. The image below gives you a good idea of which patterns belong in what category.

It is (almost) impossible to know and learn them all. My advice: Learn the basic idea of them, know when you can use them, and figure out how they work when you are in a situation where you might think “Oh, I know a pattern for that!”
Design Principles
If you look at the design principles, we see other names coming up. Names like SOLID, DRY, KISS, and other funny-looking abbreviations.
DRY stands for Don’t Repeat Yourself, which states that you should not write duplicate code. Duplicate code can cause many problems when you have to change something. Avoid issues and write your code once and reuse it in other parts of your application.
KISS stands for Keep It Simple Stupid, although the last ‘s’ is removed in this day and age. This one states that code should be simple and understandable for others. The more complex you make it, the harder it becomes to understand.
SOLID is a collection of different principles: Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation, and Dependency Inversion, I could give examples for all of them, but then it would be a lot of reading for you (and writing for me). If you follow Wikipedia on SOLID, you will get a lot of information.
Applications
The moment you use a principle or pattern is different too. There is no one-size-fits-all solution and you might not use them at all. Design patterns are used during coding since they give solutions to certain code issues. Patterns can be added or removed along the way.
While creating software, your views might change (or those of a manager). This makes you decide differently and you might want to add or remove a pattern. This is okay. For example, a simple if-statement suddenly becomes this monster of if-elseif-elseif-elseif-elseif-etc. Then maybe it’s time to implement the strategy pattern.
Design principles are usually applied in the early stages of development. They are mostly about architectural design and planning. But also high-level designs. You can’t change these things when you are halfway through coding your application.
The Single Responsibility Principle (SRP) is pretty important. If you lose control of this one and classes and methods get multiple responsibilities, you might end up refactoring your code and breaking a lot of methods because the responsibility might be scattered across multiple methods and classes.
Conclusion On Design Patterns And Design Principles
Design patterns are proven solutions that can help solve reoccurring problems. Design principles are guidelines that can help you give structure and architecture to your software. That’s it, that’s all.
You don’t need to use all design patterns and design principles that exist. Using all SOLID principles in a single software application is nearly impossible. I managed it once, and that was a fairly small project.
It’s a good practice to look at the differences between design principles and design patterns and know when to use which one. Again, it is almost impossible to know all patterns and principles by heart, but know the names and the basic idea behind them.
When I started my tutorial about the Repository Pattern I knew a particular question would be asked: Why not call Entity Framework SaveChanges in the repository after each create, update, or delete? You could do that, but there is a theory about not doing that.
A Bit Of Context
Entity Framework is perfect for letting your application communicate with (almost) all kinds of data sources. You can create, delete, get, and update data using this framework. But, if you simply want to create new data nothing will happen. You need to commit the changes to the data source. You do this with SaveChanges(Async).
For the sake of clarity, I will be using ‘database’ instead of ‘data source’. This is because it makes a little bit easier to understand what I am trying to say.
So If I execute the following code:
dataContext.Movies.Add(movie);
Nothing will happen. The new movie is not added to the database. To make Entity Framework commit the chances (write) I need to change the code to this:
dataContext.Movies.Add(movie); dataContext.SaveChanges();
This is perfectly normal and not something you should lay awake from at night. But I will become a nuisance when you forget it and search for ages for a bug, which isn’t there.
Behind the scenes of Entity Framework, the SaveChanges creates a transaction of all the create, updates, and deletes that are pending. If you call this method after each of these actions, you will have a lot of transactions.
The Repository SaveChanges
Now, look at the following code of a repository called MovieRepository and imagine I change the Create method to this:
public void Create(Movie movie)
{
    dataContext.Movies.Add(movie);
    dataContext.SaveChanges();
}
Each time this method is called, a new transaction is created and executed on the database. Now look at the following code in a class:
for (int i = 0; i< 10; i++)
{
    movieRepository.Create(new() { Title = $"Movie_{i}_Test" });
}
This code will create 10 transactions. A bit overkill, isn’t it?
To The Test
Let’s watch the output of the Web API with a SaveChanges each time a new movie is created. In other words: The method Create in the repository will call the SaveChanges, which is 10 times. This is the output:

I couldn’t get them all on one screen, but I think you get the point.
Alright, let’s remove the SaveChanges from the repository method Create and call the SaveChanges AFTER the for-loop. This is the result:

Instead of seeing 10 transactions, now I just see 1.
And this is just the very reason why I move the SaveChanges out of the methods Create, Update, and Delete.
Be careful though
Although this really saves time and communication with your database, you need to be careful. If you need to connect an entity to a new entity, but both don’t exist yet, you can run into problems.
Also, when you want to delete something you are better off committing that delete as soon as you can. If you don’t, other requests can still use an entity that is pending for deletion. The same goes for an update.
Think carefully when you want to commit a transaction to your database. Sometimes it’s better to do it right away, sometimes it’s better to put more queries in the queue.
My advice: Handle one type of entity and then call the SaveChanges. This way, other entities that may rely on the previous entity have new, updated data.
Not all support SaveChanges
If you are using the repository pattern without Entity Framework, which is possible, you might find that some data sources (yes, we are back at the data sources) don’t need a SaveChanges-method. When this happens I just leave the method blank.
This is an approach that doesn’t break the business logic; the SaveChanges can still be called, but it just doesn’t do anything. But when I configure a repository that does need a SaveChanges I can implement the body of the SaveChanges-method in that repository.
Conclusion On Entity Framework SaveChanges
Well, that’s it actually. I hope this answers the question after reading the repository pattern in C# article.
If you have any questions, suggestions, or ideas about this approach; feel free to comment below.
One of the many questions I got when I was a teacher, and one I still see a lot on the internet, is “What do I need to do to become a good C# developer?” There isn’t a simple, quick answer to that. C# is used a lot and it has a steep learning curve. That said, it does depend on how quickly someone learns. Some people have experience with other languages like Java or Python. They are likely to learn C# quicker than someone who has never printed Hello World on their screen. In this article, I show you how to learn C# essentials and give you some hints and tips on where to look.
This article is to give you some idea of where to focus while you are learning C#. It’s not an in-depth tutorial where I am going to explain everything in detail. I will point you to some crucial subjects and frameworks you should investigate and that will help you understand C#.
A Long, Long Time Ago …
… In 2002. This is the year Microsoft launched C#. I was a bit surprised because I thought C# was way older.
The idea behind C# was to combine the power of C++ and the simplicity of Java. Yes, C# is partially based on Java. These two combined made C#, a language to build software applications for Windows.
The .NET framework already existed in 2002 and C# is closely tied to this framework, using the libraries and tools from .NET at its full capacity. The .NET framework also provides a common runtime environment, type safety, garbage collection, and much more.
With C# you can build Windows desktop applications, web applications, and much more. These days, C# isn’t only working on Windows machines, but you can also use it on Linux, macOS, and mobile devices.
What Is C#?
It is a programming language, but I think you already figured that out. But it’s also OOP, which stands for Object-Oriented Programming. And that means that you can use objects.
Objects are instances of classes, which have properties and methods. You can see them as real-world objects and their interaction with each other. This makes our work as developers a bit easier to understand.
It also helps to create reusable code and make it more modular. Therefore, the code becomes easier to understand and easier to maintain.
With OOP come a few features you should be familiar with encapsulation, inheritance, and polymorphism. A short explanation:
Encapsulation refers to the practice of hiding the internal details of an object and exposing only the methods and properties that are necessary for other objects to interact with it. Inheritance allows for the creation of new classes based on existing ones, with the new class inheriting the properties and behaviors of the original class. Polymorphism allows for the same method to be used with different types of objects, which can simplify the code and make it more flexible.
C# can be used for a wide range of projects. Here is a small list:
- Windows desktop applications
 - Web applications and web services
 - Game development
 - Mobile app development
 - Artificial intelligence and machine learning applications
 - Database programming and development
 - Internet of Things applications
 - Scientific computing and numerical analysis
 - Financial applications and trading systems
 - Blockchain and cryptocurrency development
 
So, Where To Start?
There are many places where you can learn C# essentials. You need to find that one place that suits everything you need and want. Everyone learns differently. Some people love to read, while others rather watch a YouTube video.
C# Bootcamp
For those who like to read, I have created a C# Bootcamp. Divided over 24 chapters, I will explain everything you need to know about C#. From the very first Hello World in a console application to a WinForms application. You will explore Entity Framework, caching, events, and much more. It shows you the very basics of C# and is a great place to start.

https://kenslearningcurve.com/bundles/c-bootcamp-your-one-stop-resource-for-learning-the-language/
But there are many more websites where you can learn C# essentials.
W3Schools

https://www.w3schools.com/cs/index.php
W3Schools has been around for as long as I know and the content is always good. This is a nice tutorial to start with C#. It’s short, covers a lot, and is easy to follow. Besides being short and easy to follow, they also have an editor where you can try the code and experiment with it a bit.
Microsoft

https://learn.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/tutorials/
The documentation from Microsoft hasn’t always been good. It was actually pretty bad. But in the last few years, they created some good content, better to read, and more up-to-date.
One of the things I love about these tutorials is that they have videos and an interactive tutorial where you can type along. The best way to learn is by doing.
CSharpTutorial.net

https://www.csharptutorial.net/
Learn everything from start to end with this online tutorial. It even tells you how to install Visual Studio, it’s pretty up-to-date, and they use good and clear examples.
Although this tutorial has no interactive elements or videos, it does cover all the topics.
Frameworks
As said in the history, C# is specifically built for the .NET framework. It is a really good idea to dive into this framework too.
The .NET Framework, created by Microsoft, is to provide a consistent programming model and command set of APIs for building applications that run on Windows. These days .NET also works on Linux, macOS, and mobile devices.
The framework has a collection of technologies and tools. Here are a few:
- The common language runtime (CLR)
This one manages the execution of code written in different programming languages, such as C#, VB.NET, and F#. - Libraries with prebuilt code for things like file I/O, network tasks, and user interface (UI) development.
 - Different tools that not only help with developing but also help deploy applications, including Visual Studio, a very popular integrated development environment (IDE).
 - A wide range of application types, including Windows desktop applications, web applications, web services, mobile applications, and games.
 
There is .NET Framework, which is the traditional .NET, and the .NET Core. The last one is a cross-platform version of .NET that can run on Windows, Linux, and macOS. The latest versions, .NET 5, .NET 6, and .NET 7, are a merge of .NET Core and .NET Framework into a unified platform.
Most C# tutorials also cover the .NET Framework, so I wouldn’t look for .NET-specific tutorials.
Entity Framework
Another framework that should be really on your radar is Entity Framework. It is a framework that can communicate with your database. It is an ORM, which stands for object-relational mapping and enables us to work with domain objects and properties.
You develop your database using code, which makes it a lot easier to build, read, and handle a database for developers. This is ideal when you create a new project that doesn’t have a database. We call this the code-first approach.
But it also works the other way. If you already have a database you can use Entity Framework to import that database and create the C# code for you. This is what we call the database-first approach.
Most of the C# applications use Entity Framework, so it’s a good idea to dive into that. Here are a few tutorials that can help you out:
Entity Framework For Dummies

Learn the basics with Entity Framework. This is a good start if you never worked with Entity Framework before.
Entity Framework database-first

If you want to use the database-first approach, this tutorial will help you achieve it.
Using SQLite with Entity Framework

Not using an SQL server or do you want to know how you can use a different provider? Learn about it here.
Although Entity Framework will take away the use of pure SQL, it might be a good idea to get some knowledge of TSQL.
Architecture
When you learn C# you will notice you create a lot of files and projects. To keep track of these we use architecture. Yes, the same as with buildings. But instead of building something that looks strange from the outside, we use architecture to create a certain readability for our software.
There are a few types of architecture but the most used is the three-tier or n-tier architecture. It creates layers in your application that will show you where certain logic is placed.
Here are a few tutorials that can help you understand and use the three-tier architecture:
A Guide to 3-Tier Architecture

Learn the theory behind this architecture. Here I will explain the different layers you can use and how to use them.
Your C# Project with a 3-tier approach

Let’s put theory into practice in this tutorial. What you learned of the theory is being used in C# in this tutorial
Unit Testing
While coding is fun and all, you need to make sure your code is always working. When something goes wrong customers usually blame the developers, but 7 out of 10 times it’s a wrong input but the customer. 2 out of 10 times it’s a logic problem (“well, didn’t see that one coming”), and 1 out 10 times it’s a developers error.
We, developers, cannot think for a customer or all the things a user can do. But we can make sure our code works as it should. Therefore, we should test our code. Not the application, but the logic that makes the application work. This is what we call ‘unit testing’.
A few benefits of this kind of testing:
- We can submit proof the method works as it should and the front-end team should make a validation on something.
 - When we change code in another class and it breaks the functionality of a different class, we find it by testing both classes.
 - If set up right, code can not be released as long as some unit tests are not working.
 
The technique behind unit testing isn’t hard to understand, but the logic can get a bit complex. Therefore, it’s important to learn how to unit test as soon as you understand the basics of C#.
Unit Testing with C# for beginners

If you never used unit testing with C#, this is the tutorial to start with. It shows you the basics of XUnit.
Testing Exceptions with xUnit

A bit more in-depth about unit testing. Exceptions are a bit different when it comes to unit testing.
Unit Testing And Mocking

Mocking is a technique you can’t miss when you learn about unit testing. It will help you out more than you might think.
Dependency Injection
Developers love patterns and design patterns are very loved. One of the design patterns is called Dependency Injection. I recommend looking into this one when want to learn C# essentials and maybe a bit more than that.
Dependency Injection is a design pattern commonly used in object-oriented programming that helps to manage dependencies between objects in a system. In simple terms, it is a way of passing objects (or dependencies) to a class from outside, instead of having the class create them internally.
In Dependency Injection, the responsibility for creating and managing objects is shifted from the class itself to a separate component known as a “dependency injection container”. This container is responsible for creating and managing objects and injecting them into the class that needs them. This allows for more modular and flexible code since it separates the creation of objects from the logic of the application.
We don’t use it because we can, but we use it because it has some serious benefits:
- Improved code maintainability and testability
 - Reduced coupling between objects, making it easier to change and update the code
 - Easier management of object lifetimes and resources
 - Better scalability, as the system can be more easily adapted to changes in requirements or scale
 
Dependency injection is not only used in C#, so be careful when you look up information. Be sure to add “c#” in your search query, otherwise, you might be looking at code you don’t understand.
Azure DevOps / GIT / Azure Cloud
Another thing you might want to dive in, but is not really needed when you just begin, is Azure. Mostly known for its Cloud services, but it also has a DevOps service. This isn’t required when you learn C# essentials, but it could be worth looking into it.
Azure Cloud and DevOps are usually spoken out as if they are the same thing. But DevOps has everything you need for your workflow… And it’s free! A few things DevOps has:
- Agile working
You can use a (scrum)board, work in sprints, and keep track of what you are doing - Pipeline
Use the pipelines to create your CI/CD (continuous integration and deployment) - Repos
Store your code in GIT repositories so you can share it with team members or keep an online copy of your work. - Artifacts
Create your own packages, such as NuGet, NPM, etc, to share with team members or other people. - Wiki
Create your own Wiki per project so you can have documentation or notes. 
Microsoft has some pretty good tutorials about Azure DevOps. But if you want to learn more about GIT you might want to check out the tutorial W3Schools.
Azure Cloud, or any other Cloud, is a bit different. To keep it short: The cloud helps you host and manage applications on a global scale. It offers a lot of services. Here are a few:
- Virtual Machines
 - App Service
 - Storage
 - Functions
 - Cognitive Services
 - Automation Services
 - And much more
 
Although it isn’t free, you get credits when you sign up. You can use these credits for a limited period.
Cloud services aren’t things you MUST learn, but it’s a good idea to know what they are and what you can do with them.
To Learn Is To Do
If you are like me and can’t really learn from (digital) books, the best thing you can do is learn by doing it. When you have covered the basics, which you really should do before creating something, you should think of something simple you want to create with C#. A calculator, a game like Hangman, or an online portfolio.
Then you need to start learning how to start and from there you will reach certain problems. Fix those problems by looking them up and implementing the solution in your application. Some solutions aren’t the best, but when it works it works. You will learn to make it better some other time.
During your search for solutions, you will encounter terms you don’t know. Look them up and learn what they are. Don’t look for answers and just accept them; learn why you should accept them.
Don’t know what you should do? I can help you. Send me a message or a comment below.
Conclusion Learn C# Essentials
These are the parts I would check out if you are just starting to learn coding in C#. Keep in mind you can’t be an expert C# developer within a month, it will take time. You will make mistakes and be angry at your computer, but it’s all part of the learning process. This is not only with C# but with all the development languages you might learn. But these are my tips when you want to learn C# essentials.
The best thing is to try, start somewhere, and build up from there. Whether you are learning C#, PHP, Python, Java, or C++, the process is the same, but the language is different.
There are many reasons to use a particular programming language. All languages have their own pros and cons. It’s also a personal choice or because you have grown into it. Or maybe you need to achieve something specific that can only be done with that language. For me, that is C#. I have been using it for many years and noticed you can do a lot with it. From desktop applications to microservices in the Cloud; C# can do it. But why? Why do I use C#? Well, here are my top 5 reasons why you should use C#.
That doesn’t mean there aren’t other languages that can compete with C#. But since I have been using C# for over 20 years it’s hard to learn a new one (although I am working with Python lately, don’t tell anyone).
1: Object-Oriented
Using objects is pretty common these days and was a big deal when it was introduced. C# is all about objects and it works really nicely. It gives you more control of your code and how you address data.
Besides that, object-oriented programming – or OOP for short – gives you the opportunity to reuse code because it’s more modular. And this helps to solve the duplicate code violation.
Other reasons why OOP is important are polymorphism, abstraction, and inheritance. I could write a lot of these topics, but that’s not really the idea of this article.
I remember working for an organization that had software written in 1977 (I am from 1982, go figure). It was written in Basic2C and it basically was just one file, because methods, classes, and objects didn’t exist. When we were done, the original developer of the Basic2C application looked at the code and said that he wanted to have this in 1977. The code was nicely organized in classes and folders. The duplicate code was removed and the application was made for expansion.
I can’t think of a world (or C#) without objects and all the perks that come with them. That is why this one is number one of my top 5 reasons to use C#.
2: Cross-platform
One of the biggest problems we, developers, have is the different platforms. We have Windows, Linux, Android, iPhone, macOS, and everything in between. Creating software that needs to work on all platforms (and the software, like a browser, on that platform) is a nightmare. More and more programming languages are getting cross-platform, meaning you code once and run it on different platforms.
Okay, to be honest; Microsoft was a bit late to the party if it comes to cross-platform. But finally, we got it. I can now use C#, with .NET, to create a mobile app that runs on Android, iPhone, and Windows mobile. At the same time, that app can also run on Windows and Linux. It is called MAUI and is pretty new. But even before MAUI, it was possible to create Linux software with C#.
This is one of the main reasons I use C#. Cross-platform is getting easier every time with C# and the .NET framework.
3: Multipurpose
Sometimes people ask me what I have made with C#. Well, actually everything. From a simple Windows application to a full website with cloud possibilities. Cool, huh? But that is not all that you can create with C#.
Here is a “small” list of projects you can create with C#:
- Windows application
 - Websites
 - REST API
 - Mobile apps
 - Games
 - Cloud services
 - Windows services
 - IoT apps
 - AI and machine learning
 - AR and VR
 - Automation scripts/tools
 - GIS
 
And maybe I forgot a few, but I think you got the point.
I didn’t create all the types, since each project type is a different ballgame. But when I learned that C# can be used to create games, I installed Unity and tried it out. It was perfect. The combination of a GUI allows you to drag and drop all items you want to use. Then you use the C# code so the items in the game can be controlled. In the end, the game can run on multiple platforms, even Oculus Quest, which I have.
4: Asynchronous programming
C# is a big fan of asynchronous programming, making it easier to write responsive and scalable applications. It’s a way to create code that runs in the background and doesn’t block other parts of your application. It can really help improve the performance of your apps because it can multitask.
By using asynchronous programming in C#, you can write applications that are more responsive, scalable, and efficient, making them better suited for today’s high-performance computing environments.
In C#, asynchronous programming is achieved using the “async” and “await” keywords, which allow you to write asynchronous code in a way that is similar to writing synchronous code. The basic idea is that you use the “async” keyword to declare a method as asynchronous, and then use the “await” keyword to specify which operations should run asynchronously.
5: Visual Studio
When I started with Microsoft development, I used ASP classic and was introduced to Visual Studio 2005, which was launched in November 2005. I didn’t like it. I was used to UltraEdit for PHP and Visual Studio was a totally different game. I just didn’t like it and I can’t really think of a reason why, just a feeling.
It’s 2023 and I love Visual Studio. It has grown in so many ways. The free version (community) is a really big IDE with a lot of functionality and I can do everything I need. And if it’s not in Visual Studio Community, someone has made an extension for it.
Although more and more people head over (or actually head back) to the CLI, I keep using Visual Studio. With just a few clicks I have my API created and running. I can easily debug the code, find all kinds of information about my app, deploy my apps to the cloud, and much much more.
The design has become increasingly better over the years. IntelliSense has grown too, giving us a better idea of how we can use objects, classes, and libraries.
Although the RAM usage is a bit disappointing. Sometimes it uses 4 GB and on a laptop that isn’t really convenient. Updates are usually over 1GB and because I am usually in hotels, updating is a @*&^!*. But this is more of a Microsoft thing because Windows updates are commonly over 2GB.
From one IDE I can create everything I want. Just by selecting, clicking, and some configuration.
Conclusion
Well, there you have it; my top 5 reasons why I use C#. As you can see/read C# can be used for a lot of things. Especially if you combine it with the .NET framework. Together they are very powerful.
That doesn’t mean other languages can’t do the same. In fact, there are languages that could be better in some cases for particular scenarios. Yes, you can use C# on Linux, but I prefer Python on my Raspberry PI. Yes, you can build a website with C#, but I rather use PHP or Angular. I mainly use C# for the back end, holding and managing the logic and storage (databases for example).
But with Blazor and MAUI being relatively new players I might use those rather than PHP or Angular. Mainly because now I have dozens of programs to create and manage all the different projects I have. Visual Studio Code did resolve that mostly because it allows you to install plugins/extensions for different languages. I use Visual Studio Code for Angular, Python, Arduino, and PHP.
So, what’s my take on Java? Well, it looks nice and can do a lot. C# and Java are almost the same but have different histories. They are both OOP and support similar programming paradigms and features, including encapsulation, inheritance, polymorphism, and more. The big differences are the syntax, libraries, and more. But is Java really that different? Should C#’ers “hate” Java-people? Nah, both have pros and cons.
Still, choose what you think is best and suits you the most. Maybe these 5 reasons help you decide, maybe not. What is your opinion of C#? Or do you love another language for particular reasons? Let me know in the comments!
As a developer, you love to code and fit all the pieces of the puzzle. But there are a few things most developers don’t like. One of them is documentation. It feels like it takes twice as long as developing an application. Another aspect we don’t really like is unit testing. Why make tests of code we just made? Well, there are a lot of reasons to create unit tests. In this blog, I am going to explain the 10 reasons why to use unit tests.
First, I will explain what unit testing is. After that, I will give you 10 reasons why you should unit test. They are in random order since I think each reason is essential.
What is unit testing?
As you might know, unit testing is really important to proof your code. In other tutorials, I explained how you can use unit testing with C# projects. These are basic scenarios concerning unit testing. But what happens if the class or method you are testing needs external data, like a database of a file? Or maybe the method is depending on a different class.
To solve this problem we use mocking, which means creating objects that simulate the behavior of real objects. This is exactly what we want.
Unit testing is not just a way to prove your code works, there are many more reasons to use it. Unit testing isn’t limited to C#, other languages can benefit from this way of working as well.
Reason 1: Proof your code works
Maybe this is the most important reason for us developers. With unit tests, we can test and prove our code works in certain conditions and with different parameters. If you continue on classes and other methods it could happen that some unit tests will succeed first, but fail when you change code. In general, the unit test is leading, unless the specifications change.
Another reason unit testing is helpful is when something goes wrong in the application. If an end-user enters a complaint through the support desk because of an error, support will tell you there is a problem with the code. To avoid being told you didn’t do your job, you can see if the code really breaks, or if it’s a user error, or maybe it’s a design flaw, or the specs weren’t clear.
															Reason 2: work together in a team
If you work in a team, or multiple teams are working on the same project, it might happen someone is changing the code unwillingly. In the past, people would just submit the code. Or even worse: Send it to production without fully testing the code. I experienced a really big issue in a webshop that had costs 6000 Euro… Per 15 minutes. The website was down for 1 hour. They couldn’t find the problem.
With unit tests, you could avoid these problems. If you write code with unit tests you can set up CI/CD (Continuous integrations and deployment) that will stop publishing the code to a production environment as soon as there is a problem.
Reason 3: Test your code without GUI
When you need to test if something works you create a proof of concept or POC for short. It means you create a small project dedicated to the one element you want to try.
For example I was creating a project that can push posts to Twitter, Facebook, Instagram, and other social media. This all works and the project was pretty big. Next, I wanted to automate it, and set specific dates and times when something should be pushed to social media. I wanted to use Azure Automation for this, but I had no idea how to use it. So I created a new project outside the existing project to discover Azure Automation.
This way of working doesn’t break or pollute the main project. But I don’t have a GUI to test if everything works. That’s why I create a unit test project where I can build and test different aspects of the POC.
Using unit tests like this also improves performance and time. A unit test starts and finishes way quicker than a GUI.
Reason 4: It's in the pipeline
Continuous integrations and deployment (CI/CD) are really important if you work on bigger projects. It helps you code and deploy way faster. Azure DevOps uses pipelines to set up, configure, and run CI/CD.
Another big advantage of these pipelines is that you can run unit tests during the CI/CD. First, the pipeline will download your code from GIT or Azure GIT, then build it, and run unit tests. If a unit test fails the whole pipeline will stop and notify you.
This is especially convenient if you work with multiple people and someone forgets to check the unit tests before committing the code.
Reason 5: Discover the errors
Imagine you are coding your project. Some music, coffee… The lot. But then disaster strikes: You get an error and you can’t find it. Unit tests can help you with this.
You can create a unit test that simulates all your input and output and hit the error. This could make you realize a certain input or output of another method isn’t correct.
Unit tests can be created and you don’t have to rely on user input or external data. You don’t have to use a real database. This will help you pinpoint errors.
If you found the error, you can debug the unit tests, and go even deeper into the rabbit hole until you find the location of the error. Then you fix it (duh), but you keep the unit test.
Reason 6: Documentation
Unit tests are actually user scenarios, telling what happens if certain actions, parameters, or situations are used. Therefore you can use the unit tests as documentation. They tell you what methods do and how they react to certain situations or what kind of exceptions are thrown.
It’s not like you can export the unit tests to a PDF and give it to the end user. It’s more of a documentation for other developers.
When I start a new job and I see the code, the first thing I do is read the unit tests. It usually gives me a very clear idea of what the code does and how it works. If there are no unit tests I usually try to convince people to create them so I can discover the projects.
Reason 7: Code coverage
Proving your code or making sure no bad code is sent to the production is nice and all, but what happens if not all code is tested? That is why we use code coverage.
It means that you can see what kind of percentage is being tested in your projects. Most developers say that 80% of your code should be tested. I say 90%, but that is personal.
Visual Studio has code coverage built-in, but not in the community version. But don’t sigh yet! There are extensions you can install. Fine Code Coverage is one of them and I have been using this one for a long time. You can find it by going to Extensions -> Manage Extensions in Visual Studio and searching for coverage. Then download and install it. If you run your tests after installation, you will see a window like this:
											As you can see I have tested 66% of the code. It allows you to zoom in on what you didn’t test yet. In the code editor, it will add green, yellow, and red markings on the left side. Green indicates the code is tested, yellow means semi-tested (usually if-statements), and red means you haven’t tested it yet.
In the beginning, I said that there is no particular order to these reasons, but I think code coverage is one of the most important reasons why you should unit test.
Reason 8: Performance
Although it is possible, I would not recommend it to use unit tests for performance testing. It’s kind of a hack and unit tests are usually run against local data. Meaning you don’t do external API calls, use a real database, etcetera.
But if you really want to try it, there is a simple piece of code that can help you with that. All you need is a stopwatch and something to execute.
Reason 9: Readability
If you make your unit tests via specifications, your code will become better to understand. The tests will become your documentation, but building your tests also allows you to create better code. You will think more about the flow of your methods (we test methods, not classes).
Especially when you use TDD, which means test-driven development. Here you create your test first before you create your implementation. Most people find it hard to use this approach, but it solves 99% of the common errors that could crash your app.
Reason 10: Think about it
Creating unit tests, with or without TDD, lets you think about the flow of your methods. You take more time to discover all the possible outcomes in the methods you are testing.
Even when you are writing tests for existing code you will discover new outcomes and you can add new tests, making your code even better.
Conclusion
These are my 10 reasons to create unit tests, maybe there are more or maybe you don’t agree on some, which is fine.
The most important part is that you keep your code healthy, bug-free, and easy to read. Although it’s not 100% bug-free, you can try to make it to at least 90%. Unit tests aren’t bad, they don’t take that much time, and they are super handy. It makes my life a lot easier.
A well-structured and organized solution is crucial for maintainability and scalability in software development. The 3-Tier architecture clarifies concerns, allowing developers to manage the application logic, data access, and presentation layers separately. In this article, we will explore the benefits of this architecture and learn how to implement it in a Visual Studio solution using C#.
Different Architectures
There are many types of architectures you can use for your applications. You just have to find the one that suits your needs. I like the 3-tier architecture. It’s small, simple, and easy to create. But in some cases, I might change it because of my needs.
There are 9 known architectures we use these days in C#:
- Monolithic architecture
 - Microservices architecture
 - 3-tier architecture
 - Model-View-Controller (MVC) architecture
 - Model-View-Presenter (MVP) architecture
 - Model-View-ViewModel (MVVM) architecture
 - Event-Driven architecture
 - Service-Oriented Architecture (SOA)
 - Client-Server architecture
 
I am not going to explain them all. It will become a very large page. But the top 3 popular architectures are:
- Microservices architecture
A way to break down a large and complex application into smaller, independent services that are connected. They communicate with each other through APIs. This architecture is becoming popular with bigger projects. You can maintain and deploy the smaller projects more easily. - 3-tier architecture
This type of architecture separates an application into three logical components: the presentation layer, the application layer, and the data layer. This separation of concerns provides better scalability, maintainability, and ease of development. - Model-View-Controller (MVC) architecture 
Well-known if you have created web applications and/or APIs with C#. It is a design that separates the application logic from the user interface. The Model component represents the data and the business logic, the View component displays the data, and the Controller component handles user input and updates the Model. This separation of concerns provides a more organized and maintainable codebase. 
This tutorial explains the 3-tier architecture mainly because we use this one the most.
The 3-Tier Architecture
This architecture separates the application into three logical components. These three layers help to maintain the code and separation of concern. But for me, it has a different reason I use this architecture: reusability.
Especially at a time when you might want to create a Windows, macOS, Android, Linux, and iOS app that does the same thing. Do you create the same class over and over again? No, you create a class library with the logic and reference that library in all the projects that need the logic. If something changes in the class library, all other projects get updated.
Three Different Layers
Each layer has its own responsibility but only talks to one other layer. The presentation layer only talks to the application layer and the application layer only talks to the data layer. That means that if you want to show a list of movies, for example, you need to ask the application layer which will ask the data layer. The application layer will send the information of the data layer to the presentation layer. The presentation layer never talks to the data layer directly.
The Data Layer
The first layer I am discussing is the data layer. Maybe the name already tells you what this layer is about: Data. It could be data from a database or a file. Generally speaking, we use this layer to transport data from and to a data source.
This layer does not contain any form of logic. It just sends or retrieves data from the data source. Nothing special. Logic is placed in the application layer.
If you have multiple data sources you need to manage, you can create multiple data layers and bring them together in the application by using dependency injection.
The data layer is a layer that is disappearing a bit. Before we had ORMs, like Entity Framework, we had to write a lot of code to retrieve and send data from and to a database. With Entity Framework, we don’t need that anymore. That is the reason the logic for the database is moving to the application layer.
The Application Layer
This layer is the beating heart for all your logic. Here you combine logic with the data from the data layer. This layer is responsible for calculations and decisions before sending the data to your presentation layer.
The application layer is often called the business layer. This is a small heritage from the time we had the DLL (data layer library) and the BLL (business layer library). In my projects, I usually call it the business.
For example, the presentation layer requests a list of movies, ordered by title. The application layer will retrieve all the movies from the data layer and sort them by title.
This layer does nothing with the data source directly. The reason is simple: The application layer should never know what kind of data source you use. If you implement dependency injection, the data source can change. Even the whole data layer can change (except for the interface). But the application layer should always be working without changing it.
You can have multiple application layers. I created a logger once. A simple logger that stores messages in a file. But later I needed a logger that stored the messages in a database. I didn’t want two loggers in the single class library representing my application layer. So I created a second application layer for the logger.
It’s usually this layer we unit test because it contains the most important code of our application.
The Presentation Layer
And last but not least; the presentation layer. This layer is what a user sees. It could be a console application, WinForms, or an API. It’s the one that gives output that a user or a client application can work with. This layer is also referred to as the top-most layer in this architecture.
It is responsible for presenting the data from the application layer in a user-friendly way. Examples of user-friendly are web pages, WinForms, and mobile apps.
A console application or a WinForms application has a clear GUI. You can click on stuff and it works. An API doesn’t have that. But it does expose JSON (or XML if you want). An API is considered a front-end application.
Conclusion On A Guide to 3-Tier Architecture
The 3-tier architecture is the most used because it’s easy to understand and implement. I encourage you to use this if you just start or create a small project. If you have a really, really small project; make your life easier, and don’t use layers.
The idea behind these layers is to separate data, logic, and application. I think the names are a bit confusing and most people think the application layer is the front end, but that’s the presentation layer. And that is why I call the application layer the business layer.
Related Articles
Lately, I have seen a lot of articles about clean code. And that’s good! When I was teaching people asked me as well what my vision of clean code is. I never understood why people ask about ‘my vision’. I just read about Robert C. Martin, a.k.a. Uncle Bob, and apply clean code with C#.
But some people read a book and follow it as the bible of clean code. I believe there is more to it than just someone telling his best idea of clean code.
My Vision On Apply Clean Code With C#
My vision of clean code is based on experience and learning from others. I have a few ground ‘rules’ I stick to and try to teach that to others who are interested. These are not the only way to go and you may not agree with some, but the following tips are some I have been using for many years.
I will focus mostly on clean code with C#, since that is what language I generally use. But you can apply clean code in most languages.
The Team
Clean code is not some given book, like the 10 commandments. You will discover and develop it when you work your way up to being a good developer. Yes, there are good developers with terrible code who usually don’t work in a team. Because as a team you set certain ground rules on how team members should be making code. The idea of this is simple: Everyone in the team develops code in the same way, so everyone can read it, change it, and continue on it.
We call these ‘rules’ code styles. It’s most of the time a (digital) document that describes what kind of decisions the team has made on certain developing styles.
It can be about anything. Like using var or not, using brackets with a single-lined if statement or not, and declaring variables with capitals, underscores… Or not. But also things like the maximum number of parameters a method should receive.
This way the team members know how to develop certain codes. Sadly, I have seen this kind of coding style in 3 companies I worked for… Out of the 21. It usually caused frustration, (unresolved) discussions, and even a fight.
So, how to create this document of coding styles? Well, it starts with the team getting together and talking about the current code. Maybe some members have questions or disagreements about certain choices of other members. It’s not a race about who is right and who is wrong, but it’s about getting the team together in a way so everyone can read and understand the code. If two members of the team can’t agree, it’s usually the lead developer that makes the final decision.
Some Rules I use
Besides the team, it’s also important for you to make decisions on how to create clean code. If you are working on a project by yourself you could go with the flow and do whatever seems fit. But, what happens if someone comes and helps you? Or if you publish your code online, like in a question or tutorial?
So, to help you decide your way and the idea of clean code, here are the tips that I use.
To Var Or Not To Var
I think most of us developers know this discussion: Should we use var or explicit declaration? I’ve been using both because my employers asked me to. But when I work on a project by myself, I use var. Yes, past tense.
Using var looks like this:
var someString = "Hello world"; var client = new HttpClient();
Var is an alias of what’s on the right side of the =. So in the first line, var is a string. In the second line, var is an HTTPClient. A big advantage is that you don’t have to type the type twice. So… Var is for lazy people? Nah, it’s just easier. It’s not about being lazy or not. It’s a way of declaring a variable.
But it does make the code a little bit harder to read. If you use explicit declaration on the previous code it would look like this:
string someString = "Hello world"; HttpClient client = new HttpClient();
In the example above, you can see the types of variables quickly. This way of declaring a variable is enforced in example code in tutorials or teaching. Another advantage is that you can now remove the type on the right side and just type ‘new’. For example:
string someString = "Hello world"; HttpClient client = new();
This doesn’t work on the string since it doesn’t have an initializer.
I used var a lot, but I am using explicit way more. Why? The new way of initializing a type has become more efficient (read: less code) and it’s easier to read. In some cases I still use var, but that’s limited to unit tests.
Someone told me that using var was insufficient. Because the application has to convert the var to the actual type. This is not true; at compile time the var is being replaced by the actual type, making the var not insufficient when running the application.
Brackets
The use of brackets is well-known in C# and in other languages. It defines a block of code, like a method, class, if-statement, and so on. In some cases, you can remove the brackets.
We all know this piece of code:
if(val == "somestring")
{
    Console.WriteLine("Correct!");
}
A simple if-statement with one line within the block. But you can remove the brackets since there is just one line. The advantage is that you reduce the number of brackets, making the code look a bit cleaner and you can focus more on the code.
if(val == "somestring")
    Console.WriteLine("Correct!");
Maybe the example code does not really show the obvious reason for removing the brackets, so here is a bigger example with brackets:
string greeting = "Hello world";
if(greeting.GetType() == typeof(String))
{
    // Yes this is a string
    Console.WriteLine(greeting);
}
else
{
    Console.WriteLine("This is not a string.");
}
if(greeting.Length > 5)
{
    Console.WriteLine("The length of the greeting exceeds 5 characters.");
}
else
{
    Console.WriteLine(greeting);
}
It’s a simple piece of code of 20 lines. We can reduce the number of lines to 11 by simply removing the brackets:
string greeting = "Hello world";
if(greeting.GetType() == typeof(String))
    Console.WriteLine(greeting);
else
    Console.WriteLine("This is not a string.");
if(greeting.Length > 5)
    Console.WriteLine("The length of the greeting exceeds 5 characters.");
else
    Console.WriteLine(greeting);
The downside of this is when you add another line to the body of the if-statement. Brackets can be removed when you have just one line. If you add more you’ll need to add the brackets again.
string greeting = "Hello world";
if(greeting.GetType() == typeof(String))
    Console.WriteLine(greeting);
else
    Console.WriteLine("This is not a string.");
Console.WriteLine("Please use a string"); // This line will always be printed.
if(greeting.Length > 5)
    Console.WriteLine("The length of the greeting exceeds 5 characters.");
else
    Console.WriteLine(greeting);
Line 7 has been added. As you can see the line jumps to the left, showing you it’s not a part of the else-statement. Line 7 will always be executed, even when the greeting is a string.
I am not a fan of brackets as they kinda blur the code. I try to reduce the number of brackets to a minimum if possible. This usually also helps to notice other refactor options, like in the code above. We can reduce the number of lines even more.
string greeting = "Hello world";
if (greeting.GetType() == typeof(string) && greeting.Length <= 5)
    Console.WriteLine(greeting);
else if (greeting.Length > 5)
    Console.WriteLine("The length of the greeting exceeds 5 characters.");
else
    Console.WriteLine("This is not a string.");
When I apply clean code with C#, this is one of the things I look at.
Comments
I already posted a whole blog about comments and how (not) to use them. So here is a small summary.
Don’t use comments
…
Thank you.
Comments can blur the code, making it harder to read the code. The only times one should use comments are:
- When you work on a project alone.
 - To write summaries in an interface.
 - Explain a really hard calculation
 
Don’t use comments to:
- Explain the method.
Maybe the name of your method is not correct. - To mark you need to do something later.
Use a scrum board for this. - Tell your team members to leave the code alone.
 
Camel case and Pascal case
- The way we declare variables, methods, and classes is important. Not only the name but also how we write them. It’s a convention so we can see what a specific name is. Camel and Pascal casing are a big part of this. They define how to use capital letters in a name:
 - PascalCasing
 - camelCasing
 
Here I follow the Microsoft code conventions, meaning I follow their view on when to use Pascal casing or when to use camel casing. There is a whole document you can follow on this topic. But it’s pretty simple:
- Public types are PascalCasing
This includes, but is not limited to, classes, interfaces, methods, properties, and enums. - Private types are camelCasing
This includes but is not limited to, methods, variables, and private properties. 
An example:
namespace ExampleCasing // namespace = PascalCasing
{
    // interface = PascalCasing. Mind the capital I and E.
    public interface IExampleClass 
    {
        string Name { get; set; }
    }
    public class ExampleClass // class = PascalCasing
    {
        public string Name { get; set; } // public property = PascalCasing
        private DateTime currentDateTime; // private class variable = camelCasing
        public ExampleClass() // class = PascalCasing
        {
            string exampleVariable = "Hello world"; // private variable = camelCasing
        }
        private void DoSomething() // private method = camelCasing
        {
            ...
        }
    }
    public enum Gender // enum = PascalCasing
    {
        Male,
        Female,
        Other
    }
}
Naming Conventions
To apply clean code with C# is much more than how you code. It’s also how you name variables, classes, and so on. Camel casing and Pascal casing are both parts of the naming convention. But there is more to naming convention than just these two.
There are a few rules I stick to:
- A name should be short.
private void ExecuteSQLStatementOnSQLDatabaseThatIsLocatedInAzureWithAConnectionStringInAppSettings() - A name should be explainable.
private void Execute() … Execute what?
private void ExecuteSQLStatement()… Ah, that! - Only use letters.
Don’t use numbers, underscores, or other characters. Some are not even allowed.
private void Execute_Sql_Statement().Exception on the unit test methods. - Don’t use prefixes on variables
string sGreeting = “Hello world”;
var lAmount = 1029265527892; 
Keep the words readable
A variable, class, or method name that consists of multiple words should have capitals on each word. For example:
private string helloWorld { get; set; } // Correct; capital W
private string executestatement() // Incorrect. The 's' of statement should be a capital S.
{
    ...
}
public class SomeNewClass // Correct
{
}
The reason why we do this is to make the names more readable. On line 2 for example. It’s harder to read since you need to look for the words. Lines 1 and 7 are easier.
Underscores
Microsoft coding conventions dictate that when you have private property on a class you should add an underscore as a prefix. This is one of the Microsoft ‘rules’ I don’t use. A variable written with camel casing shows that it is private.
public class ExampleClass
{
    private DateTime currentDateTime;
    private string _greetingText;
}
Method length
Some people say a method should be no longer than 5 rows of code. If that includes the brackets, I don’t know. What I do know is that it’s almost impossible to accomplish this. Yes, you can create hundreds of small methods, but does that help create clean code?
What I try to do is keep my methods between 10 and 20 lines of code, keeping the single responsibility principle in mind: A method or class should have one responsibility. Sometimes I go over those 20 lines of code, but usually with a reason. Don’t stress about it too much.
Tip: Just start writing code in one method. When everything works like it should, with unit tests, try to break up the method into smaller methods. In the end, you will end up with small methods with around 10 to 20 lines. This is a great way to apply clean code with C#.
Parameters length
A method can have parameters. I like to keep them to a minimum of 5, If a method gets more, I usually create an object that holds those parameters and uses that single object as a parameter variable.
Some people think 3 is the max, which is doable. Keep the single responsibility principle in mind. If you put properties together in one single object, that have nothing in common, you’ll break that principle.
Line length
Common practice dictates that lines of code should not exceed the screen. I am really struggling with that one.
What it means is that you don’t scroll in the editor and you keep all your code in view. This is almost impossible due to the different resolutions of screens. The resolution of my laptop is 1920×1080. But if someone has a lower resolution, let’s say 800×600, the code that looks fine on my screen might create a scrollbar on the lower resolution.
Another problem is Visual Studio or different editors in general. Take a look at the screenshot below, which is my Visual Studio.

Everything I need: Editor and Solution Explorer. Other windows, like the SQL browser, GIT, and Error List are not needed all the time, so I minimize or hide them.
No, take a look at the screen of someone who likes to keep track of everything:

Who decides the length of the lines? How many characters are allowed per line? My advice: Keep your editor clean.
Conclusion On Apply Clean Code With C#
These are basically the rules of clean code and coding conventions I try to follow. Again, these are things I learned during my work and what I have found. You don’t have to agree. If you have any ideas or add-ons, please let me know in the comments.
To apply clean code with C#, or any language, is a good way to keep your code maintainable and readable. Just don’t go berserk with it. Do what you can and don’t spend hours and hours to perfect your code. Perfect code doesn’t exist.
C# has a lot of data types and some aren’t even used. We often use them without really knowing why we use them and what the data types are capable of. In the series “The C# data types”, I show you a few of those data types and look into them. Some basic examples, tips, and tricks. In this article: what is an int?
What is an int?
An int is declared by using the int data type, followed by the name of the variable. An int has a default value of 0 (zero).
An int (integer) can only contain whole numbers, so no decimals. The highest number an int can have is 2,147,483,647 and the lowest is -2,147,483,647. If you need a higher number you can use the long.
The System.Int32 and int are the same, where int is an alias of the System.Int32.
You can use integers to perform some calculations, like the example below.
Integers are not only used to calculate but they can also be used in enums and identifiers (IDs).
Parsing
There are several ways of parsing the int, meaning you can change the type of a string to an int, for example. You can only do this when the string contains numbers. If it contains a letter it will throw an exception. Below are the most common ways to parse an int.
int.Parse()
This code will change the string ‘a’ to an int of 1234. You can’t do math with a string, but you can with an int.
If the string a would be something like 123a4, line number 2 would give an example:
To avoid this exception or avoid handling any exception when parsing an int it’s better to use the TryParse().
int.TryParse()
On line 3 the int.TryParse is used. It has two parameters:
- string a
 - out int result
 
Convert.ToInt32()
This method can convert a string to an int. It works basically the same as the methods above and the outcome is the same.
If the string is a not a number, a FormatException will be thrown.
So, which one?
All three methods work and can convert a string to an int. But it depends on your situation. When you know the string will always be a number, no doubt about it, you can use int.Parse().
If you are handling user input and expect an integer, but the user can also enter other characters, use int.TryParse().
Convert.ToInt32() is a bit slower than int.TryParse() and it doesn’t give a null ArgumentNullException, whereas the others do give it when the value is NULL. The Convert class can do much more than just convert a string to an int.
Math
The int(eger) is very useful for maths, the one thing most of us hated back at school. Subtraction, adding, multiplication and division… It’s all possible. Just keep in mind that the outcome will have whole numbers only.
Basic math examples
Divide by zero
You can’t divide a number by zero. Not in C#, nor in a calculator. If you do this in C# you will get the exception DivideByZeroException.
Line 2 will give a compile error. This means Visual Studio will show a red curly line under the “6 / 0”. It’s smart enough to recognize the error.
Line 8 however won’t see the ‘error’ and you can run the application. The application will crash when reaching line 8 and give the exception.
Rounding up or down
In a previous example I showed that 5 / 6 is zero, but it’s actually 0,833333. In most case this number will be rounded up, making it 1. In C# this can be done with Math.Round().
The Math.Round() can round a number up or down. In this case, with the 5 / 6 we don’t have to do anything. The Math.Round() will see it’s above 0.5 and rounds it up. Two downsides… First, we need to convert one of the two variables to a decimal. It’s not hard, but not really nice. Second is that the Math.Round() returns a decimal, but we want to use int. Again, not hard, but not nice.
Validation
An int is by default 0 (zero) and it can only contain whole numbers, negative or positive. Validating an int is fairly easy. Usually, you check if an int is smaller, bigger, equal, or a combination of these.
An int is not nullable, meaning you can’t assign NULL to it. But you can make it nullable by adding the question mark(?) after int. This is not something I would advise.
Conclusion for integers
I think that int is just as important as the string. You can use it anywhere and you will be using it everywhere. From simple math calculations to identifiers to enumerations. The only downside for int is that it can only contain whole numbers, so no decimals. For these, you will need to use other data types.
If you want to read more about the int and numbers, you can read the official Microsoft page.
If you think some method or trick is missing, let me know!
                        
                        
                        
                        
                        
                        
                        
								
                        

