Posts Tagged ‘ Creating Classes ’

Creating Classes…

…and understanding them.

First I created a class called “Car”. Inside that class I typed the word “”prop”:

By using the Tab key on my keyboard twice a macro in Visual Studio, called the Code Snippet, is executed and it creates this line of code:

Actually I have to change something in that code. Visual Studio allows me to replace parts of this line with my own code. I am going to change “int” to “string” and give it a name:

This is called Code Snippet plus Auto Implemented Property Technique. I am using this technique a few more times to create four properties in my example:

Basically, what I did, was creating a blueprint for a car. So far I have just the properties of a car. So I am going to create a few methods, too:


“public” simply means that it is accessible from outside this code block. Meaning, as long as I have a reference to a car, an instance to a car, I’ll be able to access these properties and methods. They’re not private, they’re public.

Classes and objects are two different things. The class is the blueprint, the plan. An object is an instance of a class.

Let’s assume I want to add a new car to my list each time I press the button:

“myNewCar” is an instance of the car class. To access each property of the car class that I am going to set I just use the period operator and IntelliSense is showing me all the properties I can set.

Let’s move on and create a simple helper method:

Since I created and defined the car class earlier, I can now use the class name ”Car” as an input parameter.

What I want to do now is calling my helper method inside my button click event:


What I’ve learned from this little example is the following:

First of all, a class is a basic building block of C#. Almost everything in the .NET Framework is a class.

Second, classes are just the blueprints. I have to create a new instance of that blueprint, an object.

Third, classes can define properties and methods. Properties are attributes of the class, methods are actions or behaviors of that class.

Fourth, when I define a class, I actually create a custom data type.


To be continued…

%d bloggers like this: