Parameter of any type in method

Asked

Viewed 543 times

5

I am building a method and would like it to extend into any type. For now I am using string but I would like the same to work for int, float, double and datetime.

public static bool isNotNull(string AnyTypeValue) // nao funcionar var
{

   //tratamentos por tipo
}
  • It depends on what the method does, and how it will be used. Without more context, it is impossible to help. Of course, the method isNotNull is fictitious and not the actual code.

  • Yeah, certainly check whether a int is null doesn’t make much sense.

  • check if int is not = 0, use this method to make a kind of validation for all form data before giving input in the database

  • Null value and zero value are very different things. Of course you can make this comparison but it is a mixture of concepts. In the example you gave it can be a huge problem not allow insertion of value 0 in the database.

  • I understand, but in the cases we use here we can not enter 0 or Null. The idea of the method is to be able to check if any item is a valid date, or if the numeric value is >0 or if the string is not null/empty/empty space

1 answer

9


I don’t recommend doing this in C# but you can do it using dynamic that allows the variable to have its type determined at runtime:

public static bool isNotNull(dynamic AnyTypeValue) {
   //tratamentos por tipo
}

Another way is by using object who is the ancestor of all kinds of language:

public static bool isNotNull(object AnyTypeValue) {
   //tratamentos por tipo
}

Behold that question to better understand the differences between the use of one form and the other. The 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 keeps the whole typing. In this case the T will be replaced by the type you send as argument in the method call. I will not explain the whole operation of me all generic because it is not the focus of the question.

Maybe you even want to narrow it down to certain types. If you are going to check that the object is not null, then you might want to do this only with classes:

public static bool isNotNull<T>(T AnyTypeValue) where T : class {
   //se tiver que fazer tratamento por tipo, algo está errado
}

I put in the Github for future reference.

If you have a specialization, that is, you have to do something specific for each type, then you should probably have several overload methods for each type.

I would avoid the first two whenever possible, especially the first - it is possible that the use of object nor is it problematic if the method is simple and does things that don’t really matter what type it is. They are even useful in certain situations but distort the philosophy of language.

It is possible that you are used to dynamic languages where this is normal. In static languages, it’s not that I’m wrong and I can’t do it, 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.

  • Excellent, finally consider citing the use of overload method.

  • I was just starting to do this.

  • Thank you very much, you improved the quality of what you were doing by 100%

  • Method overload did not know. Sensational this methodology

Browser other questions tagged

You are not signed in. Login or sign up in order to post.