Born of Windows needs
Managed code in clean design,
Type-safe, sharp, and swift.
- Haiku by ChatGPT 4o
These Bites are a linked series of brief web pages hosting presentations about the C#
programming language. They are intended to ease entry into the craft
of writing C#.
Each Bite provides an introduction to, and discusses the ideas behind, some
aspect of the language. A menu on this page (lower right - Pages) shows the things we will be
covering in this sequence.
Why C# ?
Fig 1. Point Class Definition
C# is a powerful general-purpose programming language. It supports generic programming
and object oriented programming with classes and inheritance.
It has a deep integration with the traditional Windows platform but has also been ported
to the Linux, macOS, and Android platforms.
C# has access to library facilities for building user interfaces and web applications.
That augments its well-developed capabilities for building stand-alone tools and utilities.
Things you need to know:
-
C# is designed to support value types created in stack memory, reference and dynamic types that are
managed with a garbage collector and reside in a managed heap. User-defined types are created using classes, structs,
and enumerations.
-
Reference types are created with a constructor using the new keyword. That places an instance
in the managed heap referenced with a named handle in the creator's local scope.
The handle syntax appears to indicate it has the type of the new instance, but that is not correct. It does
provide an interface equivalent to the type of created instance.
Assignment of user-defined reference types copy handles to instances
on the heap, so an assignment results in two handles pointing to the same instance.
-
The C# language compiles to Common Intermediate Language (CIL) code which is loaded into the Common Language Runtime (CLR) virtual machine.
When started the CLR compiles the CIL to native code and executes it using a stack-based protocol.
That has consequences for both latency and throughput.
Figures 1, 2, and 3 provide a sample of
how C# code is structured. Figure 1. holds some of the code for a template Point class definition.
Figure 2 shows how using code creates and uses instances of the class, and Figure 3 displays the output
when the code in Figure 2 runs.
Don't be concerned if these figures are hard to understand. We will be covering all of the
details in subsequent Bites.
C# highs
-
C# is an object oriented language, making code easier to organize, maintain, and reuse.
-
It's core is relatively simple and easy to learn. It has many add-on features, like LINQ, that can
be assimilated after developing proficiency with the core.
-
It has strong static typing for its Value and Reference types so syntax errors are evaluated at compile-time, not run-time as in
dynamically typed languages.
-
C# supports Reflection through .Net APIs for dynamic type inspection and method invocation.
-
The language has a moderately large syntax footprint and
a large collection of
standard libraries. So you can find ways within the language to meet your implementation goals.
-
Support for cross-platform development on Windows, Linux, and macOS.
-
It is widely used - a lot of currently running applications have been built with C#, especially web apps, web services, cloud clients.
There are many experienced developers with C# expertise, and the tooling has been refined
for years.
C# lows
-
Performance: C# is significantly faster than dynamic languages like Python, but uses garbage collection and a virtual machine,
so its throughput and latency are inferior to native languages like C++ and Rust.
-
Complexity: the language has a moderately large syntax footprint and a large collection of
standard libraries and frameworks. So you have to invest a lot of effort and years of experience to use
most of it effectively.
-
Low-level control: C# abstracts away a lot of the low level operations that are accessible with C++ and Rust.
C# code can bind to Component Object Model (COM) objects to get low-level access, but COM is complicated, not very well encapsulated,
and poorly documented.
References