Results 1 to 6 of 6

Thread: Why there is Parentheses at the end of the List in C#

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    152

    Why there is Parentheses at the end of the List in C#

    Hi.
    In continuation to my learning, I've reached to List<T>.

    I was looking around some websites and couple of Tutorials where I learned to create object of the List.
    Here is mine:
    Code:
    List<int> numberList = new List<int>();
    He said (in tutorial) that its some kind of constructor? No one explained in different websites, couple of them, I didn't understand too..


    Why parenthesis are used?
    Somewhere I've found that curly brackets { } are used.

    What is this constructor thing in this parentheses?

    Thank you very much for any help and devotion of time.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,861

    Re: Why there is Parentheses at the end of the List in C#

    What exactly do you not understand about this?

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,861

    Re: Why there is Parentheses at the end of the List in C#

    Quote Originally Posted by colrh View Post
    Somewhere I've found that curly brackets { } are used.
    That would be an object initialiser. When you use an object initialiser along with a parameterless constructor, you can include the parentheses or omit them.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2016
    Posts
    152

    Re: Why there is Parentheses at the end of the List in C#

    The only thing which I understood with this website and few others I visited is that:
    Default Constructor is created by the C# by itself, when you the programmer does not create a constructor. The Default Constructor initializes the numbers with Zero (0) and Strings with Null.

    And your link provided this point too that:
    If you want to create your own Default constructor then leave the parentheses empty.

    Now the question:
    The code I mentioned
    Code:
    List<int> numberList = new List<int>();
    Does this code refers (if I inferred it correctly) that numberList will be inintialized with 0?
    Is that OK?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    108,861

    Re: Why there is Parentheses at the end of the List in C#

    You know how to call methods in C#, right? A constructor is basically just a method. Do you use parentheses when calling any other method? Yes you do, so why would you not when calling a constructor? Do you leave the parentheses empty when there are no parameters and fill them with arguments when there are parameters when calling any other method? Yes you do, so why would you not when calling a constructor? You're trying to turn something simple into something complex. You use parentheses when calling a method and constructors are methods.

  6. #6
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,921

    Re: Why there is Parentheses at the end of the List in C#

    Quote Originally Posted by colrh View Post
    The only thing which I understood with this website and few others I visited is that:
    Default Constructor is created by the C# by itself, when you the programmer does not create a constructor. The Default Constructor initializes the numbers with Zero (0) and Strings with Null.

    And your link provided this point too that:
    If you want to create your own Default constructor then leave the parentheses empty.

    Now the question:
    The code I mentioned
    Code:
    List<int> numberList = new List<int>();
    Does this code refers (if I inferred it correctly) that numberList will be inintialized with 0?
    Is that OK?
    No...
    Think of it like this:
    List<int> numberList. says "I need a box that can hold ints" ... it's a signal of something that you need. But it doesn't create it yet.
    new List<int>(); says "Ok, that box I needed, go make it for me now" ... This actually builds the box. The box is empty, and so is your list. What you now have is something that can hold ints.
    Since numberList is a list, it's just simply instanciated and ready to hold what ever ints you add to it. It is not initialized to 0.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

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



Click Here to Expand Forum to Full Width