I was recently told that my next project at work would be a .NET application using C# in the backend. C# has been on my radar for a while now, since it supports wide ranging applications such as .NET apps and Xamarin. While C# supports multiple programming paradigms, its mostly used for object oriented programming. C# is commonly referred to as a descendant of Java and C++1, which is great for me since Java is my strongest language and I'm learning C++ in parallel with C#.
C# is statically typed with strict type rules (type coercion is rare)2. Programs in C# consist of executable and library files (with the .exe and .dll file extensions, respectively). C# is a compiled language, just like its ancestors C++ and Java.
This article explores my initial reactions to the C# language after writing about 300 lines of code. Much of the article consists of language features I find cool and unique. I also compare C# to other languages I use such as Java.
The first thing I noticed when writing C# is how similar its syntax looks to Java. For example, the first class I wrote represents a song.
While namespaces are an influence of C++, the rest of the object looks like a Java POJO. It even overrides the ToString() method from the object superclass, which is the same as Java besides for some letter case differences.
The main method also looks like Java.
While you can write C# code just like Java, there are a bunch of unique features to the language as well. One of the differences I noticed early on was the shortened getter/setter syntax, which is much more elegant than Java.
The Java equivalent to this code includes a long list of getter and setter methods. Java developers often use libraries such as Lombok to shorten getter/setter syntax, but a native solution is preferred.
One of the newer features in C# is tuples, which are a great way to represent an arbitrary amount of data. Tuples are one of my favorite Python features, so I'm glad to see other languages using them.
In C# you can create variable identifiers with the same name as a reserved keyword. I don't think any other languages I use have this feature.
The @ token isn't part of the identifier, and can be removed if the name isn't a reserved keyword3.
C# also provides two different behaviors for numeric overflows. Depending on whether the code block is checked or unchecked, numeric overflows throw an exception or wrap the value, respectively.
C# adds more features to arrays from C++ and Java. For example, C# provides two types of multi-dimensional arrays: rectangular and jagged. Rectangular arrays must be the same length in each dimension, while jagged arrays can be different lengths in the inner dimension. While C# has explicit syntax to enforce rectangular arrays, Java has no such enforcement.
By default, variables are passed to functions by value in C# such that value-types are passed as a copy of the value and reference-types are passed as a copy of the reference. Pass by value behavior can be changed to pass by reference with the ref keyword. In C++ this behavior requires the use of pointers.
Function parameters can also use the out keyword to declare an output parameter. Output parameters are used to return multiple values from a function. Although I think tuples are a more elegant solution, output variables are also a viable option. The only other language I can think of that uses output parameters is PL/SQL.
C# also has operator level support for dealing with null values. The null-coalescing and null-conditional operators present in C# are some of the most useful operators found in modern languages (Swift and PHP also have these operators).
While C# is influenced by Java and C++, it also includes many modern operations that I appreciate about languages such as Python, Swift, and Groovy. I'm excited to learn more of what C# has to offer in the future. All the code from this post is available on GitHub.