-
Sep 21st, 2022, 12:59 AM
#1
Thread Starter
Addicted Member
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.
-
Sep 21st, 2022, 01:36 AM
#2
Re: Why there is Parentheses at the end of the List in C#
What exactly do you not understand about this?
-
Sep 21st, 2022, 01:37 AM
#3
Re: Why there is Parentheses at the end of the List in C#
Originally Posted by colrh
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.
-
Sep 21st, 2022, 10:49 PM
#4
Thread Starter
Addicted Member
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?
-
Sep 21st, 2022, 11:23 PM
#5
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.
-
Sep 22nd, 2022, 06:51 AM
#6
Re: Why there is Parentheses at the end of the List in C#
Originally Posted by colrh
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|