C# for JavaScript Developers: Object Oriented Refactoring 01

Link to the exercises I’ll be working through from the Breaking IT Down Blog.

Now that I’ve laid the groundwork for some of the differences (and mostly similarities) between C# and JavaScript, I’m going to refactor the first exercise from the worksheet to be more Object Oriented. There are a few ways to do this, so I will show different approaches.

Creating A Class

When I was learning JavaScript, something my instructors always said was that everything in JS is an object, even if it isn’t. What they meant was that everything in JS is an instance of a “prototype” object, which is a template from which everything inherits methods and properties. It’s possible to modify the prototypes in JS so, for example, you could add a new method to the Array prototype and then every array you create would have access to that method. People in forums like stackexchange usually caution against modifying prototypes in JavaScript.

C# is explicitly built around this templating and customization concept. We can create template objects called Classes and then customize their methods and fields/properties to do what we need.

Having learned programming mainly through building “functional” apps and websites, this templating was a new concept for me to grasp. It requires a different type of big picture thinking about the structure of your code and how it will fulfill its purpose while interacting with users. For example, will the solutions to the various questions need to be accessed later in the program? Or are they only used once and then never needed again? Will the user need to see the answer to every question in every exercise at once, or just one at a time? What are the similarities and differences between all the exercises? The answers to these questions will fundamentally affect how the program is written.

I’ll show different approaches based on different use cases.

For a thorough explanation of how Classes and Namespaces work in C# I would recommend reading the Microsoft Documentation.

Encapsulation

When I solve the first exercise, my solution could look something like this:

Then I might add the solutions to another exercise below this, and so on, with all my code mixed together in one big .CS file.

However, this would violate the principle of encapsulation in Object Oriented Programming, which says that no code should know about or touch any other code unless it’s absolutely necessary. Read more about encapsulation in object oriented programming.

To solve this problem, I can create a class that holds all the code to solve the first exercise, and then instantiate it in the Main method.

In Visual Studio, I can go to the Project menu, then choose Add Class. Give it the name FullName2 (the second way to solve the problem), and then I’ll have a new blank template file like this:

Then in the Main method I can create a new instance of FullName2 with this line:

Now, all of the behavior that solves exercise 1 will live inside the class FullName2.

Notice there was no need to create a constructor explicitly. This is done automatically in this instance. It is necessary to create a constructor when the class requires some external information at the time of instantiation.

Another very important thing to notice is the type declaration of fullName2. The type is actually the prototype object I created in a different file. When I declare the variable of type FullName2 C# takes that template file and creates an instance of it here that will be customized as necessary. This works very similar in concept to creating a class in JavaScript.

Read more about creating a class in C#.

Approach 1

I’ll be referencing the exercise from here on out without actually quoting it directly, so it may help to have a copy in front of you as you follow along.

Let’s say that the program will NOT need access to the manipulated versions of the user’s name after they are displayed. That means that after the user puts in their name and receives the uppercase and lowercase versions etc., they can be discarded.

The object-oriented implementation for this would be a method we could call on the FullName2 class that executes these manipulations. We’ll call it Execute().

Just like in the functional implementation, it asks the user for their full name, reads that into a variable called name, and then prints the necessary strings to the Console:

Some syntactical notes:

In C# foreach, in is not a method called on a collection of data (like in JavaScript), but is a statement in its own right. Read more about foreach in C#.

Also, in front of the function’s name are two important words. public and void.

public is an access modifier that describes the level of accessibility for this method. In other words, how available is this method to other code in the program? public is the most permissive of all the access modifiers, and that’s what allows the main program to access it in this case.

The next word void describes the return value of the method. void means there is no return value. Other possible values are string, int, char, or any other type in C# including an instance of a class you create.

This method accomplishes the same goal as the functional version, but look at how we can start all of the computation with one line in the Main method:

Most importantly the code is more compliant with Object Oriented Practices!

There you have the first approach to refactoring the previous exercises to be more in line with object-oriented paradigms. Look for the second part of the refactoring process in a subsequent article that touches on a different approach for a different hypothetical use case.

I’m looking forward to hearing your thoughts on these subjects, so please don’t be a stranger in the comments or in notes here on Medium!

Github repo for this project.

I’m a web developer focusing on interactive projects using React and Nodejs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store