DEV Community

Cover image for C# Classes, Objects, and Class Members Explained by Dan Harmon's "Community" | Dear Coder
Kasey Wahl
Kasey Wahl

Posted on

C# Classes, Objects, and Class Members Explained by Dan Harmon's "Community" | Dear Coder

Dear Coder,

If you're a complete beginner or transitioning into object-oriented programming (OOP) you might have already discovered that classes, objects, and class members are EVERYTHING. If you're anything like me, you might find that dizzying at first.

But don't worry, Coder. We got this with a little help from the folks at Greendale Community College. After all, you know what they say at Greendale: coding is nothing but the abstraction of American pop culture references.

Er, something like that.


What is OOP?

Procedural programming emphasizes writing procedures or methods that perform operations on data. Object-oriented programming is about creating objects that contain data and procedures.

In Pierce Hawthorne terms, OOP is about THINGS doing stuff and procedural is about DOING stuff to things. Nouny code vs. verby code.


Pierce trying to figure out OOP

For us OOP people, that means we need to understand how to create objects out of words so we can describe what they do and how they're supposed to do it. We do that using classes, objects, and class members.


Classes

Classes and objects are the terms we use code a thing into existence. But what do they look like, and how can we use them in our code? Say hello to our ethnically neutral class-cot:


class HumanBeing { }

A class is the broadest definition of a thing. In this case, we'll use it to create a HumanBeing using the "class" keyword.

class HumanBeing
{}
Enter fullscreen mode Exit fullscreen mode

There you have it: a human being.

Sorta.

But we've only declared that HumanBeing exists. What kind of features define ANY HumanBeing?


Class Members

We've declared a class of HumanBeing, so now let's give it a little shape using fields and methods.

class HumanBeing
{
public string Name; // field
public int Age; // field
public string Major; // field
public bool InStudyGroup; //field
public void tellsJoke(); // method
}
Enter fullscreen mode Exit fullscreen mode

The fields in our HumanBeing object are pieces of data that describe what a HumanBeing is. They're the "adjectives" and "nouns" that give it shape.

The tellsJoke() method is a procedural, executable piece of code that allows our HumanBeing to do something. Community is a funny show. The HumanBeings at Greendale all use the tellsJoke() method.

And that's an important thing to note: all of the characters in Community are HumanBeings, but not all HumanBeings are on the show Community.

Duh, right?

Good! You already understand the fundamental difference between classes and objects.


Objects

Every ounce of my Millenial angst adores Pierce Hawthorne, but how do I represent him in code?

The good news is, we're already halfway there. We'll use our HumanBeing template (class) to create a single instance of HumanBeing (object) called "Pierce" with dot notation.

 class HumanBeing
{
public string Name; // field
public int Age; // field
public string Major; // field
public bool InStudyGroup; //field
public void tellsJoke(); // method

static void Main(string[] args)
   {
   HumanBeing Pierce = new HumanBeing();
   Pierce.Name = "Pierce Hawthorne";
   Pierce.Age = 65;
   Pierce.Major = "undeclared";
   Pierce.InStudyGroup = true;
   }
}
Enter fullscreen mode Exit fullscreen mode

Now we've given some life to one instance of our HumanBeing class. He looks something like this. Can you see him?

HumanBeing Pierce

We can use the same HumanBeing class to make any number of instances of itself. Just for fun, let's build some of the cast of Community:

 class HumanBeing
{
public string Name; // field
public int Age; // field
public string Major; // field
public bool InStudyGroup; //field
public void tellsJoke(); // method

static void Main(string[] args)
   {
   HumanBeing Winger= new HumanBeing();
   Winger.Name = "Jeff Winger";
   Winger.Age = 32;
   Winger.Major = "Education";
   Winger.InStudyGroup = true;

   HumanBeing Britta = new HumanBeing();
   Britta.Name = "Britta Perry";
   Britta.Age = 30;
   Britta.Major = "Psychology";
   Britta.InStudyGroup = true;

   HumanBeing Starburns = new HumanBeing();
   Starburns.Name = "Alex Osbourne";
   Starburns.Age = 39;
   Starburns.Major = "undeclared";
   Starburns.InStudyGroup = false;
  }
}
Enter fullscreen mode Exit fullscreen mode

Not too bad, yeah?

We started by building a class with class members, then created several instances of that class called objects with more specific characteristics:

classes and objects


There you have it in the simplest, Greendale-iest terms.

That's all from me today, dearest Coder. Godspeed in your keystrokes.

Clickity-clacks,

Kasey

Top comments (1)

Collapse
 
murphymurph21 profile image
John Murphy

Nice Kasey, love your humor. You definitely are your way to being a great content creator!!! Some things to consider that are hopefully constructive. I think you meant Functional programming and not Procedural. I could be wrong. When I think of procedural programming I think of things(objects, data, etc.) being dynamically changed at runtime. Also Javascript functions uses camelCase tellsJoke (); and C# methods use PascalCase TellsJoke();. Love the work you put into this article tho, great work man!!!!