static void UseObjectVarible()
{
object o = new Person() { FirstName = "Mike", LastName = "Larson" };
Console.WriteLine(o.GetType());
}
static void ChangeDynamicDataType()
{
dynamic t = "Hello!";
Console.WriteLine("t is of type: {0}", t.GetType());
t = false;
Console.WriteLine("t is of type: {0}", t.GetType());
t = new List<int>();
Console.WriteLine("t is of type: {0}", t.GetType());
}
static void ChangeObjectDataType()
{
object t = "Hello!";
Console.WriteLine("t is of type: {0}", t.GetType());
t = false;
Console.WriteLine("t is of type: {0}", t.GetType());
t = new List<int>();
Console.WriteLine("t is of type: {0}", t.GetType());
}
static void ChangeVarDataType()
{
var t = "Hello!";
Console.WriteLine("t is of type: {0}", t.GetType());
t = false;
Console.WriteLine("t is of type: {0}", t.GetType());
t = new List<int>();
Console.WriteLine("t is of type: {0}", t.GetType());
}
(1)On the illustration, is there something wrong?
(2)I expected this one, Console.WriteLine(o.GetType());
should be Object type, but it shows MyProjectNamespace.Person type.
See, ChangeDynamicDataType()
(1)When I mouse over each t which is assigned by "Hello", it shows dynamic type(dynamic t). But the result of t.GetType() is String. Before compile, can I say t is always dynamic type like IntelliSense shows? And after compile, can I say type of t gets implicit type casted to the type of value which is assigned to t?See, ChangeObjectDataType()
Similar to the 1. I expected the result like this:
Object, Object, Object. But it shows, String, Boolean, Generic.
Why is my expectation wrong?See, ChangeVarDataType()
When I mouse over each t, I can see the type is already implicit casted like (string t, boolean t, List t). So can I conclusively say it means that in the case of var, before even a compile, the type of variable gets implicit casted based on the type of value which is assigned to the variable t?In the method, there are these declaration for local variable:
dynamic t, object t var t.
Where are they allocated in the memory?
Especially, in the case of object t, object type is reference type, so under my understading, it needs new keyword to allocate object in the managed heap like this:
Object t = new Object();
How is it possible to allocate object type variable (maybe in the heap of memory) without new keyword?