-1

Here's an example of what I'm talking about.

class Car()
{
    int speed = 50;
}

Now imagine in some other file, you do

Car truck;

How is that different from

Car truck = new car();

I've been using C# for a little over a year, but still don't get this. It's been an on and off type of thing.

Ovicior
  • 19
  • 3
  • 4
    `new` causes a new instance of the class of type `car` to be instantiated. Your code also assigns that instance to the variable called `truck`. – Eric J. Feb 03 '16 at 00:21
  • `Car truck;` is just a declaration. `Car truck = new car()` declares and initialises `truck` to point to a `car` instance. – Lee Feb 03 '16 at 00:21
  • 8
    I'm voting to close this question as off-topic because seriously, google prior to asking on SO. https://msdn.microsoft.com/nl-be/library/fa0ab757.aspx – Jeroen Vannevel Feb 03 '16 at 00:24
  • 2
    Is there somewhere in the SO rules that explain how this is off-topic? I agree it's a simple question, but we all started somewhere. – moswald Feb 03 '16 at 00:28
  • 1
    And my answer gets nucked by put on hold... While it is simple it is certainly something that can be answered. – Guvante Feb 03 '16 at 00:39
  • 2
    Down-votes; closed as _"does not appear to be about programming"_. How is this any different to say _[Can somebody please explain async / await?](http://stackoverflow.com/questions/14177891/can-somebody-please-explain-async-await)_ bearing in mind _[Are “beginner questions” allowed on Stackoverflow?](http://meta.stackexchange.com/questions/152066/are-beginner-questions-allowed-on-stackoverflow)_ –  Feb 03 '16 at 00:54

2 Answers2

1

Car truck; doesn't do anything. All you're saying with that line of code is that you may or may not eventually have an object of type Car, and you'll want to refer to it as truck.

Car truck = new Car(); declares that you've want to refer to a Car with the name truck (just like before), but then you also give that name to an actual Car object that you are instantiating in place.

To continue the car analogy with a (somewhat flawed) concept: Car truck; is a parking spot, Car truck = new Car(); is a parking spot you just built a new car in.

moswald
  • 11,491
  • 7
  • 52
  • 78
1

Let me first say that whether it is a class or struct matters heavily in how this all works out, but for now I will ignore struct to simplify things:

Now imagine in some other file, you do
Car truck;

This creates a variable (or field in some cases) that could contain a Car instance, however it does not create such an instance. Note that if it is actually a field (because it is in a class definition and not in a method) then it will get the default value null. If it is a local variable then attempting to access it is a compiler error.

This isn't an instance of Car and attempting to treat it as such will cause an error to occur unless it is a field and you are calling a method that will accept a null.

How is that different from
Car truck = new car();

By putting the = new Car(); before the ; you are telling the language to give you an instance using the default constructor (that is a term for the () constructor). This causes a new instance of the class to be created and the constructor you provided to be called to initialize that new instance.

Note that in this case you have a valid Car that will work as you expect, compared to the previous one where you did not have one at all.

In short if you want a place to hold a Car use the first definition, if you actually want a car, use the second.


Now I am going to expand on struct, it is important to keep in mind but they aren't nearly as common as classes so you can focus on the above.

Now imagine in some other file, you do
StructCar truck;
How is that different from
StructCar truck = new StructCar();

These are now identical, they both create a variable or field and calls the default constructor for the StructCar. This will in turn set each field to its default value (calling the default constructor on any struct and setting any class to null). This is a perfectly valid StructCar but be careful, because every time you give it to someone (by saying calling a method and passing it as an argument) they will get a copy, not the original. Also note that in the definition of StructCar you cannot define a default constructor, it will also do what I am describing here.

Guvante
  • 18,775
  • 1
  • 33
  • 64