Hello everyone,
I was learning coding for quite a while, i reached (at last) the point where i understand what i read and now i would like to practice coding.
But there is one thing i cant understand.
When to use the word “new”
I ve read that it creates a new object. but why do we NEED to create new object? I understand we need Variables and Functions.
for example, why do we write “new Rect” and not just “Rect”
Could someone explain me with an example? I ve read it many times in programming language but i cant see the use of it 
Thanks in advance.
Because C# is an Object-Oriented Language(Look up for ‘OOP’).
Why ‘new Rect’ instead of ‘Rect’?
Because you’re creating a new ‘Rect’. (Duh? :P)
You’re creating a new instance of an Object, with it’s own properties and stuff.
If you want a class that can be accessed by any class, use the ‘static’ keyword.
If it bothers you that much, use Unityscript, where “new” is optional in cases like “new Rect” (though you still need to use it for things like “x = new int[55]”).
–Eric
The “new” keyword tells the computer to create an instance of a class or struct in memory. In other programming languages its presence determines where in memory to create the instance, but in C# that is not the case as it is based entirely on whether it’s a class or a struct.
@miguelvesga i know it create instance object. but i was wondering what the purpose of creating it one is.
@Eric5h5 i started with JS, but i would like to focus with C# due to the flexibility. Thats the reason why im breaking my head to understand how it works 
@angrypenguin So in other words, by writing “new” its just to save a class/struct in memory (make an instance of it) for future use. I though new it was obligatory in some cases.
Could you give me a small example where would you and where you wouldnt use the “new” and why?
(started making sense. thank you all
)
Rect myRect;
declares a variable to use in your code. this variable is just a reference pointing at nowhere (null).
myRect = new Rect(20,20,50,50);
now your variable is initialized and refers to an object.
myRect = new Rect(100,100, 20,20);
or
Rect tmpRect = new Rect(100,100, 20,20);
myRect = tmpRect;
now your variable refers to another object andt he old one is released for garabge collection if it has no other references on it.
so basically your variable is only a reference (pointer in c++ fe) and you tell what it should contain ie which object it should be referenced by it. this way is much more clear and consistent imo than handling variables and pointers differently like in c++.
you must always use new when you want to use a variable (reference) as they start uninitialized (you get null reference exception otherwise).
you must use new when you want to constructor be executed for some reason.
you don’t use new when you assign an initialized variable to another (like my last exeample).
hint:
struct = struct gains you 2 variables with the same values in it. it copies the object and both are on the stack.
class = class gains you 2 variables pointing to the same object on the heap.
i suggest reading this.
That’s incorrect, since a Rect is a struct, so it always has a default value and can’t be null.
–Eric
yes you are right. sorry, my fault.
so a Rect is initialized already and instead of assigning a new Rect you could also assign the values directly:
Rect myRect;
myRect.width = 100;
sorry again.
I see. thank you all 
Is there any way i could give you credits? your explanations were really good 
Thanks again.
I hope this thread will help the newcomers too
(Page Not Found, but im aware of the C Yellow Book for C#. it will be my next book once i finish the one im on atm)
Thank you EliteMossy, its a great site 
I think it will come very handy, and not just for the “new”