Skip to main content
CSharpUniversity ASP.NET and C# lessons

The search runs entirely in your browser over the published lesson list.

Defining a class of your own

A class is a blueprint for a kind of thing. It bundles the data that describes the thing with the methods that act on it.

A small class

C#
public class Lesson
{
    public string Title;
    public int Minutes;

    public string Summary()
    {
        return Title + " takes about " + Minutes + " minutes";
    }
}

Why this is worth doing

  • The names in your code start to match the words in your problem.
  • Related values travel together instead of being passed around separately.
  • Behaviour that belongs to the data lives next to it.
Start small. A class with two fields and one method is a fine first class. Big designs are built from small ones, not typed in one sitting.

Next, actually create one and use it in creating objects from your class.

New lessons by email

Leave your address and get a short note whenever a fresh lesson is published. No account needed and you can stop at any time.

Thank you. Your address has been noted in this browser session. Connect a mailing service to deliver the notes for real.