D
D
Denis Bredun2020-07-10 19:49:52
C++ / C#
Denis Bredun, 2020-07-10 19:49:52

Do a string object and a self-created type object have the same structure, given that they are both reference types?

class A
{

}
class Program
{
  public static void Main()
  {
    string a = "99";
    A a2 = new A();
  }
}

I read Richter and Goldstein's book, so I know what an object of my own type looks like, and I know that this object is a reference type, since the class is a reference type. But since string is also a reference type, does it have the same structure as an object of a self-created type? If so, does a string object store its value in a field or something like that? If not, then not an object of type string is created on the heap, but the value of the variable itself, that is, memory is simply allocated on the heap, and one value will be pushed into this memory? But then we would not be able to access methods of type object, since there would be no method table, so they are similar in structure?

I mean, does an object of type string have a pointer to a type object, an index of a synchronization block, a method table that has an EEClass and a pointer to interfaces, etc., etc., as well as an object of type created by myself.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2020-07-10
@Luffy1

All objects are the same and have the same structure in memory, yes, everything that an object has, a string, like arrays, allocate memory on the heap for storing data. It's worth a look https://youtu.be/KbuJdkPLAjw

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question