What is actually the difference between System::String^ ex; and System::String ex1;?
I found out that the one with “^” means that it is top-level. But what does that mean?
What is actually the difference between System::String^ ex; and System::String ex1; ? I found
Share
System::String^is a reference to a managed string object,System::Stringis a managed string object directly on the stack or inline in another class.As mentioned in What does the caret (‘^’) mean in C++/CLI?, the
^is a tracking reference, roughly equivalent to*for a pointer in unmanaged code. In the same way you can haveunmanagedClass* foo1;andunmanagedClass foo2;, you can haveSystem::String^ str1;andSystem::String str2;When used without the
^, it follows the same rules as an unmanaged class without the*: Access methods on it with a.not a->. Automatically cleaned up when it leaves scope (destructor in unmanaged, dispose method in managed).One thing that does make working with managed objects without their
^harder is that most managed objects don’t define a copy constructor or the equals operator. Neither of those would be used in C# or VB, so they generally aren’t implemented. Without them, it’s impossible to assign a new value to a variable without a^, so you’re generally limited to constructing just a single object.