myCar.petName = "Fred";
}
To correctly create a class type variable, you may define and allocate a Car object on a single
line of code:
static void Main(string[] args)
{
Car myCar = new Car();
myCar.petName = "Fred";
}
As an alternative, if you wish to define and allocate an object on separate lines of code, you
may do so as follows:
static void Main(string[] args)
{
Car myCar;
myCar = new Car();
myCar.petName = "Fred";
}
Here, the first code statement simply declares a reference to a yet-to-be-determined Car object.
It is not until you assign a reference to an object via the new keyword that
this reference points to a
valid class instance in memory.
In any case, at this point we have a trivial class type that defines a few points of data and some
basic methods. To enhance the functionality of the current Car type, we need to understand the role
of
class constructors.
Do'stlaringiz bilan baham: