The most important part of this example is the first two lines of code. First of all we dimension a variable myCar that is of type Car. If you remember back to video lesson 4 we worked with simple value types like strings and integers. Now we are working with more complex reference types, AND we are creating our own custom types!
The second line of code is where the magic actually happens. In line 1 we created a variable of type Car, but as of yet it is not actually a Car. At the moment we use the new keyword, a Car object is created in memory and we store a reference to that new Car in the myCar variable. Let me clarify this. All the information about the newly created Car object is stored in your computer’s memory. The variable myCar is given the address of that memory space, which is called a “reference”. Now, whenever you use the word “myCar” in code, it just refers the compiler to the memory address that represents that instance of the Car object.
There's a subtle difference between dimensioning a variable as a type (or class) and actually creating an instance of that type (or class). This will become more apparent as we talk about Class Constructors a little bit later on.
Almost everything you work with in C#, whether you know it or not, is a Class. For example, look at the code examples from the previous lessons.
There is a lot more to learn, but in this lesson we've introduced Object Oriented Programming and have shown some fundamentals about classes.
Let’s now look at what goes inside of Classes, namely Fields, Properties and Methods.
Supplemental Readings for the Express Edition Videos Copyright © 2005 LearnVisualStudio.NET