Skip to content

5 (Basic)Tips in C# (part 2)🔧

9 de August de 2021
5 basic c# tips part 2

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 another 5 good tips that will help you learn programming skills.

Index

Converting Business Entities Using the Explicit Keyword

Use Explicit to define entity conversion. The conversion method will be called when needed.

An example 👇

Keep the original Stack Trace

In .NET, when a catch block is used to catch an exception and it is thrown again, information is lost since it is understood that it has been handled and that the exception is generated from the block that caught it.

Example👇

To keep the original trace of the error we will do this👇

Flags Attribute — Enum Grouping

Using the Flags attribute within an enumeration will allow you to group the enum values.

Force the base type for a generic type

Let’s start from the fact that we have created a generic class where it must be fulfilled that the generic type provided in the class must inherit from a specific interface, this can be done as shown in the following example👇

Or even at the method level👇

Exposing a property as IEnumerable does not make it read-only

Let’s imagine that we create a class with a property of type IEnumerable, in this case it is possible to modify this property even if it is read only.

An example👇

In the previous code we have been able to verify that the list could be modified when our intention is to read it only.

To remedy this we can use the following 👇

Settings