C#.NET Programming Language

C# (pronounced see sharp) is the flagship language for the Microsoft .NET platform.  It is a member of the C family of languages which currently includes:

image C# was created by Anders Hejlsberg at Microsoft.  Anders is originally known by his work at Borland where he created the Turbo Pascal programming language, then later the Object Pascal language provided in the product “Delphi” and the BCL (Borland Class Library).  Delphi and the BCL turned out to be essentially the precursor to C# and the .NET framework (also called the BCL “Base Class Library”).  Not surprisingly, a .NET program looks and smells an awful lot like a Delphi program.

The purpose of the C# programming language was to give theimage programmer everything they liked about C++, but remove the headaches caused by things such as pointers, function pointers, and memory management, to name a few.

Unlike C++, C# programs are compiled to IL or MSIL (Microsoft Intermediate Language).  This is a byte code language not targeted to any real CPU, but targeted to the .NET runtime.  This is similar to how Java programs are compiled to byte code to run in the Java Virtual Machine.  C# programs are “managed” programs, meaning they don’t get free reign of the image computing environment in which they run.  The .NET runtime has control over the program and can actively deny the program any activity the program is attempting, such as writing to disk, accessing the network, etc…  This makes C# a safe language, in comparison to C++.  It also helps prevent common programmer mistakes with regards to memory management.

image Even though C# was created at Microsoft, Microsoft does not own the language, like it does the proprietary Visual BASIC language.  It handed control over to the ECMA standards body and any changes Microsoft intends to make to the language must be submitted to the ECMA standards body for approval.  This also means that anyone is free to create their own C# implementation.  And, in fact, others have.  The first was Borland, with their C# Builder product.  The most popular being the Mono project, which is a complete rewrite of the .NET technology stack for Linux and Mac.

image Using C#, a developer can create software for Windows desktops, Windows services, web services, web applications, mobile applications, Linux, Mac, iPhone, XBox 360, Zune, and many embedded devices, to name just a few.

The 1.0 version of the .NET framework was written entirely in the C# programming language.  Later versions of the .NET framework have a few pieces also written in VB.NET.

Objective C

image Objective C is a programming language based on the classic “C” language, with object oriented capabilities added to it.  It is not the next version of C.  C++ is another language based on C that many people consider as “the next version of C”.  But that is debatable.  Objective C came out much later than C++.

Objective C was originally created for the NeXT Step PC, which is the computer system touted by Steve Jobs after he was fired from Apple Computer.

imageToday, Objective C is primarily used for writing software for the Macintosh and the iPhone.image

The language is designed to provide Smalltalk like messaging to a C like language.  It is an object oriented language, but it is noticeably different from C++.

Event Handlers

When a piece of software responds to an external event, such as a mouse clicking on a button, a game controller pressed in a game, a key pressed on a keyboard, or the tick of the system clock, the subroutine that responds to that event is called an event handler.

For example, imagine a Windows application with nothing but an “OK” button in it.  When you click it, it displays a message box with “Hello” in it.

image

Here’s what the event handler code looks like for the “OK” button on the window titled “Form1” above: (this is C# code)

Code Snippet

  1. private void OKButton_Click(object sender, EventArgs e)
  2. {
  3.     MessageBox.Show(“Hello”);
  4. }

Any subroutine of code that is automatically triggered by a user or machine initiated event is called an event handler.

MSDN: Microsoft Developer Network

image The Microsoft Developer Network is a subscription service for software developers.  There are multiple tiers of subscriptions, each higher tier providing more services and products than the lower tier.

In general, most MSDN subscriptions provide access to most of the Windows operating systems for servers and desktops, Visual Studio for programming, SDKs, and SQL Server databases.  Periodically, Microsoft will send out refreshed DVDs to the subscribers with the latest versions of these products.

The MSDN website also allows its subscribers to download any of the software they have rights to in their subscription, so they don’t have to wait for the DVDs to be delivered.

GUI: Graphical User Interface

A GUI (or Graphical User Interface) is a visual display of information and user manipulatable controls on a display device.  This is opposed to a textural user interface that can only display plain text.  Textural user interfaces usually require the user to type in commands for all interactivity.  GUIs usually allow for both keyboard entry as well as mouse user input.  Newer GUIs are now supporting touch and even multi-touch interfaces.

Anyone using a modern operating system should be familiar with a GUI.  It’s all those windows, buttons, menus and such that make up your computer’s desktop interface.  This is true whether you’re using Mac, Windows, Linux, TV Game Console (like XBox 360, Playstation, or Nintendo), or even a smart phone.

Operating system makers like to name their GUIs.  For example, the Mac OSX GUI is called “Aqua”.  The Windows Vista and Windows 7 GUI is called “Aero Glass”.  On Linux, there are many competing GUIs, but the two most popular are Gnome and GTK.  To the average user, most of the user interfaces look very similar because they all provide the same, basic components (windows and controls like buttons, lists, radio buttons, drop down list boxes, etc…).

Visual Component Development

When a programmer writes a new piece of software, he may need a new type of visual control that doesn’t yet exist.  Perhaps the standard set of controls (buttons, radio buttons, drop down list boxes, text boxes, etc…) don’t have the specific functionality that the programmer needs to present to his users.  When this is the case, the programmer may design and code a totally new visual component with new functionality.

Win32

image

“Win32” is the term used when discussing Windows 32 bit operating systems or technology.  In the context of programming, it usually refers to the API for programming directly to the Windows low level functions and data structures.

Before Win32 was Win16.  Windows 3.11 (the version before Windows 95) was the last version of Windows before Microsoft added 32bit capability to all of their desktop and server operating system.  The function calls and data structures in 16 bit were very similar to the ones in 32 bit Windows.  As a matter of fact, most of the Win16 APIs are still available in the latest versions of Windows, but only for backward compatibility.  No one writes software these days that depends on the 16 bit APIs in Windows.  32 bit Windows has been around since the early 1990’s, starting with Windows NT around 1993.

For nearly 10 years, 64bit versions of Windows have been around, though few people have been using them.  When Microsoft first made 64bit versions of Windows available, only the most expensive, top of the line CPU chips were capable of running them, therefore, few developers wrote code specifically for 64 bit.

Today, “Win32” is just the generic term for Windows APIs, even when discussing 64 bit programming.

These days, it’s hard to find a CPU chip that’s not fully 64 bit, but most people still choose to install the 32bit version of Windows XP, Vista, or Windows 7.  But, since Vista was released, more and more people are switching to 64 bit versions of Windows.  They’d been avoiding it because there were compatibility problems with older software that wasn’t written right and as a result, failed under 64 bit windows.  Most modern software runs just fine under 64 bit Windows now. 

Since the release of Windows 7, Microsoft has tightened up the Windows Logo requirements so that any software requesting the Windows Logo rights must pass a rigorous 64 bit test.  Microsoft is heavily pushing 64 bit Windows.  It’s about time too, because 32 bit Windows is incapable of using more than 4GB of RAM.  Up to a couple of years ago, that wasn’t an issue because until then, it wasn’t even possible to have that much memory.  But, with the continually rapid pace of technological advancement, anyone can get memory for less than $30 per GB.  Computers with more than 4GB are not only possible today, but common place.  To use the rest of your memory, you MUST have a 64bit operating system.

SDK: Software Development Kit

A software development kit can be many things, but generally, it’s a package of source files, usually that define new data types and subroutines and classes to make programming for a particular technology easier.

For example, to write programs for Windows using C++, you imageneed information on what Windows functions and data structures are available, then you’d need to write C++ code to those specifications so that you can call those methods and you’d need to write code that defines the data structures that those methods expect you to pass to them.

imageFortunately, Microsoft has provided a Windows SDK that has all of this already written.  Now, all you need to do is reference those files from your own source files, then those complex data structures and function calls are available for your program to code against, without you having to reinvent the wheel.

image There are SDKs for plenty of other technologies as well, and not just for operating system level stuff.  There are SDKs for writing games.  For example, to write “Games for Windows” (a generic sounding label for a branded technology), Zune, andimage XBox 360, you can download the XNA Framework,  which is an SDK for writing those types of games.  That SDK greatly simplifies writing games for those three platforms.

There are thousands of SDKs for multiple platforms for multiple products for multiple technologies.  Some SDK include more than just source code.  Some include already compiled libraries and/or end user (for programmers) utilities.

MFC – Microsoft Foundation Classes

image Microsoft Foundation Classes are a set of C++ classes provided by Microsoft and distributed with the Windows SDK (Software Development Kit).  The Windows SDK is included with the Microsoft Visual Studio IDE too.

Prior to MFC, writing programs for Windows was incredibly difficult by today’s standards.  A programmer had to become intimately familiar with many of the global Win32 API function calls and data structures embedded in the Windows Operating System.  This was very cumbersome and had a very high learning curve and was very prone to error.  For example, to write code to display simply a blank window, took about 50 lines of code.

MFC is a set of C++ classes that wrap around (or hide or abstract) the cryptic Win32 API function calls.  So, instead of creating multiple, archaic data structures, populating their large amount of fields with data, and passing them to the multiple Win32 function calls in the OS, you can now just instantiate an object of type CWindow, set a few properties, and call a method or two to display your window.  It greatly simplified Windows programming.

Speaking of improvements to Windows development, it would be irresponsible to leave out .NET development.  Programming in C# with the .NET framework greatly simplifies Windows development by an order of magnitude beyond what MFC provided with C++.

C++ Templates

A template is a special type of class definition in the C++ language.  Normally, in C++, when a programmer creates a new type of data structure in the form of a class, the programmer can write code to declare a new variable of that new type.  For example, the following C++ code declares a new data type called “EmployeeType”:

Code Snippet

  1. class EmployeeType
  2. {
  3.     public:
  4.         EmployeeType(void);
  5.         ~EmployeeType(void);
  6.         char * FirstName;
  7.         char * LastName;
  8.         double salary;
  9. };

Without templates, to be able to use this type in a complex data structure, such as a list, queue, or stack, you’d have to create your own class and write all of the functionality to add and remove items.  If you wanted the same functionality for a type other than EmployeeType, you’d have to rewrite it all again.

A class is essentially a blueprint for an object.  A template is a a blueprint for a class.  For example, the following is a custom template:

Code Snippet

  1. template <class T> class PrintableTemplate
  2. {
  3.     public:
  4.         int GetSize();
  5. };

You can’t declare a variable of type PrintableTemplate, but you can declare a new class or type based on this template, like so:

Code Snippet

  1.     PrintableTemplate PrintableEmployee;

This line of code does two things:

  1. Causes the compiler to generate a new class based on EmployeeType, but adds the GetSize() method.
  2. Instantiates a variable of this new type.

Don’t confuse this new type with a new class that’s derived from EmployeeType.  This is a whole new class.  Note that you can now call methods directly on this type that came from the template:

Code Snippet

  1.     PrintableTemplate PrintableEmployee;
  2.     PrintableEmployee.GetSize();

In the early days of C++ templates, there were no standard templates available, so if you wanted to have a list, queue, or stack based on one of your own, custom types, you’d have to write the whole template yourself.  A couple years after templates were introduced to C++, the ANSI committee approved of a standard template library (STL) that provided the majority of templates that you’d need.  Since then, there are few times when you need to write your own templates, but you still can, of course.

That’s the short description of C++ templates.

C# is modeled somewhat on the C++ language and as of C# 2.0 (2005), the C# language has supported templates (as well as the .NET runtime itself).  In C# though, they’re called “generics” rather than “templates”, but they are virtually identical in capabilities and the syntax is very very similar as well.