+ 1

Variant for basic value types in C#?

If Object is used to handle an object as it was of any kind (Variant). How do the same for basic types ( int, float, double, etc)

28th Jul 2021, 8:42 AM
Kiwwi#
Kiwwi# - avatar
5 Réponses
30th Jul 2021, 2:29 AM
NosZanou Officiel
NosZanou Officiel - avatar
+ 1
So based on the question, you can have a generic parameter that will accept any value, the way that "object" can be used as a parameter that accepts any type of object. This can be in the following signature. public void AcceptsAny<T>(T anyValue) T is a generic type and this method will accept any value including an object.
2nd Aug 2021, 9:13 PM
Kail Galestorm
Kail Galestorm - avatar
+ 1
A couple notes. You will have to figure out what has been passed in to do any work on it. The reason why "object" works is because it is a primitive type. You can think of it as all objects inherit from "object". But string and int ect are already primitive types. The correct way to accomplish what I think you are trying to do, would be to overload the method for each possible parameter. https://code.sololearn.com/cCFCStzjP0xh/?ref=app
2nd Aug 2021, 9:30 PM
Kail Galestorm
Kail Galestorm - avatar
0
Kail Galestorm when asked that, was trying to do something like use an array hosting one value of every primitive type.l and iterate it within a loop to pass it calling a generic method to operate with. I was not aware that even primitive value types inherit from object too, so it solves the case. Also there's ValueType too. Thx for answer anyways.
2nd Aug 2021, 11:46 PM
Kiwwi#
Kiwwi# - avatar
0
Kail Galestorm, What did you mean by "string and int already are primitive types"?
3rd Aug 2021, 12:26 AM
Ipang