Learning C# as a PHP dev

By dustin, 20 December, 2022

I've recently started learning C# and Unity in my spare time to learn the basics necessary to create VR applications. I was initially intimidated by the idea of learning C# because I always assumed C derivative languages, especially statically compiled languages, that are often used for desktop application development would be incredibly hard for me to pick up. After all, they'd be so different from what I was used to.

Fortunately, though I haven't directly realized it, my journey through PHP has lead me to pick up a lot of the concepts of statically typed and object-oriented programming that seems to be the base of languages like C#. Most of my PHP code these days includes type hints, and some of it includes return type statements, but at the minimum, I understand these concepts and their value, which has made transitioning to a language that (I believe) requires these things in all code.

Earlier in my career, these types of things would have prohibited me from learning to program at all. I distinctly remember needing to write some Java for an early project I was assigned towards the beginning of my programming career that was so confusing.

I had initially learned to program in Ruby, writing things like:

class RectangleController < ApplicationController
  def area
    # Get the length of the rectangle from the params
    length = params[:length].to_f
    # Get the width of the rectangle from the params
    width = params[:width].to_f
    # Calculate the area of the rectangle
    @area = length * width

I had ChatGPT generate that code because it's been a while since I wrote some Ruby.

You'll notice there are no type hints or return type declarations the method doesn't even have to use the return keyword.

This code has very few odd 'programmer' things in it, really only the < in the class declaration and the params[:length].to_f during the assignment require much dedicated understanding. This was good to learn to program.

Now a lot of the PHP I write is something like this:

namespace App;
use Exception;
use DateTime;
use JsonSerializable;
class User implements JsonSerializable
    private string $name;
    private DateTime $birthday;
    private ?string $email;
    public function __construct(string $name, DateTime $birthday, ?string $email = null)
        $this->name = $name;
        $this->birthday = $birthday;
        $this->email = $email;
    public function getName(): string
        return $this->name;
    public function setName(string $name): void
        $this->name = $name;

Again, ChatGPT throws us an example to use because it's early and I didn't feel like digging through my code but look at how much more text there is here! we have namespace declarations, interfaces, imports, visibility declarations, and type declarations (with nullable indications), all the damn variables have a $ in them! We've got brackets and colons, semicolons, return type declarations, type hints, etc.


So much visual noise!

You know what though? I am used to it at this point, and more than that, I find a ton of it helpful while I work.

Here is the example converted to C#:

using System;
using System.Collections.Generic;
namespace App
    public class User : IJsonSerializable
        private string name;
        private DateTime birthday;
        private string email;
        public User(string name, DateTime birthday, string email = null)
            this.name = name;
            this.birthday = birthday;
            this.email = email;
        public string Name
            get => name;
            set => name = value;

Look at how conceptually similar that is! We can see the type declarations, the interface usage, the namespacing, and visibility declarations. It's maybe like going from Spanish to French, whereas the initial jump from no code to code is like jumping from English to something from another planet.

Anyhow, all that to say, if you're a PHP developer who is using the modern features of the language, and you write a lot of OO PHP, and you want to do a bit of learning of game dev stuff, give it a try, learning the language won't be that overwhelming. If you need somewhere to practice, the C# track on Exercism is a good place to start.