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

Juan España
ByteHide
Published in
3 min readAug 9, 2021

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.

Converting Business Entities Using the Explicit Keyword

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

An example 👇

class Program
{
static void Main(string[] args)
{
ExternalEntity entity = new ExternalEntity()
{
Id = 1001,
FirstName = "Dave",
LastName = "Johnson"
};
MyEntity convertedEntity = (MyEntity)entity;
}
}
class MyEntity
{
public int Id { get; set; }
public string FullName { get; set; }
public static explicit operator MyEntity(ExternalEntity externalEntity)
{
return new MyEntity()
{
Id = externalEntity.Id,
FullName = externalEntity.FirstName + " " + externalEntity.LastName
};}
}
class ExternalEntity
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
}

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👇

public void RunDataOperation()
{
try
{
Intialize();
ConnectDatabase();
Execute();
}
catch (Exception exception)
{
throw exception;
}
}

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

public void RunDataOperation()
{
try
{
Intialize();
ConnectDatabase();
Execute();
}
catch (Exception)
{
throw;
}
}

Flags Attribute — Enum Grouping

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

class Program
{
static void Main(string[] args)
{
int snakes = 14;
Console.WriteLine((Reptile)snakes);
}
}
[Flags]
enum Reptile
{
BlackMamba = 2,
CottonMouth = 4,
Wiper = 8,
Crocodile = 16,
Aligator = 32
}

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👇

class MyGenricClass<T> where T : IMyInterface
{
// Class body would go here
}

Or even at the method level👇

class MyGenricClass
{
public void MyGenericMethod<T>(T t) where T : IMyInterface
{
//
Generic implementation would go here
}
}

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👇

class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
((List<string>)myClass.ReadOnlyNameCollection).Add("######From Client#####");
myClass.Print();
}
}
class MyClass
{
List<string> _nameCollection = new List<string>();
public MyClass()
{
_nameCollection.Add("Rob");
_nameCollection.Add("John");
_nameCollection.Add("Jummy");
_nameCollection.Add("Derek");
}
public IEnumerable<string> ReadOnlyNameCollection
{
get { return _nameCollection.AsEnumerable(); }
}
public void Print()
{
foreach (var item in ReadOnlyNameCollection)
{
Console.WriteLine(item);
}
}
}

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 👇

public IEnumerable<string> ReadOnlyNameCollection
get { return _nameCollection.AsReadOnly(); }
}

--

--

Juan España
ByteHide

CEO at ByteHide🔐, passionate about highly scalable technology businesses and .NET content creator 👨‍💻