Chevron Left Icon
Basic Classes and Objects
Lesson 1 of 2
Chevron Right Icon
  1. 1
    With data types like int, string, and bool we can represent basic data and perform basic operations: int count = 32; count++; What if we want to represent something more complex — somethin…
  2. 2
    C# provides built-in data types, like string: each instance of the string type has its own values and functionality. string phrase = “zoinks!”; Console.WriteLine(phrase.Length); Console.WriteLine(…
  3. 3
    We need to associate different pieces of data, like a size and name, to each Forest object. In C#, these pieces of data are called fields. Fields are one type of class member, which is the gene…
  4. 4
    As of now, a program can plant any value in a Forest field. For example, if we had an area field of type int, we could set it to 0, 40, or -1249. Can we have a forest of -1249 area? We need a way t…
  5. 5
    It might have felt tedious to write the same getter and setter for the Name and Trees properties. C# has a solution for that! The basic getter and setter pattern is so common that there is a short…
  6. 6
    At this point we have built fields to associate data with a class and properties to control the getting and setting of each field. As it is now, any code outside of the Forest class can “sneak past…
  7. 7
    Previously we used properties for field validation. By applying public and private, we can also use properties to control access to fields. Recall our imaginary Area property. Say we want program…
  8. 8
    The third type of member in classes is methods. This lesson assumes that you are already familiar with methods, so the syntax should look familiar. In the past you learned that methods are a us…
  9. 9
    In each of the examples so far, we created a new Forest object and set the property values one by one. It would be nice if we could write a method that’s run every time an object is created to set …
  10. 10
    In the last exercise we assigned the area field in the constructor: class Forest { public int Area { / property omitted / } public Forest(int area) { Area = area; } } The par…
  11. 11
    Just like other methods, constructors can be overloaded. For example, we may want to define an additional constructor that takes one argument: public Forest(int area, string country) { this.Ar…
  12. 12
    Congrats! You’ve finished a lot of content and some of the most important concepts in C#. When someone asks you, “How do I make a custom data type in C#?” you can talk all about it! In this lesson,…
  1. 1
    At this point, you may recall: A custom data type is defined by a class. An instance of a class is called an object. Multiple, unique objects can be instantiated from one class. * This pr…
  2. 2
    You already know how to create a field and property, like: class Forest { private string definition; public string Definition { get { return definition; } set { definition = value;…
  3. 3
    You already know how to create an instance method, like: class Forest { private string definition; public void Define() { Console.WriteLine(definition); } } This behavior (printing …
  4. 4
    An instance constructor is run before an instance is used, and a static constructor is run once before a class is used: class Forest { static Forest() { // } } This constructor is…
  5. 5
    We covered a few static members: field, property, method, and constructor. What if we made the whole class static? static class Forest {} A static class cannot be instantiated, so you only want…
  6. 6
    With great power comes great responsibility. If you plan on using static, you must be familiar with the errors you might cause! Here a few common ones: This error usually means you labeled a stat…
  7. 7
    Now that you’re familiar with classes, you’re ready to tackle the Main() method, the entry point for any program. You’ve seen it many times, but now you can explain every part! class Program { p…
  8. 8
    Congrats! You are now ready to use static throughout your classes: In general, static means “associated with the class, not an instance”. A static member is always accessed by the class name,…

What you'll create

Portfolio projects that showcase your new skills

Pro Logo

How you'll master it

Stress-test your knowledge with quizzes that help commit syntax to memory

Pro Logo