When do i use new ?
I'm new to programing and i wanna know when to write new.
For example:
x = y; or x = new y;
Can someone explain what new means please ?
Answer by Socapex · Oct 26, 2015 at 08:51 PM
Ok let me simplify this as much as possible. First of all, I highly recommend you watch and follow the Unity beginner programmer videos. They are definitely going to help you, it is also a very clean, consistent and FREE way to learn programming :)
new
is used for creating objects. Your example doesn't really make sense because y is a variable, not a type. Also you didn't provide us with what type you are using.
So, if you can do x = y;
, you definitely cannot do x = new y;
(also note that should by x = new y();
).
In C#, you use new when creating objects. It is a garbage collected language, which means you do not have to release memory yourself. Issues of stack vs. heap are a more advanced concept that I would not worry too much for now, until you get a good grasp on the language. The notation comes from C++, but is used to make it clear what you want to do. Class1();
could be a function or a method call, new Class1();
makes it clear you are initializing an object.
Simply put: Use new to create objects. MyObject potato = new MyObject();
If you want to assign an object, like in your example, then you do not use new as the object is already created. You are simply copying the object and it's values potato = tomato;
. Note that the reality is a little more complex, but in practice this will work as you expect.
Once you understand more concepts of programming, scope, initializing your objects etc. Then I would google various topics related to: C# references, C# new, C# structs, C# classes, C# stack heap, C# initialization. These should all be related to what you are asking, but are a little bit too advanced for now I believe.
Some doc: https://msdn.microsoft.com/en-us/library/fa0ab757.aspx http://stackoverflow.com/a/6972155 http://stackoverflow.com/a/6531692 http://stackoverflow.com/questions/433591/why-do-c-sharp-and-java-bother-with-the-new-operator
Great article on references and stack/heap. I would read the first part at least: http://www.albahari.com/valuevsreftypes.aspx
No problem :) The "new" and similar concepts are hard to understand at first. Take it one step at a time ;)
Also, remember that C# isn't only used for Unity. That means you have a plethora of great information and learning resources available on the $$anonymous$$icrosoft website, stack overflow, various forums, blogs etc. One thing to keep in $$anonymous$$d though, when doing your research, Unity uses C# 2.0, so some fancy things don't always work.
Best of luck in your program$$anonymous$$g learning! :D
Thx, oh and by do way i knew just a few things from C++, that's why i decided to go for C# and not for JavaScript because its more similar.
This is where i was co$$anonymous$$g from i didn't understand why myOtherClass = new AnotherClass(); So if i understand right, this means that myOtherClass is just a copy of a public class AnotherClass ?
private AnotherClass myOtherClass;
void Start ()
{
alpha = 29;
myOtherClass = new AnotherClass();
myOtherClass.Fruit$$anonymous$$achine(alpha, myOtherClass.apples);
}
I'm not sure I understand your question 100% correctly, but myOtherClass becomes an initialized object. It is a "thing", that thing is what you put inside AnotherClass.
It is a "copy" in a way, by that I mean it contains everything you put when you declared that class. In other words, if AnotherClass contains a few ints, a few strings and some floats, then myOtherClass contains those.
Just to make it clear, it is important you understand that declaring a class (`public class Potato{...}`) doesn't do anything in and of itself. You need to initialize objects of that class somewhere. In Unity this is less clear, because when you inherit $$anonymous$$onoBehavior and attach your script to a prefab (inside the editor), than it will create an object for you.
To sum it up, in your example, myOtherClass is declared as going to be an object of type AnotherClass. Then when you do the new
, it is initialized and "becomes" that actual object, which lives in your computer memory somewhere. You can now do stuff with it :)
Hope this is clear?
[edit] @JanZagar, I agree that was a good decision :) C# is also useful because you can easily use for any type of other projects/applications. That means what you learn with Unity can be applied for other things. Also, learning C# will help you if you ever want to learn Java, and even go deeper in C++ and (omg) C ;)
Your answer
Follow this Question
Related Questions
JavaScript taken from the web isn't working. Help 0 Answers
Steam VR one object interaction using both controllers 0 Answers
Terrain editor broken? 0 Answers
Make the player go from a hook to another hook 0 Answers
Really basic question... 1 Answer