Coding 101

Aug 21st 2014

Coding 101 31

C# Objects and Classes

The return of C#

Although the show is no longer in production, you can enjoy episodes from the TWiT Archives.
Guests: Louis Maresca
Category: Help & How To

Snubs Compiled

  • Microsoft Visual Studio Express 13 Windows
  • MonoDeveloper OSX

Ivory Tower

Way back in Episode 6 of the first module of Coding 101, we talked a little about "Object Oriented Programming". OOP is an approach to programming that divides the process -- the "problem solving" of a program into modular, reusable pieces.

Before OOP, you wrote your program, then you considered how data might be processed by your code. In OOP, you don't think of data seperate from code -- instead, you think of "Objects" which combine code and data.  You wrote your code, which then processed data. OOP unified the two: instead of having data and code, you have a "object" that combines the two. Instead of having pieces of code that handle sets of data, you now have OBJECTS that model In-Real-Life logic with In-Real-Life sets of data.

In other words... you code problem-solving, instead of process. It was a huge jump.

  • Easier to Understand
  • Easier to Maintain
  • Easier to Upgrade

Now let's talk about CLASSES and OBJECTS

Back in Module 1, we talked about coding functions or "methods".

For Example

public int answer(int a, int b)
{
int c = a + b;
return c;
}

This will create a public function called "answer" that will accept two variables and return an integer value when called. We would call this function by writing:
answer(1,2)
and it would return:
3

That function or "method" combines variables and code that processes those variables.

A CLASS combines multiple "methods" and variables.

  • A "class" is a blue print that can be created multiple times.
  • When the blue print is used to create -- it creates an "OBJECT of that class
  • We can create as many objects of a class, and when we're done with it, it's removed from memory.

IMPORTANT!!! --- Don't Interchange "Class" and "Object"

  • A Class is NOT an Object
  • A Class is the blueprint from which an Object is created.
  • A Class DEFINES an object, but it is not an object itself (Can I repeat the same thing more times?)
  • An OBJECT is NOT a class
  • An object is an INSTANCE of a class (based on that "blue print")
  • An object exists in memory ... a class does not (i.e. a class is not "real" until it is used to create an object of that class.)

Defining a Class is easy:

public class C101
{
//Fields, methods, properties, events... everything in the "blue print" goes in here
}

Creating an OBJECT of a class:

  • We use the keyword "new" to create an instance of a class (aka an "object")
  • In our example, we've created a class named "C101"
  • If we wanted to create an instance of that class (aka an "object") we would write:
    C101 object1 = new C101();

This will create an instance of the class "C101" called "object1". The reason why we write "C101" twice is because we're

  1. Creating an REFERENCE to an object, named "object1" that is based on the class "C101"
  2. Then we're actually CREATING a "new" instance of the class "C101()" that can be referenced with "object1"

Get in Touch With Us!

Bandwidth for Coding 101 is provided by CacheFly.