Well, that's my thesis at least. I started programming on the web in Perl, from there I moved on to PHP and eventually found myself were I am as a C# developer. I programmed in PHP exclusively for over 5 years. I loved it. I learned a lot about it, its quarks and why it really stuck with so many people. At its core it's simple.

But as I grew as a programmer I wanted more. After a discussion with another programmer I knew at the time, who was far more advanced then I still am, I went with C# to start the next leg of my journey.

I'll get a few things out of the way: Developing on Windows, in .NET is free: Visual Studio. ASP.NET is modern and very cutting edge: ASP.NET MVC5. Most of PHP's issues are one of its image because so many non-programmers make really horrible web pages with it. On many leading web hosts hosting Windows server costs the same. C#/.NET pays better.

Now let's get dirty.

True OOP

At its core it's dificult to argue that PHP isn't still a functionally driven language. Nearly everything you need to use you use by calling a function. It's rare to see the OOP implementation of those functions and when you do it's often just wrapper implements and not an actual extension of the object.

Get every alpha char from a string

Ignoring the fact that a search for this for PHP just brings up regex's (and now you have two problems), here's a basic way to do this in PHP.

$arr = str_split("my string has 12345 in it");

foreach($arr as $c){
    if(ctype_alpha($c)){
        $res .= $c;
    }
}

echo $res;
// mystringhasinit

A little verbose, but gets the job down and is generally pretty readable for even someone without PHP knowledge of what ctype_alpha is exactly.

You could do it almost exactly this way in C# too, but we're here to learn (well, I'm here to learn, no idea what you people are doing).

var res = new String((@"my string has 12345 in it").ToCharArray().Where(Char.IsLetter).ToArray());

The big difference here is all of the functions (in C# they're methods) are performed directly on the object. You don't call ToCharArray("my string..."), because of C# extension methods the input is automatically the current object you're handling.

What if I want to add new methods for a specific type that we didn't make and can't edit (we'll get to generics later)?

public static class MyStringExentions
{
    public static string ToLowerAndTrim(this string value)
    {
        return value.ToLower().Trim();
    }
}

Ussage

var myvar = "  some String Content!    ";
myvar = myvar.ToLowerAndTrim();

// "some string content!"

This is a very simple example of how methods can be used to extend even objects that you don't control.

For the Loops!

It's impossible to iterate through any enumerable type in PHP without writing out some kind of loop or another.

Enter LINQ

LINQ is such a huge topic it has multiple versions of its own syntax. It can take a long time to understand LINQ but at its core it is a way to handle enumerables through SQL style commands.

A simple loop

foreach(var item in myItems)
{
    mynewitems.Add(new MyNewItems
    {
        Part1 = item.Part1
        Part2 = item.Part2
        Part3 = item.Part3
        // ...
    }
}

LINQ it.

mynewitems.AddRange(
    myItems.Select(m => 
        new MyNewItems
        {
            Part1 = item.Part1
            Part2 = item.Part2
            Part3 = item.Part3
            // ...
        }));

Make it look like SQL.

mynewitems.AddRange(
    (from item in myItems
     select new MyNewItems
    {
        Part1 = item.Part1
        Part2 = item.Part2
        Part3 = item.Part3
        // ...
    }
));

Extend it.

bool sometruth = false;

mynewitems.AddRange(
    (from item in myItems
     where item.SomeTruth == sometruth
     select new MyNewItems
    {
        Part1 = item.Part1
        Part2 = item.Part2
        Part3 = item.Part3
        // ...
    }
));

It can handle joining multiple enumerables even.

var myIds = new[]{ 1, 2, 3, 4, 5 };

mynewitems.AddRange(
    (from item in myItems
     join id in myIds on item.Id equals id
     where item.SomeTruth == sometruth
     select new MyNewItems
     {
         myId = id
         Part1 = item.Part1
         Part2 = item.Part2
         Part3 = item.Part3
         // ...
     }
 ));

For the most part if you can do it on SQL you can do it in LINQ. Just like with writing slow queries in SQL you can shoot yourself in the foot with LINQ and get some horrible performance. Much like PHP, LINQ's "issues" are one of its image. People have abused it and written terrible things and so now the masses think it's slow.

LINQ to SQL INSERT your DATASOURCE HERE

A lot of the joy in LINQ is being able to directly query a database through it. There’s many extensions for LINQ that let your query SQL Server, Mongo, MySQL, Couchbase... I honestly can't list them all here but heck, you can even query a XML and Excel files.

It's a lot more fun to query all those data sources with a var result = myTable.Where(model => model.Something == true && model.SomethingElse == false).Skip(20).Take(20).ToList() (LINQ Note: the general way these operate is they build the query up to a call such as .ToList(), so the filtering will happen on the data source and not in your application) The best part of this method is it can be seamless to change the backing provider. I recently had to move a project from SQL Server to Mongo and didn't change a single query.

Coming up...

In part two I will be covering how ASP.NET can save you time writing website backends...

About Author

Siva Katir

Siva Katir

Programmer working at MGM Solutions in Bellingham Washington.