Key Concepts

Review core concepts you need to learn to master this subject

C# Classes

using System; namespace BasicClasses { class Forest { public string name; public int trees; } } // Here we have the Forest class which has two pieces of data, called fields. They are the "name" and "trees" fields.

In C#, classes are used to create custom types. The class defines the kinds of information and methods included in a custom type.

Basic Classes and Objects
Lesson 1 of 2
  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,…

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