Can C# 9.0 Outclass Python as a Scripting Language?

“Simplicity is the soul of efficiency.” — Austin Freeman.

Sameeh Shkeer
Nov 27, 2020 · 4 min read
Image for post
Image for post
Photo by Jeremy Zero on Unsplash

“Simplicity is the soul of efficiency.” — Austin Freeman

Python shines as a scripting or glue language, not to mention Python is simple, easy to learn, and that the Zen design philosophy adds to its awesomeness and readability.

Whereas C#, is often popular for its versatility, robustness, and safety, coupled with the fact that it is counted among the most evolving languages nowadays.

C# 9.0 was officially released on the 10-Nov-2020; thanks to its Top-level statements feature, it is now a much more scripting friendly language.

Both Python and C# are High-Level, Object-Oriented, and General-Purpose languages.

Given these points, what are the major differences between both languages?

Interpreted vs. Compiled:

Python is an Interpreted language, while C# is a compiled one, which can count in favor of Python since there is no compilation step; the edit-test-debug cycle can be shortened.

However, we will see how C# can be compiled and executed in one step, turning this advantage into a tie.

Dynamically Typed vs. Statically Typed:

Python is dynamically typed, which means that variables do not have a type, but values do; in other words, you can give a Python variable an integer value and then give it a string value.

C#, on the contrary, is a Statically Typed language; hence all variables must be declared to be of a specific type.

With this in mind, let us see some real-world usages compared in both languages.

Prerequisites:

SDK

Python:

Python recent SDK(v 3.0 and above).

C#:

.NET recent SDK (v 5.0 and above).

Both are simple to find, download, and install.

Code

Python:

Code can be written in any text editor and saved as a *.py script.

Python scripts can be executed easily from CMD:

> Python "Script (*.py) Full Path"

C#:

Since each *.cs file must be coupled with a *.csproj to be executed,

I found the following approach to be the easiest.

  1. Create a new folder and give it a name.

2. Run CMD from within the new folder.

3. Using CMD:

> dotnet new console

This shall create a new project that you can easily edit its Program.cs file and type any script you wish to execute.

Then compile & run it easily by running this CMD:

> dotnet run

Now that we saw how to install the SDK and Run the Scripts let’s see some Python examples taken from python.org, written in C#.

Image for post
Image for post
Hello World!

The “Hello world” example:

Python:

C#:

Before the Top-Level statements feature, printing “Hello world!” to the console using C# required a complicated code block including a namespace, a class, and a function.

Not anymore, though! From now on, the following one-liner will compile just fine, yielding the same result :-)

Expected output:

Hello World!

The “JSON” example:

Given this JSON file ‘ex.json’:

Python:

C#:

Expected output:

root: {"name":"John", "age":31, "city":"New York"}Name: JohnAge: 31

The “Enumerate” example:

Python:

C#:

Expected output:

iteration 0 is johniteration 1 is patiteration 2 is garyiteration 3 is michael

The “Tuple” example:

Python:

C#:

Expected output:

This generation has 1 babiesThis generation has 2 babiesThis generation has 3 babiesThis generation has 5 babiesThis generation has 8 babiesThis generation has 13 babiesThis generation has 21 babiesThis generation has 34 babiesThis generation has 55 babiesThis generation has 89 babies

The “Regex” example:

Python:

C#:

Expected output:

555-1212 is a valid US local phone numberILL-EGAL rejected

The “Dictionary” example:

Python:

C#:

Expected output:

I owe the grocer $3.40

The “ARGV” example:

Python:

C#:

The “File System” example:

Python:

C#:

Expected output (C#):

------./Program.cs
/Users/sameehshkeer/Desktop/C# Scripts/Project/Program.cs

The “Class” example:

Python:

C#:

Expected output:

-35, True

The “CSV” example:

Python:

C#:

CSV is a weaker spot of C#, and that is due to the lack of default CSV reader and writer in the language.

However, the solution is simple: Using the powerful NuGet .Net’s package manager.

Running the following CMD from the “*.csproj” folder would give us a CSV NuGet package with a CSV reader and writer.

Expected output:

Google, Inc. is up (0.09)
Yahoo! Inc. is up (1.22)
CNET Networks, Inc. is down (-1.49)

Bottom line, Apparently, Python is still the faster, easier, and more user-friendly option when it comes to writing simple scripts.

However, if you are already a C# developer and are thinking about learning Python to write simple scripts, I assume you already have what you need, and C# should be your choice.

The Startup

Medium's largest active publication, followed by +755K people. Follow to join our community.

Sameeh Shkeer

Written by

Hi! My name is Sameeh Shkeer and I’m an Experienced Software Engineer, with a Bachelor of Science (BSc) focused in Computer Science.

The Startup

Medium's largest active publication, followed by +755K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store