I really enjoy computer programming. It’s what I started doing when I first retired, and what I still do because it brings me joy. Over the years I have collected a file of programming project ideas. One of them is based on a quote that I can’t remember the source of. It was something like “to learn how to program games, take six simple, classic games and program them.” I was originally going to do this is Pygame, since I’m mainly a Python programmer, but then I learned about Unity. I decided to do the six games thing in Unity, but Unity doesn’t support Python, so I decided to learn C# (pronounced “cee-sharp”), which it does support.

You’re Just Going to Learn C#?

Someone asked me this when I mentioned the idea, like just deciding to learn a particular language is an extreme thing to do. But my data structures teacher in college told us (on the first day of class) that he knew 40 computer languages, and that he wouldn’t be surprised if we knew that many some day. When he needed to learn a new language he would just buy a book on that language, read through it, and write some code with the language. But the data structures and algorithms he was going to teach us would be useful in all of the languages we might learn.

Now, I only know thirteen to twenty languages (depending on how you define “know” and “language”), and they don’t really make books that you can use to learn languages any more. And it’s not like I’m planning to come out of this as some jedi master/senior dev of C#. I just want to get the basics of the language, so I can apply forty years of general programming knowledge and start learning the basics of Unity.

C# is not Python

Like I said, I’m mainly a Python programmer, at least these days. C# is a much different language than Python. In C# you spend a lot of time telling the computer what you are going to do and how you are going to do it, and if you don’t do things the way you said you would, it gets mad at you and refuses to work. In Python you just do whatever you want and Python tries to keep up with you. It might still get mad at you and stop working, but it’s willing to try things out without checking them over first.

I thought this was going to be a real problem for me, switching from loose to constrained. But it wasn’t that much of a problem, and where it was a problem, it just made me realize I need to plan things ahead of time more. And it’s not like I have a problem with that. Most of my programming has been hobby programming where I don’t need to worry about planning. But when I got a job programming SAS, I really began to see the value in planning things ahead of time. And I had previously done some programming in other languages (like C++ and Java) that require declaring things ahead of time and being careful with access to different parts of the program.

How I Started

I started with Microsoft’s site, and went through all of the articles in their C# Fundamentals section. Mostly I took notes, about 500 lines worth in a text file. I later came to hate these notes, as I did not put in nearly enough examples of the syntax required for various things. However, I could always fall back on my standard programmer skills of doing web searches and finding the answer on Stack Exchange. It doesn’t matter how stupid a programming question is, someone probably asked it on Stack Exchange. And there’s a good chance someone even made a useful answer.

At the end of the Fundamentals section there are a bunch of tutorials, so I programmed all of those. I also hunted down more information on topics that confused me or that I was more interested in, and often made short programs to try them out. Two in particular were the LINQ system and string formatting. The LINQ system reminded my of SQL, and having done a lot of PROC SQL back in my SAS days, I found it very interesting how similar ideas were applied to data within the program. One thing I write a lot of in Python are command line interfaces, so text formatting is something that I use a lot. It’s similar, and yet different, in C#, and I wanted to make sure I understood those similarities and differences.

The Side Quests

One thing I really got into in C# was the XML documentation. I’m a big fan of documenting your code. I didn’t used to do it much, but early in my professional SAS career I cam across an undocumented program that was really hard to understand. The kicker was that this was a program I had written six months previously. That convinced me to start documenting my code. To the point where some people think I over document my code, but those people obviously under document their code, and aren’t worth listening to.

C# has defined ways to document your code in XML (eXtensible Markup Language), which is a more general system similar to the HTML (Hyper-Text Markup Language) used in web programming. This allows for other computer systems to read and make use of your comments. Unfortunately, the standard way of doing this in C# is really ugly. That may seem odd coming from a self-proclaimed function over form guy, but I find ugly code hard to read, much like undocumented code. And my text editor wasn’t cooperating. So I had to fiddle around with how to write my XML comments so they weren’t ugly, and so my text editor interpreted them as XML.

The whole ugly code issue and my quirks with XML comments got me to write a style guide for my C# code. This probably doesn’t surprise many programmers, since Python has PEP 8 (Python Enhancement Proposal 8), which is a style guide for the whole Python language. Except it isn’t. It’s a style guide for the Python Standard Library, which many parts of the Python Standard Library don’t even use, and it was never meant to be the standard for all of Python. But some people have decided it’s the standard for all of Python, and they get rather obsessive over it (says the obsessive compulsive guy). But I have serious problems with it. Spaces instead of tabs? What is wrong with you people?

Anyway, I got into the weeds of C# style, because of the way Python uses indentation. In many languages, including C#, blocks of code for certain statements are identified by punctuation. C# uses braces ({}):

if (x > 5)
{
    x++;
    Console.WriteLine(x);
}

The two lines under the if statement are indented, but as long as they have the braces they don’t need to be:

if (x > 5) {
x++;
Console.WriteLine(x);}

The above works just as well, but I would say it’s hard to read. Python doesn’t use as much punctuation (just a colon), but it mandates the indentation:

if x > 5:
    x += 1
    print(x)

Now, if you don’t want to change x, and just want to print it, both languages allow you to do it on one line. In C# that looks like this:

if (x > 5) Console.WriteLine(x);

But having programmed so much Python, I want that indentation for clarity. C# is fine with that, the following works just as well:

if (x > 5)
    Console.WriteLine(x);

But there’s the issue of code maintenance. You want to write your code so it is easy to change without introducing bugs, because odds are you are going to have to change it. And if I later needed to put changing x back into the code, my Python habits would have me do this:

if (x > 5)
    x++;
    Console.WriteLine(x);

And that doesn’t work right in C#. Because without the braces, and with the way C# doesn’t care about indentation, it will only treat the first line as part of the conditional. So I settled on this style:

if (x > 5)
    {Console.WriteLine(x);}

It has the indentation I want to show the structure of the code, and the braces to keep my Python instincts from tripping me up, even though I expect other C# programmers will hate it. <joke>But I promise not to be obsessive about it like those PEP 8 people.</joke>

The Bow on Top

Then there was the final step, which is the same is for every new programming language I learn. I have a small program that I always write in every language I learn. Something that pretty simple, but complicated enough to test out the basics of the language. Since I have written the program several times, I know the basic structure of the program, I just have to apply it right in the new language. I used to always write a program to call poker hands, for five and seven cards hands, high and low, usually in a basic way and then trying to use any good efficiencies in the new language. However, I don’t play poker anymore, not since the Texas Hold’em craze killed dealer’s night. Now adays I write a simple FreeCell program, using a command line interface. I did a rather simple one this time. It doesn’t have the statistics and undo capability that I normally write in, but I think it got me enough experience with C# to move on.

And so I will be moving on to learn Unity programming with C#.