Lesson 1.1: Public?You might have noticed that I told you to add 'public' before anything in your class. Try removing that and running the program again. Any luck? No, I thought not.
Classes have attributes. In our Person class, they have two attributes, Name and Age. However, there are different types of attributes. Four in fact, these are public, private, protected and internal. Right now I want you to forget the last two, you won't need to think about these yet, we are going to focus on the first two.
When an attribute is public, anything can access it. It is open to be read and written by any other class. When an attribute is private, only the class it is within can do anything to it. By default in c# attribute variables are private, meaning that having nothing in front of a variables is the same as having 'private' there. Java works in the opposite manner, in that things are public by default. This is because Java is inferior, and I plan to mock it during any lessons to do with delegates, or indexes, partial classes, or foreach loops.
Lesson 1.2: public void Talk()Now, hopefully you remember what public means, right? Well it works for methods as well as attributes. If you make a method 'private' then only the class itself will be able to call it, but as we want other things to make the person talk, it needs to be public.
Void means that there is no return type. When this method is called, it doesn’t give anything back. Later on we will be using things with return types, so it should become more clear.
'Talk' is the name of the method. That is what you use to invoke it.
() is where we put parameters. These are things we tell the method for it to work. In this case, it will go fine without anything, so we have empty brackets.
I'm hoping you have already messed around with Console.WriteLine before, but this has a cool trick in it. Within the string you want to write, you can include {number}, and have that relate to anything else you add to the methods parameters. This is useful as duct tape if your using the Console.
Lesson 1.3: Onto the main.We start with declaring a Person with 'Person guy'
'guy' is the name of the variable, and how we refer to it, how we use it. We could have used anything here, and replaced 'guy' with that new thing anywhere. In fact, lets do that now!
Right click Guy > Refractor > Rename...
In the New Name space, change it to 'dude', click Ok and Accept. Isn't Visual Studio awesome? It just changed 'guy' to 'dude' throughout your code. That is a really nifty trick sometimes if you suck at spelling like me.
Run the code again. Any change? Nope, none. Like I said, it doesn’t matter what we call the variable, it will run fine.
We then have = new Person()
That is the same as going to the construction workers and asking them to make a new building. The 'new' keyword summons your army of workers, and the Person() hands them the blueprints. It is really a constructor, and later on you will be able to give them more details like givin the person a name on construction, but this works for now.
Now, dude.Name = “Steve Jobs” is telling dude what he's name is. You are assigning he's name. Like I said before, you can only do this because it is public. Otherwise you could only assign it within the Person Class
Lesson 1.4: UMLI'm sure you know what a flow chart is. You know that thing that shows repetition and selection and iteration? It is pretty sueful, but it falls flat when classes get involved. As such, we have something new for showing class structure, and it is called a class diagram.
There are different ways to draw a class diagram, but a some what universal way is UML. UML stands for United Modeling Language, and it applies to a lot more than class diagrams. There are use cases, and system sequence diagrams, and all sorts of cool shit, but I figure keep it to the class diagram for now, as it will suit what we need.
It is important to realize that we have a very, very simple class structure here, so you can expect a very simple class diagram. Because we only have one class in our program, you can expect only one class in our diagram, and here it is.

This diagram was made in Microsoft Visio, but there are other programs that can make these, some with free versions. Visual Paradigm and Rational Rose are two off the top of my head. Otherwise, it is fine to just draw these by hand, just like any other diagram.
First thing to notice is that we have a box. You see that box? That is a class. Every class gets a box, it's a rule. Each box is separated into three parts.
The first part is just the class name. It is pretty simple right now, but the way that you write the class name means stuff. Seriously, you need to write abstract classes in a different way, but don't worry about that for now.
The next part shows the attributes. It follows the convention -{name} : {type} and that is pretty much all you need to worry about it for now.
The last part of the box is for methods. It has a very similar convention to attributes, but a little more detailed with +{name}({parameters}) : {return type}
It might not seem like much now, but in larger programs, being able to see the flow of data around your program makes life a lot easier. This is the road map to your program, if only there was more than one place to go...