Introduction

C# is an elegant and type-safe object-oriented language that enables developers to build a variety of secure and robust applications that run in the .NET ecosystem. The .NET ecosystem is composed of all the implementations of .NET, including both but not limited to .NET Core, and .NET Framework. This article focuses on .NET Framework. You can use C# to create Windows client applications, XML Web services, distributed components, client-server applications, database applications, and much, much more.

As an object-oriented language, C# supports the concepts of encapsulation, inheritance, and polymorphism. All variables and methods, including the Main method, the application's entry point, are encapsulated within class definitions. A class may inherit directly from one parent class, but it may implement any number of interfaces. Methods that override virtual methods in a parent class require the override keyword as a way to avoid accidental redefinition. In C#, a struct is like a lightweight class; it is a stack-allocated type that can implement interfaces but does not support inheritance.

In addition to these basic object-oriented principles, C# makes it easy to develop software components through several innovative language constructs, including the following:

  • Encapsulated method signatures called delegates, which enable type-safe event notifications.
  • Properties, which serve as accessors for private member variables.
  • Attributes, which provide declarative metadata about types at run time.
  • Inline XML documentation comments.
  • Language-Integrated Query (LINQ), which provides built-in query capabilities across a variety of data sources.
.NET Framework Platform Architecture

C# programs run on the .NET Framework, an integral component of Windows that includes a virtual execution system called the common language runtime (CLR) and a unified set of class libraries. The CLR is the commercial implementation by Microsoft of the common language infrastructure (CLI), an international standard that is the basis for creating execution and development environments in which languages and libraries work together seamlessly.

Source code written in C# is compiled into an intermediate language (IL) that conforms to the CLI specification. The IL code and resources, such as bitmaps and strings, are stored on disk in an executable file called an assembly, typically with an extension of .exe or .dll. An assembly contains a manifest that provides information about the assembly's types, version, culture, and security requirements.

In addition to the run time services, the .NET Framework also includes an extensive library of over 4000 classes organized into namespaces that provide a wide variety of useful functionality for everything from file input and output to string manipulation to XML parsing, to Windows Forms controls. The typical C# application uses the .NET Framework class library extensively to handle common "plumbing" chores.

Hello World

Run the following code in the interactive window. Select the Enter focus mode button. Then, type the following code block in the interactive window and select Run:

Console.WriteLine("Hello World!");

Congratulations! You've run your first C# program. It's a simple program that prints the message "Hello World!". It used the Console.WriteLine method to print that message. Console is a type that represents the console window. WriteLine is a method of the Console type that prints a line of text to that text console.

Declare and use variables

Your first program printed the string "Hello World!" on the screen.

Your first program is limited to printing one message. You can write more useful programs by using variables. A variable is a symbol you can use to run the same code with different values. Let's try it! Replace the code you've written in the interactive window with the following code:

string aFriend = "Bill";
Console.WriteLine(aFriend);

The first line declares a variable, aFriend and assigns it a value, "Bill". The second line prints out the name.

The first line declares a variable, aFriend and assigns it a value, "Bill". The second line prints out the name.

aFriend = "Maira";
Console.WriteLine(aFriend);

Notice that the same line of code prints two different messages, based on the value stored in the aFriend variable.

You may have also noticed that the word "Hello" was missing in the last two messages. Let's fix that now. Modify the lines that print the message to the following:

Console.WriteLine("Hello" + aFriend);

Select Run again to see the results.Select Run again to see the results.

You've been using + to build strings from variables and constant strings. There's a better way. You can place a variable between { and } characters to tell C# to replace that text with the value of the variable.

This is called String interpolation.

If you add a $ before the opening quote of the string, you can then include variables, like aFriend, inside the string between curly braces. Give it a try:

Console.WriteLine($"Hello {aFriend}");

Select Run again to see the results. Instead of "Hello {aFriend}", the message should be "Hello Maira".

Work with strings

Your last edit was our first look at what you can do with strings. Let's explore more.

You're not limited to a single variable between the curly braces. Try this:

string firstFriend = "Maira";
string secondFriend = "Sage";
Console.WriteLine($"My friends are {firstFriend} and {secondFriend}");

As you explore more with strings, you'll find that strings are more than a collection of letters. You can find the length of a string using Length. Length is a property of a string and it returns the number of characters in that string. Add the following code at the bottom of the interactive window:

Console.WriteLine($"The name {firstFriend} has {firstFriend.Length} letters.");
Console.WriteLine($"The name {secondFriend} has {secondFriend.Length} letters.");
Types variables and values

C# is a strongly typed language. Every variable and constant has a type, as does every expression that evaluates to a value. Every method signature specifies a type for each input parameter and for the return value. The .NET Framework class library defines a set of built-in numeric types as well as more complex types that represent a wide variety of logical constructs, such as the file system, network connections, collections and arrays of objects, and dates. A typical C# program uses types from the class library as well as user-defined types that model the concepts that are specific to the program's problem domain.

The information stored in a type can include the following:

  • The storage space that a variable of the type requires.
  • The maximum and minimum values that it can represent.
  • The members (methods, fields, events, and so on) that it contains.
  • The base type it inherits from.
  • The location where the memory for variables will be allocated at run time.
  • The kinds of operations that are permitted.

The compiler uses type information to make sure that all operations that are performed in your code are type safe. For example, if you declare a variable of type int, the compiler allows you to use the variable in addition and subtraction operations. If you try to perform those same operations on a variable of type bool, the compiler generates an error, as shown in the following example:

int a = 5;
int b = a + 2; //OK
bool test = true ;
// Error. Operator '+' cannot be applied to operands of type 'int' and 'bool'.
int c = a + test;

The compiler embeds the type information into the executable file as metadata. The common language runtime (CLR) uses that metadata at run time to further guarantee type safety when it allocates and reclaims memory.

Reference