0
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. See UseObjectVarible().
    I considered object allocation like this: enter image description here

(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.

  1. 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?

  2. 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?

  3. 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?

  4. 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?

YoungMin Park
  • 1,101
  • 1
  • 10
  • 18
  • 1
    1. It creates a `Person` object and stores it in an `object` variable; but the *object* is still a `Person` 2. `GetType()` asks the *object* what type it is - not the variable; the *object* is a `Person` – Marc Gravell Dec 03 '17 at 01:41
  • @Marc Gravell♦ Do you mean a Person object is located inside of object variable object in the managed heap of the memory? Where do you mean the object variable is in the memory? – YoungMin Park Dec 03 '17 at 01:52
  • Method `ChangeVarDataType()` shouldn't even compile. `var` is just like a regular variable declaration, except that the type is inferred by the variable's initializer. You can't change the type of the variable later in the method. In any case, your questions are all thoroughly answered if you'd do some research, read the documentation, and look at previous similar questions (such as marked duplicates). There is not going to be any way to adequately address all of your misconceptions in the Stack Overflow Q&A environment. – Peter Duniho Dec 03 '17 at 02:15
  • Your question appears to have more to do with understanding the semantics of the .NET Object.GetType() method. MSDN has this explanation of the [Object.GetType method](https://msdn.microsoft.com/en-us/library/system.object.gettype(v=vs.110).aspx) – Chris Carr Dec 03 '17 at 06:39
  • No, an object is never "inside" a variable. An object is allocated on the managed heap and the variable just keeps a reference to it (a reference is basically an address/pointer, but with stronger semantics - although technically any similarity between a reference and a pointer is an implementation detail). The variable here is a "local", so the value of the variable (the reference/pointer) is kept on the stack in this case. The compiler/IL knows the type of the variable, but calling .GetType() isn't asking for the type of the variable... – Marc Gravell Dec 03 '17 at 10:16
  • ...it is saying: *dereference* the variable and ask the *object* (at the other end of the reference) what type of is. Every managed object has a small object header that includes enough information for the object to know it's exact type. – Marc Gravell Dec 03 '17 at 10:17
  • @Marc Gravell Thanks. It helps a lot. – YoungMin Park Dec 03 '17 at 10:53

0 Answers0