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.