What is Object Oriented Programming(OOP)?

OOP revolves around the idea of defining relationships between objects. Objects can have properties (attributes) and methods (functions) associated with them. With this approach, code is more modular, reusable, and maintainable. Objects are the runtime instance of what C# calls a class.The class serves as the blueprint for creating this instances. When starting C#, it is helpful to make real life analogies. For example, we could represent a car as a class in C#.

Let's first ask ourselves, What does a car have?

  • Owner - a person's name
  • Make - such as Lexus, Honda, Tesla
  • Model - such as Accord, Model Y
  • Color
  • VIN Number
  • Registration number - License plate number
  • Let's convert this to a class now in C#. If you are following this along, make a new C# file and name it Car.cs

    If you do not know how to add a file in Visual Studio, please look at this image.

    public class Car 
    {
        public string Owner {get; set;}
        public string Make {get; set;}
        public string Model {get;set;}
        public string Color {get;set;}
        public string VIN {get;set;}
        public string RegNumber {get;set;}
    }

    Let's inspect each part of one line:

    public string Make {get;set;}

    Public is the scope of the variable. It is an example of an access modifier.

    Different access modifiers

  • Public variables can be accessed from any class and any external project.
  • Private variables can only be accessed from the class the variable was defined in. If we made the VIN number private, the VIN number would only be able to be accessed from inside the Car class.
  • Internal variables can only be accessed from within the DLL. This is mainly used when your DLL will be interacting with other DLLs. Internal variables are public within the scope of your DLL but not available to other DLLs.
  • Modifying the level of access for variables allows certain variables to be hidden so that all data will not be available to everyone. For example, if a user has to be authenticated to see certain information, that information would not be public in the User class. This idea is a concept known as encapsulation.

    "String" is just the data type of the variable. This is stating that the variable can hold any alphanumeric value.

    "Make" is the variable name. In this context, Make represents the Make of the car.

    Access Modifiers Continued

    {get; set;}

    This allows the variable to be get and set from anywhere because the variable is public. Sometimes, variables only should be set within the class instead of anywhere. We can customize this line in order to only allow for the Make to be edited inside the class but accessed from anywhere, we can edit the line to be

    {get; private set;}

    Now, the make can only be edited inside the Car class but can be accessed from anywhere. This is our first look at access modifiers. This is something that takes some practice. If you want to hide data from the user/other devs, you can use access modifiers to do that.

    This is our Car class right now. We are going to add onto it on the next page.

    public class Car 
    {
        public string Owner {get; private set;}
        public string Make {get; private set;}
        public string Model {get; private set;}
        public string Color {get; private set;}
        public string VIN {get; private set;}
        public string RegNumber {get; private set;}
    }

    A Bank Account -> Class

    If we continued using our current structure for the bank account, having multiple users will be difficult because for each user, you need (for now) three variables stored. This is where we can use classes in order to make our bank account more scalable so in the future we can support more than one user.

    I made a new file called Account.cs just like Car.cs up above.

    Here is Account.cs

    public class Account
    {
        public string Name { get; private set; }
        public int Age { get; private set; }
        public double Balance { get; private set; }
    }

    Next Section: Constructors