What is the difference to instantiate an integer using new and without new?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: What is the difference to instantiate an integer using new and without new?

  1. #1
    Join Date
    Jul 2005
    Posts
    894

    What is the difference to instantiate an integer using new and without new?

    For example,
    Code:
    int i = 0;
    
    int i = new int(0);
    Is there any difference? Thanks.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,964

    Re: What is the difference to instantiate an integer using new and without new?

    What do you think the differences are, if any? Btw, a basic C# book will explain this too you better than posting to a forum.

  3. #3
    Join Date
    Aug 2005
    Posts
    198

    Re: What is the difference to instantiate an integer using new and without new?

    There is absolutely no difference.
    David Anton
    Convert between VB, C#, C++, & Java
    www.tangiblesoftwaresolutions.com
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

  4. #4
    Join Date
    Jul 2005
    Posts
    894

    Re: What is the difference to instantiate an integer using new and without new?

    Quote Originally Posted by David Anton View Post
    There is absolutely no difference.
    This is quoted from MSDN,
    new operator Used to create objects on the heap and invoke constructors.
    Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap.

    Since integer is value type so it must be created on the stack. But on the other hand, new operator will create object on the heap if we are using int i = new int(0). It looks like conflicting. Correct me if I am wrong. Thanks.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    10,964

    Re: What is the difference to instantiate an integer using new and without new?

    Quote Originally Posted by LarryChen View Post
    This is quoted from MSDN,
    new operator Used to create objects on the heap and invoke constructors.
    Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap.

    Since integer is value type so it must be created on the stack. But on the other hand, new operator will create object on the heap if we are using int i = new int(0). It looks like conflicting. Correct me if I am wrong. Thanks.
    Why not use the built-in profiler to see what is really happening?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center