I do not recommend doing this in C # but you can do it using dynamic
that allows variable has its given type at runtime:
public static bool isNotNull(dynamic AnyTypeValue) {
//tratamentos por tipo
}
Another way is using object
. which is the ancestor of all kinds of language:
public static bool isNotNull(object AnyTypeValue) {
//tratamentos por tipo
}
See this question to better understand the differences between using one form and another. var
has a completely different purpose.
There is still a possibility that I consider much better and recommend it, if possible. It is the use of generic types :
public static bool isNotNull<T>(T AnyTypeValue) {
//não precisa fazer tratamentos por tipo
}
This form maintains the integral typing. In this case the T
will be replaced by the type you send as argument in the method call. I'm not going to explain all the working of generic methods because it's not the focus of the question.
Maybe you even want to restrict it to certain types. If it is going to check if the object is not null then you might want to do this with only classes:
public static bool isNotNull<T>(T AnyTypeValue) where T : class {
//se tiver que fazer tratamento por tipo, algo está errado
}
If you have a specialization, that is, you have to do something specific for each type, so you probably should have several overloading methods for each type.
I would avoid the first two whenever possible, especially the first - it is possible that using object
is not problematic if the method is simple and does things that do not really matter what kind it is. They are even useful in certain situations but they distort the philosophy of language.
You may be accustomed to dynamic languages where this is normal. In static languages, it is not that it is wrong and can not do, but the philosophy is to separate each manipulation into its own method. You should only use a generic method - in the conceptual sense, as opposed to a specific method - if the operation is effectively generic.