Showing posts with label Programming Languages. Show all posts
Showing posts with label Programming Languages. Show all posts

This is intended to be a short introduction to the C# language. I won’t go into syntax or cover the principles of Object-Oriented Programming but I will be describing the language through reference to other languages.

Please let me know if there’s any confusion and I will edit the post suitably.

So… you want to learn C#.

Me too!

I’ve started learning C# through Udemy courses and books (links for each at the end of the article) with the intent of developing multi-platform applications. But enough about me, on to what you really care about.


C# (C Sharp)

C# was developed by Microsoft as an Object-Oriented version of the C language for their .NET Framework (be on the lookout for separate posts on OOP and the .NET Framework in the future).

C# is statically-typed (or strongly-typed), which means that every variable and object has a well-defined type (Ky, 2013).

Every variable and constant has a type, as does every expression that evaluates to a value.

Methods have unique signatures which specify the type(s) of input parameters that they can accept and also what type of value they return.

Statically-typed languages check to make sure all operations use the appropriate types at compile time. 

C# is a High-Level Language (HLL)

Programming languages can be categorized based on their level of abstraction from the details of the computer.

Original computers relied on a series of binary switches that were configured manually or according to a program of 0s and 1s or binary digits (bits). This is not always the case with the onset of quantum processors which utilizes a superposition which operates on an entirely different paradigm. While it would be possible to build every program using 0s and 1s, it would be very difficult for most people to read (Vahid & Lysecky, 2017).

This led to the development of assembly language which was a little easier to read, that is closer to the way humans communicate. Assembly language compiles to (translates to) the binary machine code (0s and 1s) which your computer understands.

Assembly language can be thought of as being one level of abstraction from the computer.

Abstraction?

Think of it like your smartphone. It has only a few buttons on it. You know WHAT those buttons do but don’t need to know HOW they do it.

Make sense? Think of it like Back to the Future…

Marty McFly didn’t need to understand how Doc’s Delorean Time-Machine worked or what a Flux Capacitor does. He simply needed to know how to turn it on, put in a date, and floor it until 88mph! The details which made this amazing machine work were abstracted from Marty.
(Gale & Canton, 1985).



/*Abstraction is also a fundamental aspect of Object-Oriented Programming which can be explained in a similar manner. More on this in a later post. This is also how to use a block comment in C#!*/

//This a single-line comment in C#!

As the field of software development evolved, languages like COBOL, Fortran, and others began to crop up with the same intent as the Assembly language. They were easier for people to read and write. C# doesn’t have the programmer directly dealing with details of the computer. The programmer doesn't need to know how what's going on behind the scene that enables her program to work. That said, knowing can be very valuable. 

Languages similar to C#:
  • C
  • C++
  • Java

C# was derived from C and also incorporated some aspects of C++ and Java. If you’ve written code in any of these languages, C# syntax shouldn’t be terribly difficult to pick up on (Ky, 2013).
Unlike C++ and C, C# handles garbage collection for us! This means that memory management or direct memory manipulation isn’t necessary.

You see, processors have limited amounts of memory (cache) and use Random Access Memory (RAM) as a buffer. The more complicated an operation or series of operations is, the more often it needs to use the RAM as a buffer. But the RAM isn’t accessed as quickly as the processor’s cache (Vahid & Lysecky, 2017).

Some languages require the direct manipulation of memory, assigning variable values to specific addresses and more. In C++ raw pointers, variables which hold an address in memory, were often used and required that the pointers be deleted when they were no longer needed. But the ways that values were passed as method arguments often resulted in memory leaks – or loss of access to portions of memory because of declared, unused, un-deleted pointers. The introduction of smart pointers (self-deleting pointers) helped to alleviate this but with C# memory management requires nearly no developer-involvement (Microsoft, 2019).

This memory management is referred to as garbage collection.



Furthermore, where you might see multiple inheritance in other languages like C++, C# does not support multiple inheritance.

In a personnel management software we might have two classes Faculty and Student that have specific characteristics (like PayRate for Faculty and TuitionRate for Students).

But what about a Student Aide that’s on the payroll?

In C++ you could have a StudentAide class which inherits properties from both the Faculty and Student classes.

Not so in C#...

The issue is that multiple inheritance can caused what referred to as the “diamond problem”. Essentially if both the Faculty and Student classes had properties or methods with the same name, the compiler wouldn’t be able to determine which one to use when a new instance of a StudentAide object occurred.

C# Doesn’t Compile to Executable Code – at least not directly

Yep, you read that correctly.

Because C# was designed to work within the .NET Framework, C# doesn’t compile to machine-executable code, at least not directly. The .NET Framework enables a C# developer to code once and push to multiple platforms (Windows, iOS, Android, Xbox, and more). In order to enable this, code written in C# is compiled to Common Intermediate Language (CIL) which can only run on the Common Language Runtime (CLR). 

“The CLR is a native application that interprets CIL code” and then compiles that into the appropriate native code for the platform it’s on (Ky, 2013).

I’ll go into a bit more detail on the .NET Framework, C# syntax, OOP and other topics covered above at a later date. But for now, I hope that this has given you a general idea of what C# is.

Important takeaways:
C# is an Object-Oriented Programming Language (OOP)
C# is statically typed
C# is a high-level language
C# handles garbage collection!
C# doesn't support multiple inheritance
C# doesn't compile to machine-executable code
C# compiles to Common Intermediate Language (CIL)
CIL on runs on Common Language Runtime (CLR)
CLR compiles to the native code for the appropriate platform


References:
Gale, B. & Canton, N. (1985). Back to the Future. Universal Studios.

Ky, J. (2013). C#: A beginner’s tutorial. [Montreal, CAN]: Brainy Software Inc., 2013. Retrieved from https:://search.ebscohost.com.proxy-library.ashford.edu/login.aspx?direct=true&db=ca t02191a&AN=aul.10882077&site=eds-live&scope=site

Microsoft. (4 April 2019). A Tour of the C# Language. Retrieved from https://docs.microsoft.com/en-us/dotnet/csharp/tour-of-csharp/index

Vahid, F., & Lysecky, S. (2017). Computing technology for all. Retrieved from zybooks.zyante.com/



Pete the Piranha Scratch Project: https://scratch.mit.edu/projects/317422602/

Compelled to do so by an assignment for my INT100 coursework, I created a relatively simple game in MIT's block language Scratch. The link to the project is above but I enjoyed making the game and it appears that my wife, kids, friends, and coworkers are all fans. So it's inspired me to recreate the game as a mobile app! Will provide more info on this as work on that app progresses. 

Having made simple programs before, Scratch proved to be a bit of a headache at for two reasons.
  1. There’s a learning curve associated with the graphic user interface and the specific limitations of the language.  
  2. Having to hunt for the appropriate block(s) when I could have typed out the code was frustrating.

How did I get around these issues?

These difficulties were primarily solved by a thorough use of Google and Youtube, as well as looking at projects that other people have shared. What I gained from this experience were some insights into how modern games are coded and how variables in the program are manipulated to provide some of the mechanics for the game.

Assessment of the languages
It’s plainly evident that Scratch makes programming many simple programs significantly easier. The exercises involving Python, named for Monty Python, were my first real experience programming in the language and, to be frank, it also took some getting used to. Given my recent self-education in C++ and C#, the differences in syntax required constant attention. That said, I would prefer Python over Scratch for most software development projects given what I can only assume are numerous limitations that Scratch has as a very high-level language.

The differences in these languages stem from the era of their development. Machine language is a means of communicating with a CPU in binary. Humans have had thousands of years to develop languages comprised of thousands of symbols and phonemes in order to communicate complex messages – while communication of this nature is possible in binary it is very difficult. As such, Assembly language was developed to make it easier for programmers to instruct CPUs in a human-readable manner.  

Python is a comparatively new high-level language which utilizes indentation, common amongst programmers for increased readability, instead of curly-brackets to structure its programs and scripts into blocks and it significantly easier to work with than Assembly language. Scratch is a block programming language created to help children learn to break problems into chunks of logic and apply that logic toward the creation of programs – that is, to help children learn to think like programmers.

Machine code is used anywhere a traditional computer is in use – higher-level languages are translated into machine code. This includes assembly language which is typically found in drivers and the like. While you could accomplish in assembly anything you could with higher level languages, it becomes impractically long and difficult to debug. Python, on the other hand, can be used to create desktop programs, web applications and more. Recently, it has gained popularity as a language for data analysis. Scratch is useful for its intended purpose – helping newcomers to the art of programming learn to think in a way that enables them to create programs from code.

Which language is most popular? – That depends on how popularity is defined. 

Of those discussed, Machine language is certainly the most omnipresent (as nearly all programs are eventually converted into those ones and zeros), however Python is most likely to be selected for nearly any software development project not requiring direct processor interaction. It’s comparative ease of use without the limitations of a language like Scratch make it ideal for most development (in comparison to the languages discussed herein).