+ 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)
5 Réponses
+ 1
You Can use extension
https://code.sololearn.com/cufa0XF0PvD2/?ref=app
+ 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.
+ 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
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.
0
Kail Galestorm,
What did you mean by "string and int already are primitive types"?