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/