DEV Community

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

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

5 (Basic) Tips in C# (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.


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

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

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

_public void RunDataOperation()  
{  
try  
{  
Intialize();  
ConnectDatabase();  
Execute();  
}  
catch (Exception)  
{  
throw;  
}  
}_
Enter fullscreen mode Exit fullscreen mode

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

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

Or even at the method level👇

_class MyGenricClass  
{  
public void MyGenericMethod<T>(T t) where T : IMyInterface  
{  
//_ Generic implementation would go here_  
}  
}_
Enter fullscreen mode Exit fullscreen mode

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

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

Top comments (1)

Collapse
 
jonasbr68 profile image
Jonas Brandel

Flag example for enum is really bad advice. It is to be used when your enum values are bit-flag or combination thereof.