C# Classes, Objects, and Class Members Explained by Dan Harmon's "Community" | Dear Coder
Tags: csharp, beginners, webdev, oop
Published: 2021-04-27 15:25:51

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.

{% youtube -fpAc768iJo%}
_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:

{% youtube ANfVHstArQ0%}
_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
{}
```
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
}
```
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;
   }
}
```

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

![HumanBeing Pierce](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fagqe7gwewm18eozn0c.jpg)

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;
  }
}
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xl1pr7xf4mgj32iuiyeq.png)

---

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