C# – Using reflection to get properties from subclass and base class

I came across this scenario recently where I needed to use reflection to get the properties declared in a subclass, and then get the properties declared in the base class. I had to handle these sets of properties differently. Here’s an example. Consider the following two classes. PersonBase is the base class. Driver is the … Read more

C# – Enum generic type constraint

In C# 7.3, Microsoft added the ability to specify an Enum as a generic constraint, like this: Whenever you have a generic method, it’s a good idea to use generic type constraints. Without constraints, you would have to implement type checking in the generic method and throw exceptions if an invalid type was used. With … Read more

C# – Cannot convert null to type parameter ‘T’ because it could be a non-nullable value type

Problem You’re trying to return null from a generic method and you’re getting the following compiler error: Cannot convert null to type parameter ‘T’ because it could be a non-nullable value type. Consider using ‘default(T)’ instead You can’t return null because the compiler doesn’t know if T is nullable. Solution There are a few options … Read more