DEV Community

Cover image for 5 (Basic) Tips in C# 🔧
ByteHide
ByteHide

Posted on • Edited on • Originally published at bytehide.com

5 (Basic) Tips in C# 🔧

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.

_// Make a specific assembly visible  
\[assembly: InternalsVisibleTo("MyTestAssembly")\]_
Enter fullscreen mode Exit fullscreen mode

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.

_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);  
_}
Enter fullscreen mode Exit fullscreen mode

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 👇

_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;  
}_
Enter fullscreen mode Exit fullscreen mode

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 👇

_public IEnumerable<int> GetValuesGreaterThan100(List<int> masterCollection)  
{  
foreach (var value in masterCollection)  
{  
if (value > 100)  
yield return value;  
}  
}_
Enter fullscreen mode Exit fullscreen mode

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 👇

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

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.

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

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.

_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());  
}_
Enter fullscreen mode Exit fullscreen mode

Top comments (0)