DEV Community

Miguel Teheran
Miguel Teheran

Posted on • Updated on

C# evolution

C# has been in the top 10 of the most used and loved programming languages in the world during recent years. With more than 20 years of existence, the changes it has made in its syntax and the different technologies that use it today make it a language worth learning and deepening.

In this article, we will review the different C# phases that I personally think have been the most important until version 10.

Phase 1: The Java copy and the Visual Basic age (C# 1 - C# 2)

The first versions of C# were very limited and similar to Java and C++ in everything, so much in fact, that C# was known as the copy of Java. Even though, it was inspired by multiple languages, including Pascal, for people its resemblance to Java and the fact that it was released at a time when Java was the king was hard to accept.

For Microsoft developers, Visual Basic was still the language par excellence and with greater acceptance by the community.

Some improvements to highlight from the C# in this period (Version 1/2) are,

Partial class

public partial class MyClass
{
    public void MyMethod1()
    {
        // Manually written code
    }
}

//file2.cs:
public partial class MyClass
{
    public void MyMethod2()
    {
        // Automatically generated code
    }
}
Enter fullscreen mode Exit fullscreen mode

Generics

public static IEnumerable<int> GetEven(IEnumerable<int> numbers)
{
    foreach (int i in numbers)
    {
        if (i % 2 == 0)
            yield return i;
    }
}
Enter fullscreen mode Exit fullscreen mode

Phase 2: The new favorite (C#3 - C#4)

At this stage, C# has started to become more popular and many Microsoft developers have started to work with it and adapt it to their favorite language.

C# starts to embrace different improvements that other programming languages already have and starts to improve its syntax. And it has also been adopted by other technologies such as Xamarin, mono and unity.

Some improvements to highlight from this period of C# (Version 3-4).

Lambda Expresiones

//before C#3:
listOfFoo.Where(delegate(Foo x) { return x.Size > 10; });
//C# 3:
listOfFoo.Where(x => x.Size > 10);
Enter fullscreen mode Exit fullscreen mode

Default parameters

void Increment(ref int x, int dx = 1)
{
  x += dx;
}
Enter fullscreen mode Exit fullscreen mode

Phase 3: The innovator (C# 5 - C# 6)

C# begins a new chapter in which it gives examples of greatness by demonstrating innovative improvements that were later adapted by other programming languages.

async/await became the standard for handling asynchronous for several programming languages. With this improvement, C# moves away from the shadow of Java, becoming a different, innovative and simpler language.

In addition, the async/await C# implements different operators that allow us to simplify the lines of code used to make checks and conditionals.

The most important improvements to highlight are,

Async/Await

public async Task<int> GetUrlContentLengthAsync()
{
    var client = new HttpClient();
    Task<string> getStringTask =
        client.GetStringAsync("https://docs.microsoft.com/dotnet");
    DoIndependentWork();
    string contents = await getStringTask;
    return contents.Length;
}
Enter fullscreen mode Exit fullscreen mode

String Interpolation

$"Expected: {expected} Received: {received}."
Enter fullscreen mode Exit fullscreen mode

Null operator

var ss = new string[] { "Foo", null };
var length0 = ss [0]?.Length; // 3
var length1 = ss [1]?.Length; // null
var lengths = ss.Select (s => s?.Length ?? 0);
Enter fullscreen mode Exit fullscreen mode

Phase 4: The most complete (C# 7 - C# 8)

After so many improvements and continuous evolution, C# has started to gain popularity among developers and, with the arrival and development of .core the open-source community started to give it a chance. C# remains among the top 5 most used languages in the world and continues with great support for different technologies.

Now, with all the improvements in C# there are several ways to do the same thing, but depending on the scenario, you can use one syntax or another and look for ways to reduce the lines of code.

C# also adopts many enhancements from other programming languages (like Python) such as tuples and local functions (like JavaScript).

Some built-in enhancements,

Tuples

(double, int) t1 = (4.5, 3);
Enter fullscreen mode Exit fullscreen mode

Local Functions

private static string GetText(string path, string filename)
{
     var reader = File.OpenText($"{AppendPathSeparator(path)}{filename}");
     var text = reader.ReadToEnd();
     return text;
     string AppendPathSeparator(string filepath)
     {
        return filepath.EndsWith(@"\") ? filepath : filepath + @"\";
     }
}
Enter fullscreen mode Exit fullscreen mode

Phase 5: The minimalist (C#9 - C# 10)

One of the biggest criticisms of C# and Java is the complexity of the code to do simple things.

Any developer who is just starting out and tries to make a "hello world" with C# is going to be surprised that he will have to know what a namespace, a class, and a method are. This is crazy considering that we must consider that he is just trying to learn and understand the programming world. Languages like JavaScript, Go and Python greatly simplify the way of doing things, so they are preferred among junior devs and for many companies that need to speed up their development and decrease the learning curve.

C# 9 and C#10 are the dream of many years of effort in which we wanted C# to be preferred not only by senior devs or large companies but also by junior devs and new startups.

C# 10 Finally, we can say that the hello world of C# is just one line:

Console.WriteLine("Hello World");
Enter fullscreen mode Exit fullscreen mode

Features in C# 9 and C# 10

Top-level statement,

namespace HelloWorld
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
        }
    }
}

//In C# 9:
using System;
Console.WriteLine("Hello World!");
Enter fullscreen mode Exit fullscreen mode

Global using,

namespace MyNamespace;
Enter fullscreen mode Exit fullscreen mode

Using C# enhancements will allow you to reduce lines of code and apply best practices. C# is a language with an interesting evolution that has allowed it to maintain its popularity.

In the official Microsoft documentation, you can always find the description of each enhancement and an example,

https://docs.microsoft.com/en-us/dotnet/csharp/whats-new/csharp-10

And if you want to contribute to the design and evolution of C# you can create an issue or participate in the discussions of the official repo,

https://github.com/dotnet/csharplang

Top comments (5)

Collapse
 
carlhugom profile image
Carl-Hugo Marcotte • Edited

What a fantastic concise timeline you've put together here. Sometimes it's hard to remember those early days before generics, LINQ, and such modern features.

That said, you are missing the Global using example and your Global using example relates to file-scoped namespace.

Collapse
 
jdnichollsc profile image
J.D Nicholls

Great post mate, thanks for sharing! ❤️

Collapse
 
laxedo17 profile image
laxcivo

super article. C# 10 is even more minimalist than that, with Implicit Usings, top level statements dont even need to import a typical using. Cheers!

Collapse
 
carlhugom profile image
Carl-Hugo Marcotte

Very true, I updated my comment to reflect that.

Collapse
 
mteheran profile image
Miguel Teheran

I couldn't agree more. Thanks for commenting.