Introduction
C# is a type safe object oriented language typically utilized with the .NET framework.
It allows for the creation of client applications, web services, client-server applications, and database applications. It supports encapsulation, polymorphism, inheritance, pointers, “unsafe” code (direct memory access), and interoperability. Class definitions encapsulate all variables and methods. Classes can only inherit from a single parent, but implement any amount of interfaces. A C# source file can define any amount of structs, events, classes, or interfaces.
This simple language allows for a high level of expression, and its syntax should be familiar to C, C++, and Java developers. It offers many C++ features in a simplified way delivering things such as nullable value types, lambda expressions, and direct memory access; features not found in Java.
C# simplifies the development of software components through its unique constructs:
- Encapsulated method signatures known as delegates
- Properties serving as means of access for private variables
- Attributes providing declarative metadata at runtime
- Inline XML comments
- LINQ built-in queries
The interoperability process allows for interaction with Windows software (e.g., drivers). C# programs also achieve the same capability of C++ programs through interoperability. The build process for C# offers more flexibility than Java, and proves more simple than C or C++. It does not use header files or require a specific order for the declaration of methods and types.
Many frameworks support C#, but most developers opt for .NET. C# applications using .NET are powered by its CLR (Common Language Runtime). CLR, Microsoft's implementation of the common language infrastructure (CLI), allows for the creation of runtime and development environments supporting a seamless execution relationship between libraries and languages. In compilation, C# code becomes an intermediate language conforming to CLI. The IL code and its resources are stored as .exe or .dll files. These files contain all necessary information related to the application: types, culture, version, and etc. When executed, the file loads into CLR. If its conditions are satisfied, the code becomes machine code. CLR provides more than a runtime environment, it also performs garbage collection, exception management, and resource handling. CLR, also known as “managed” code, does not target a specific machine or environment.
VISUAL STUDIO
Though many other options exist, the most used tool in C# development is Microsoft's Visual Studio. It includes a wide variety of tools supporting and facilitating virtually every aspect of C# development. Visit its official website to download the software. It offers a simple, guided install.
Visual Studio contains the following components:
- Start page – This initial screen provides a selection of options related to development such as shortcuts to beginning a project, important news, and more.
- Solution Explorer – This lists all project files.
- Output – This details the status of Visual Studio actions and features.
- Class View – This details classes, methods, and properties of a file.
- Code Editor – This provides a text editor for authoring code.
- Error list – This lists errors and their believed source after an attempted compilation, or during code production.
Compile an application by selecting Build > Build Solution. Execute by pressing F5; clicking the “Start Debugging” button on the toolbar; or selecting Debug > Start Debugging. Stop execution by pressing Shift+F5; clicking the “Stop Debugging” button on the toolbar; or selecting Debug > Stop Debugging.
Console applications (applications without a GUI) can be created in Visual Studio:
- Start a project and select the “Console Application” template.
- Then simply code, compile, and execute normally.
GENERAL STRUCTURE
C# applications are composed of one or more files, and each file holds zero or more namespaces. Namespaces hold types (e.g., structs, classes, interfaces, and more) as well as namespaces. Review the typical code structure of a C# application below:
using System;
namespace ANamespace
{
class AClass
{
}
struct AStruct
{
}
interface AnInterface
{
}
delegate int ADelegate();
enum AnEnum
{
}
namespace ANestedNamespace
{
struct AStruct
{
}
}
class TheMainClass
{
static void Main(string[] args)
{
//The application substance
}
}
}
HI UNIVERSE
Review the “Hi Universe” application below:
// A Hi Universe! program
using System;
namespace HiUniverse
{
class Hi
{
static void Main()
{
Console.WriteLine("Hi Universe!");
//Ensure the window stays open
Console.WriteLine("Hit any key to close.");
Console.ReadKey();
}
}
}
This line of code demonstrates the syntax for comments, which requires comments to either be enclosed by “/*” and “*/” or preceded by “//”:
// A Hi Universe! program
This block of code demonstrates the required Main method, which houses the creation of objects and execution of methods, and returns either void or an integer (both allow arguments):
static void Main()
{
Console.WriteLine("Hi Universe!");
//Ensure the window stays open
Console.WriteLine("Hit any key to close.");
Console.ReadKey();
}
The call to ReadKey causes the program to pause and wait for a key event before closing the console window, which allows the user time to review output.
Console.ReadKey();
This statement uses the WriteLine method for input/output services:
Console.WriteLine("Hit any key to close.");
This line provides the Console class from which the application draws the method, and helps to avoid excessively verbose references to a class within code:
using System;