Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is gcnew?

Tags:

.net

c++-cli

I stumbled across this code and am too proud to go and ask the author what it means.

Hashtable^ tempHash = gcnew Hashtable(iterators_);  IDictionaryEnumerator^ enumerator = tempHash->GetEnumerator(); 

What is gcnew and how important is it to use that instead of simply new? (I'm also stumped by the caret; I asked about that over here.)

like image 726
Owen Avatar asked Oct 14 '08 19:10

Owen


People also ask

Does Gcnew need delete?

No, the garbage collector will delete it.

What is managed C++ code?

Managed C++ is a language invented by Microsoft, that compiles to bytecode run by the . NET Framework. It uses mostly the same syntax as C++ (hence the name) but is compiled in the same way as C# or VB.NET; basically only the syntax changes, e.g. using '->' to point to a member of an object (instead of '.

What is ref class in C++?

A ref class can have standard C++ types, including const types, in any private , internal , or protected private members. Public ref classes that have type parameters are not permitted. User-defined generic ref classes are not permitted. A private, internal, or protected private ref class may be a template.


2 Answers

gcnew is for .NET reference objects; objects created with gcnew are automatically garbage-collected; it is important to use gcnew with CLR types

like image 184
Steven A. Lowe Avatar answered Sep 23 '22 13:09

Steven A. Lowe


gcnew is an operator, just like the new operator, except you don't need to delete anything created with it; it's garbage collected. You use gcnew for creating .Net managed types, and new for creating unmanaged types.

like image 23
Joel Coehoorn Avatar answered Sep 24 '22 13:09

Joel Coehoorn