+ 2
How can i use object variable like a type inside this object
https://code.sololearn.com/cPQ58xzxT8oq/?ref=app X & Y can be class or double type but, initially, i don't know what type is it.
4 Respuestas
+ 1
But then why i can do this: x.GetType(), and it return the type of value? If object x = 10, it return System.Int32.
0
Although object is the base type of every object. Object itself is nothing. It needs a class definition to be usefull.
If a object is a house
A class is a building plan
The the object type is a blank building plan.
You can define a villa or a tiny house on it. But if it it is empty you do not know whats it is going to be.
0
This is actually really difficult and theoretically stuff.
Please appreciated that c# is a strongly typed language.
If something is possible it does not mean it is good.
So if something is a int, declare it a int.
The problem shown here is that boxing is implicit and unboxing is explicit.
So GetType shows the type of the boxed value, which is int
But to be able to do concatenation you need this value to be unboxed.
The actual type is still object.
https://social.msdn.microsoft.com/Forums/vstudio/en-US/3368d0d5-eb3e-4ce0-b45a-ad4e67ab79d9/why-does-gettype-of-object-show-as-systemint32?forum=csharpgeneral
https://stackoverflow.com/questions/3447315/boxing-and-unboxing-why-arent-the-outputs-both-system-object
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/boxing-and-unboxing
0
But why I can't do that (x.GetType())x ?