[Solved] What is the use of multiple classes


Reason 1

To make the code easier to understand.

(Don’t underestimate how important easy to understand code is)

Imagine this:

class Student
{
    public string First { get; set; }
    public string Last {get; set;}
    public int ID { get; set; }
    public string Street { get; set; }
    public string City { get; set; }
    public int[] Scores { get; set; }
}

class SomethingComplex
{
    public void AComplexThing_1(string first, string last, int id, string street, string city, int[] scores)
    {
        // do something
    }

    public void AComplexThing_2(Student student)
    {
    }
}

When calling the AComplexThing_1 the method will look like AComplexThing_1("Al","Williams", 12345, "Highbridge", "Liverpool", new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9}); which is hard on the eyes, for a more complex and less intuative set od data this method would quickly become a nightmare to work with. However, creating a student object keeps things readable, even if there were hundreds of properties.

Reason 2

We can serialize objects

We might want to return this over HTTP (as an example). Having lots of individual variable is hard to handle, however, if they are all in an object (DTO) then we can use Serializers to convert the object to text like XML or JSON (or binary or whatever) and then back into an object again.

Reason 3

It’s OOP

You are working in an Object Orientated Language, the entire framework and tooling expects you to work with objects. If you do it, your life will become much, much easier.

Reason …

There are lots of other reasons people could add, those are the main ones I can thing of off the bat.

1

solved What is the use of multiple classes