5 (Basic) Tips in C# 🔧
Tags: csharp, dotnet, netcore, programming
Published: 2021-08-02 16:46:00

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.

---

### 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._

```csharp
_// Make a specific assembly visible  
\[assembly: InternalsVisibleTo("MyTestAssembly")\]_
```

---

### 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.

```csharp
_public Tuple<int, string, string> GetEmployee()  
{  
int employeeId = 100;  
string firstName = "John";  
string lastName = "Smith";  
//create a tuple and return it  
return Tuple.Create(employeeId, firstName, lastName);  
_}
```

---

### 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 👇

```csharp
_public List<int> GetValuesGreaterThan100(List<int> masterCollection)  
{  
List<int> tempResult = new List<int>();  
foreach (var value in masterCollection)  
{  
if (value > 100)  
tempResult.Add(value);  
}  
return tempResult;  
}_
```

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 👇

```csharp
_public IEnumerable<int> GetValuesGreaterThan100(List<int> masterCollection)  
{  
foreach (var value in masterCollection)  
{  
if (value > 100)  
yield return value;  
}  
}_
```

---

### 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 👇

```csharp
_\[Obsolete("This method will be obsolete soon. To replace it you can use the XYZ method.")\]  
public void MyComponentLegacyMethod()  
{  
// Here the implementation  
}_
```

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_.


```csharp
_\[Obsolete("This method will be obsolete soon. To replace it you can use the XYZ method.", true)\]  
public void MyComponentLegacyMethod()  
{  
// Here the implementation  
}_
```

---

### 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.

```csharp
_public void MyComponentLegacyMethod(List<int> masterCollection)  
{  
// Without the ToList method this linq query would be executed twice  
var result = masterCollection.Where(i => i > 100).ToList();  
Console.WriteLine(result.Count());  
Console.WriteLine(result.Average());  
}_
```