6.3. Creating an object with the new operator

Like Java, there is only one way [4] to create a new object in C# – by using the new keyword.

[4] There are two ways to create a new object in C++. Car c(); (creates a Car object on the stack), and Car *c = new Car(); (creates a Car object on the heap). For the case of C#, you can only create an instance of an object using the new keyword. There is no way for the C# developer to control whether a new object is to be created on the heap or stack. All C# objects (reference types) are created on the heap, and all value types are created on the stack.

Like Java

Creating an object in C# is very similar to creating an object in Java. The following statement creates a new object object (object with a small ...

Get From Java to C#: A Developer's Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.