Is there a way to check if a variable is value type of reference type?
Imagine:
private object GetSomething<T>(params T[] values)
{
foreach (var value in values)
{
bool is ValueType; // Check if 'value' is a value type or reference type
}
}
You can get the Type that represents T , and use the IsInterface property: Type type = typeof(T); if (type. IsInterface) { ... } If you want to know which interface is passed, just use == to compare the Type objects, e.g.
Generics means parameterized types. The idea is to allow type (Integer, String, … etc., and user-defined types) to be a parameter to methods, classes, and interfaces. Using Generics, it is possible to create classes that work with different data types.
An attribute cannot inherit from a generic class, nor can a generic class inherit from an attribute.
bool isValueType = typeof(T).IsValueType;
Job done... it doesn't matter if any of the values is null
, and it works even for an empty array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With