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.

C# Constructor

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#, whenever an instance of a class is created, its constructor is called. Like methods, a constructor can be overloaded. It must have the same name as the enclosing class. This is useful when you may want to define an additional constructor that takes a different number of arguments.

C# Parameterless Constructor

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#, if no constructors are specified in a class, the compiler automatically creates a parameterless constructor.

C# Access Modifiers

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#, members of a class can be marked with access modifiers, including public and private. A public member can be accessed by other classes. A private member can only be accessed by code in the same class.

By default, fields, properties, and methods are private, and classes are public.

C# Field

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#, a field stores a piece of data within an object. It acts like a variable and may have a different value for each instance of a type.

A field can have a number of modifiers, including: public, private, static, and readonly. If no access modifier is provided, a field is private by default.

C# this Keyword

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#, the this keyword refers to the current instance of a class.

C# Members

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#, a class contains members, which define the kind of data stored in a class and the behaviors a class can perform.

C# Dot Notation

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#, a member of a class can be accessed with dot notation.

C# Class Instance

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#, an object is an instance of a class. An object can be created from a class using the new keyword.

C# Property

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#, a property is a member of an object that controls how one field may be accessed and/or modified. A property defines two methods: a get() method that describes how a field can be accessed, and a set() method that describes how a field can be modified.

One use case for properties is to control access to a field. Another is to validate values for a field.

C# Auto-Implemented Property

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#, an auto-implemented property reads and writes to a private field, like other properties, but it does not require explicit definitions for the accessor methods nor the field. It is used with the { get; set; } syntax. This helps your code become more concise.

C# Static Constructor

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#, a static constructor is run once per type, not per instance. It must be parameterless. It is invoked before the type is instantiated or a static member is accessed.

C# Static Class

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#, a static class cannot be instantiated. Its members are accessed by the class name.

This is useful when you want a class that provides a set of tools, but doesn’t need to maintain any internal data.

Math is a commonly-used static class.

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