.NET Classes
Start course

This course takes code from the Data Types and Variables course and refines it using object-oriented (OO) principles. We explore some of the main concepts of OO programming during this process, such as encapsulation, code reuse, and inheritance.

Along the way, we learn more about essential code structures such as conditional evaluation with if-then-else statements, functions for grouping code that performs a specific task, and for-loops for dynamically repeating an action. We will also look at how .NET and C# have object-oriented baked-in as a fundamental design principle underpinning the framework and language.

Learning Objectives

  • Understand the benefits of object orientation and what came before it
  • Learn about essential code structures and turn code into a class
  • Refine the class code and learn more object-oriented concepts
  • Learn about inheritance, a fundamental object-oriented concept
  • Understand how object-orientation is a foundation principle of C# and .NET

Intended Audience

This course is intended for those who already have an understanding of data types and variables in C# and now want to learn about object-oriented principles.


To get the most out of this course, you should have an understanding of C# as well as basic data types: strings, numbers, and Booleans. In order to follow along with the demos, you should also have a working development environment, whether in Windows, Linux, or macOS.


The GitHub repository for this course can be found here.



Let's now turn our attention to some of the classes within .NET Core. I'm going to mix it up by using Visual Studio in this demo, but VS Code will work just as well. As I've alluded to, pretty much everything with C# is an object or a class with properties and methods. I'm going to add a new class file called Builtin to the Integer's project from the last course and define some simple data type members of string, int, double, char, and an integer array. I'll define a constructor and assign some values to the private members. For many, if not most programmers, string manipulation or parsing can make up a significant portion of the job from time to time. With that in mind, the .NET string class has a plethora of methods for dealing with text. Thanks to language IntelliSense, I can type the name of a string variable followed by a dot and get a list of the public properties and methods available. Most of these methods are specific to the String class, like Contains, EndsWith, StartsWith, and Substring, but some are from the string's ancestor, Object. The Object class is the ancestor or root of all C# classes, including simple data types, and has methods such as GetType, which returns the data type or class of the object, and ToString that returns the object instance as text. String has an int property, Length that I'll assign to count. Even a char is a class, although all the methods except CompareTo are inherited from Object. I'll delete the current Main code and replace it with a new instance of the Builtin class. Console that we've used extensively is also a static class, one where you don't have to create an instance of it to use it. Static classes are often used where you don't need to create instances of the class (you can't instantiate and static class), but you need functionality not related to an object's data. In C#, the Math class is an excellent example of this providing a selection of mathematical and geometry functions in the form of static methods.

I'm going to use some string methods to explain how easy it is to manipulate text. Each one of these methods returns a string object allowing you to chain them together. We start with our name variable, which is an instance of a string class. Then I call its ToUpper method, which returns another string object all in upper case. Instead of assigning the uppercase string to another variable to call the substring method, it's implicitly called on the output or returned string of ToUpper. Finally, I'll call the TrimStart method. We can use Object's GetType method to find out the data type of any variable: a simple variable or a complex class. GetType is a simple form of viewing the internals or metadata of a class. In C#, looking into the internal structure of a class at runtime, like listing its properties or methods, is called reflection, and we'll look at that in a future course on classes. I'll write out the data types of these variables with GetType. You can see these simple data type classes all belong to the System namespace. We'll just add in the double floating as it's displaying a green squiggly line indicating a declared variable that isn't being used, so use it or lose it. Unused variables do no good and only serve to confuse.


It will come as no surprise that there is a static class for retrieving metadata about your application. We can use AppDomain.CurrentDomain.FriendlyName to get our app name. The “this” keyword refers to the class the code is executing in. In this case, excuse the pun, it's redundant, but you'll need it if you are working with objects of the same class or with shared ancestors, and you need to differentiate between them.

About the Author
Learning Paths

Hallam is a software architect with over 20 years experience across a wide range of industries. He began his software career as a  Delphi/Interbase disciple but changed his allegiance to Microsoft with its deep and broad ecosystem. While Hallam has designed and crafted custom software utilizing web, mobile and desktop technologies, good quality reliable data is the key to a successful solution. The challenge of quickly turning data into useful information for digestion by humans and machines has led Hallam to specialize in database design and process automation. Showing customers how leverage new technology to change and improve their business processes is one of the key drivers keeping Hallam coming back to the keyboard. 

Covered Topics