Skip to content

5 (Basic)Tips in C#🔧

16 de July de 2021
5 basic csharp tips

If you have taken a call to grow your career in the information technology sector, knowledge of coding is essential. It is the most in-demand skill in the industry. Thus, the programming knowledge you gain and practice, in the beginning, is priceless.

Here you have 5 good tips that will help you learn programming skills.

Index

Write unit tests for non-public methods

Surely if you work with projects that require quality control you have had to introduce a test project that involves unit tests. In that case, you have probably had problems trying to try a method that is not public.

C# allows internal assemblies to be made visible to other assemblies. To solve it we will add the following attribute in the AseemblyInfo.cs.

Use tuples

Sometimes it is better to use a containing tuple of typed values than to use classes, especially when the data structure created is only going to be used at a specific moment.

No need to create temporary collections, better to use yield

We usually create enumerable temporary structures to return in a method.

Something similar to the following 👇

In these cases we can save a couple of lines of code by using the yield command.

Next we show the previous example but using yield 👇

Report that the method is obsolete

If your library is being used by different clients and you want some method to stop being used because there is another method that replaces them or simply because it no longer works, you can indicate it with the following attribute 👇

The above code would cause a warning in the code that invokes this method. However, if you want it to no longer be usable in any way, you would use an additional Boolean parameter such as True.

Remember that linq queries are deferred

When a query is made in linq it only actually runs when the results are accessed. This is called deferred execution. In order for the query to be executed only once, we must use the ToList method.

Settings