C
C#14mo ago
Re'Seania

What does code "new" means.

I would like to know what it means.
33 Replies
Moods
Moods14mo ago
Wdym? Do you have a code snippet to show? It depends on the context
Re'Seania
Re'SeaniaOP14mo ago
like, I have a hard time understanding what it does, I get the gist of it but I don't know
Moods
Moods14mo ago
Well I asked you to be more specific cus the keyword can be applied differently depending on the context, but I’ll assume you’re referring to the most common usage, which is the new operator Well, i don’t really know what else to say, but it’s used alongside a type’s constructor to create a new instance of this type
Re'Seania
Re'SeaniaOP14mo ago
oh like variables example: string smth = new string[];
Moods
Moods14mo ago
Something like
List<int> bar = new List<int>();
List<int> bar = new List<int>();
Here the new keyword is being used alongside the default constructor for List<int> to create a new instance of List<int> and to return a reference to it to the variable foo
Re'Seania
Re'SeaniaOP14mo ago
what does it do in the line of code liek like*
Moods
Moods14mo ago
It creates the instance of the type, what data the instance will have will depend on what constructor is used Which is usually after the new keyword
Re'Seania
Re'SeaniaOP14mo ago
erhhhhhhhh I am stupid so it just like, makes it smth that's not the other thing
Moods
Moods14mo ago
Also it should be string[] smth = new string[insert a number here];
Re'Seania
Re'SeaniaOP14mo ago
ik I am tired
Moods
Moods14mo ago
Sorry
Re'Seania
Re'SeaniaOP14mo ago
not ur fault My choice to sleep deprive myself over this
Moods
Moods14mo ago
Okay okay, depending on whatever constructor is used after the keyword, it will return an instance of whatever type the constructor belongs to
Re'Seania
Re'SeaniaOP14mo ago
uhhh what is the contrustor? I forgot
Moods
Moods14mo ago
What comes after new, it’s a special function that’s used to initialize instance data
Re'Seania
Re'SeaniaOP14mo ago
ohhhhh so like how much it holds or smth ?
Moods
Moods14mo ago
The constructor you use determines what data the instance will hold initially So not how much, but what it holds
Re'Seania
Re'SeaniaOP14mo ago
oh so like think of it as a container?
Moods
Moods14mo ago
The instance is the container, the constructor determines what’s in the container initially And the new keyword creates the container
Re'Seania
Re'SeaniaOP14mo ago
ok so sorry for wasting your time but
Moods
Moods14mo ago
Nah don’t apologize I feel bad I am not explaining better I can send documents if they’d help Official Microsoft ones
Re'Seania
Re'SeaniaOP14mo ago
No your explaining fine I just like, have harder time understanding bigger words like for a example, you said did The outrageous courageous lion decided to do a malicious deed to capture it's prey. I'd probably need to hear it again to understand
Moods
Moods14mo ago
Oh? I did not say that unless I am missing something
Re'Seania
Re'SeaniaOP14mo ago
figuratively
Moods
Moods14mo ago
Oh you’re saying I said a lot of stuff that isn’t easy to understand?
Re'Seania
Re'SeaniaOP14mo ago
yup
Moods
Moods14mo ago
Ah I see
Re'Seania
Re'SeaniaOP14mo ago
ok thx one more thing Like stuff like this: Represents the standard input, output, and error streams for console applications. This class cannot be inherited. I could not understand unless I built a personal meaning of it in my mind.
Moods
Moods14mo ago
Hmmm, you’d have to start from scratch to build a foundation to understand stuff like that
Re'Seania
Re'SeaniaOP14mo ago
that's why I make notes it's like a training book specifically for me to help ME understand the best way to understand it.
Moods
Moods14mo ago
I think that’s good then if it helps you understand better It’s easy to get lost though so don’t be too hard on yourself
Re'Seania
Re'SeaniaOP14mo ago
Or maybe I'm just slow. 🥱😭

Did you find this page helpful?