So, basically, you are wondering what the value of classes are when you can accomplish pretty much all programming without classes?
Let me tell you a story. A long time ago in a galaxy not unlike our own, there were programming languages much less developed than C#. One of these languages was called
assembly language. This assembly language was as close to the processor as you could get without writing in
machine code, which is just a bunch of 0's and 1's. Assembly language allows you to do anything you want with a computer, as it gives you access to the full instruction set that a processor can compute.
However, there is one problem with assembly language. It's so simple, that many common tasks are very hard to accomplish, and there is a lot of unnecessary repeat typing. For example, if you want to create a function, you have to initiate the instruction to jump to it, you have to save register values to the stack, and when the function call is over, you have to jump back to where it was called from, and undo all the steps you did when you did the initial jump.
Modern
high level programming languages make these common tasks much easier, as they have shortcuts built in. For example, in C#, that complicated assembly language task of calling a function is done like this:
namespace MyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
int y = Add(5, 6);
}
static int Add(int x1, int x2)
{
return x1 + x2;
}
}
}
Now, I only wanted to demonstrate the
Add
function and the call to it, but I also include the namespace and class and main function for completeness sake. Here are the only important parts that I was talking about:
int y = Add(5, 6);
static int Add(int x1, int x2)
{
return x1 + x2;
}
Very simple. Having a function like this and being able to easily call that function in many locations means that you have increased the
reusability of that code. That is a very good thing, as reusability means you have to type less, and so it lets you program faster. The same types of principles apply with classes. As you saw above, I had a class called
Program
with two functions in it. However, putting all of your functions into a single class can get out of hand. I have worked in code bases with millions of lines of code. By splitting your code into different functions, classes, and namespaces, you can make better sense of the code with something called
encapsulation. Encapsulation basically means writing classes and functions that do a bunch of work, but that the caller of the function/class doesn't have to know about. To give you an example, I have created a new class and moved our
Add
function into it:
namespace MyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Maths m = new Maths();
int y = m.Add(5, 6);
int z = m.Add(8, 9);
}
}
class Maths
{
public int Add(int x1, int x2)
{
return x1 + x2;
}
}
}
If we wanted, we could move the Maths class into a different file. Right now, all of the above code is in the Program.cs file, but we could create a new file called Maths.cs and put the Maths class in it. This is what that file would contain:
namespace MyConsoleApplication
{
class Maths
{
public int Add(int x1, int x2)
{
return x1 + x2;
}
}
}
If we look at our Program.cs file again, we see that it is very simple:
namespace MyConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Maths m = new Maths();
int y = m.Add(5, 6);
int z = m.Add(8, 9);
}
}
}
Write a few million lines of code with a few hundred coworkers, and the value of classes becomes much more clear. They allow you to encapsulate functionality and data to simplify the code base and allow for greater reusability.
I am not going to teach a full Computer Science course here, but to give you a taste of why else classes are useful, they can be used to: store data, create structure for serialization, allow for inheritance and interface implementation, and much more. You will come across all of this over time. For now, just trust that classes do have their uses and you will definitely make use of them greatly over your career.