Confused with the 'new' function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Confused with the 'new' function

I'm currently on the third lesson about classes in C # but I don't get it in the slightest. All I'm getting at here is that; first you have to declare the class however you can't just access it straight away(?) so first you have to initialize it by assigning it to a placeholder (?) by using the new function. Now then if you want to access it you have to reference the placeholder and then you can access the class like this Person.SayHi (); (?) Please tell me if I'm getting this or not at all. Thank you :)

7th Apr 2017, 8:20 AM
Nutty
Nutty - avatar
4 Answers
+ 1
class is a blueprint. like a car. in the factory a new car is produced from a blueprint. this one is red. another one done by the same factory is maybe green. another one is black. in this analogy the class is the blueprint. the new keyword is the factory. and the blue, green and black car are INSTANCES OF the class (=objects). Each instance has its own state (color=green or whatever). You save the reference to the object into a variable and can access the color via e.g methods (greenCar.getColor () ) That's the gist of it
7th Apr 2017, 8:30 AM
Petja Boigk
Petja Boigk - avatar
+ 1
yes
7th Apr 2017, 8:34 AM
Petja Boigk
Petja Boigk - avatar
0
So basically I initialize the class I place it into a variable using the 'new' function and from there I can access it.
7th Apr 2017, 8:33 AM
Nutty
Nutty - avatar
0
That explains it perfectly thank you.
7th Apr 2017, 8:41 AM
Nutty
Nutty - avatar