Last Modified 5-1-2010

IUSB

Home

C#


Introduction to C#

.NET Framework

VS2008 IDE

How C# is Compiled

Start Visual Studio


Windows Form App


C# OOP Coding

C# Code Example


C# Coding Rules

Recommendations

Error Window

C# Comments

C# Help Window

Language Essentials

C# Data Types

Declare Variables

Declare Constants

Arith. Expressions

Assign Statements


Operator Precedence

Type Casting

Math Class

C# Strings

String Esc Sequences

Convert Data Types


ToString Formatting

Variable Scope Use

C# Enumeration Use

C# Nullable Types

Program #2


C# Code Control

Relational Operators

Logical Operators

If-Else Statement

Switch Statement


C# Programming Introduction

You can use Visual Studio for developing the two types of applications:

  1. A Windows Forms Application: A WinForms app is a type of Windows Application that runs on the user's PC. Each Windows form in the application provides a user interface that lets the user interact with the application.
    • As part of the user interface, a Windows Forms application uses Windows forms controls. For instance, the form controls can use radio buttons, labels, text boxes, and buttons. In the next section, we begin to develop Windows Forms Applications.
  2. Web Forms Applications: Like a Windows Forms Application, a web application consists of one or more web forms that can contain controls. Unlike Windows forms, web forms are accessed by and displayed in a Web Browser such as Internet Explorer, Firefox and Google Chrome.
    • As part of the user interface, a web form uses Web Form Controls. These controls are similar to the Windows Forms controls, but they work only with web forms.
    • In contrast to a Windows Forms application, which runs on the user's PC, the code for a Web Forms application runs on a web server. As this code is executed, it passes the visual portion to the browser running on the client in the form of HTML. The browser then interprets the HTML and displays the form.

The .NET Framework

A more detailed view of the .NET Framework is given in Figure CS-1:

Net Framework

Figure CS-1: Detailed View of Microsoft .NET Framework


Description

  • Windows Forms applications do not access the operating system or computer hardware directly. Instead, they use services of the .NET Framework, which in turn accesses the operating system and hardware.
  • The .NET Framework consists of two main components: the .NET Framework Class Library and Common Language Runtime.
  • .NET Framework Class Libary: Provides files that contain pre-written code known as classes that are available to all of the .NET programming languages. This class library consists of thousands of classes, but you can create simple .NET applications once you learn how to use just a few of them.
  • Common Language Runtime: Manages the execution of .NET programs by coordinating esssential functions such as memory management, code execution, security, and other services. Because .NET applications are managed by the Common Language Runtime, they are called managed applications.
  • Common Type System: A component of the Common Language Runtime that ensures that all .NET applications use the same basic data types no matter what programming languages are used to develop the applications.

As you can see, the .NET Framework provides a common set of services that application programs written in a .NET language such as C# can use to run on various operating systems and hardware platforms. The .NET Framework is divided into two main components:

  1. The .NET Framework Class Library.
  2. The Common Language Runtime.

The .NET Framework Class Library consists of segments of pre-written code called classes that provide many of the functions that you need for developing .NET applications. For instance, the Windows Forms that you need for developing .NET applications. For instance, the Windows Forms classes are used for developing Windows Forms applications. The ASP.NET classes are used for developing Web Forms applications. And other classes let you work with databases, manage security, access files, and perform many other functions.

Although it's not apparent in this figure, the classes in the .NET Framework Class Library are organized in a hierarchical structure. Within this structure, related classes are organized into groups called namespaces. Each namespace contains the classes used to support a particular function. For example, the System.Windows.Forms namespace contains the classes you use to access data.


Visual Studio Integrated Development Environment IDE

Visual Studio IDE

Figure CS-2: Visual Studio Integrated Development Environment IDE


Figure CS-2 shows the Microsoft Visual Studio Integrated Development Environment. In practice, this IDE if often referred to as Visual Studio. Microsoft Visual Studio supports 3 languages: Visual Basic, Visual C#, and Visual C++.

Microsoft Visual Studio includes designers that can be used to design the user interface for Windows Forms and Web Forms applications. The visual tools make this tough task much easier. You'll be introduced to the designer for Windows Forms.

Visual Studio also includes an editor that can be used to work with any of the three languages presented in Figure CS-1 as well as HTML and XML. This editor contains many features that make it easy to enter and edit the code for an application.

Description

  • The Visual Studio IDE is often referred to as Visual Studio, even though that name is also used to refer to the suite of products, including the .NET Framework.
  • Visual Studio supports Visual Basic, Visucal C++, and Visual C#.
  • Visual Studio includes designers that can be used to develop Windows Forms applications and Web Forms applications.
  • Visual Studio includes a code editor that can be used to work with all 3 languages as well as HTML and XML.

How A C# Application is compiled and Run

C# Compiled & Run

Figure CS-3: How a Visual Studio C# Program is Compiled and Run



Figure CS-3 shows how a C# program is compiled and run. To start, you use Visual Studio to create a project, which is made up of source files that contain C# statements. A project may also contain other types of files, such as sound, image or text files.

After you enter the C# code for a project, you use the C# Compiler, which is built into Visual Studio, to build or compile your C# source code into Microsoft Intermediate Language.

At this point, the Intermediate Language is stored on disk in a file that's called an assembly. In addition to the Intermediate Language, the assembly includes references to the classes that the application requires. The assembly can then be run on any PC that has the Common Language Runtime installed on it. When the assembly is run, the Common Language Runtime converts the Intermediate Language to native code that can be run by the Windows operating system.

If you have developed applications with other languages, this process should be familiar to you. If this is your first language, though, you will not really understand this process until you develop your first applications.

Incidentally, a solution is a container that can hold one or more projects. Although a solution can contain more than one project, the solution for a simple application usually contains just one project. In that case, the solution and the project are essentially the same thing.

Operation

  1. The programmer uses Visual Studio to create a project which includes C# source files. In some cases, a project will also contain other types of files, such as graphic image or sound files.
  2. The C# compiler translates the C# source code for a project into Microsoft Intermediate Language. This language is stored on disk in an assembly that also contains references to the required classes. An assembly is an executable file that has an .exe or .dll extension.
  3. The assembly is run by the .NET Framework's Common Language Runtime. The Common Language Runtime manages all aspects of how the assembly is run, including converting the Intermediate Langauge to native code that can be run by the operating system, managing memory for the assembly, and enforcing security.

How C# differs from the other .NET languages

C# uses the same .NET Framework classes as the other .NET programming languages. These classes affect almost every aspect of programming, including creating and working with forms and controls, using databases, and working with basic language features such as arrays and strings. In addition, C# works has many similarities to the other .NET languages. The main difference is the syntax of the language.

How C# differs from Java

The C# language uses a syntax that's similar to the syntax for the Java language. However, Java relies on a different framework of supporting classes and the development environments for working with Java are different than Visual Studio. As a result, if you have experience with Java, it should be easy for you to learn the C# language. However, if Visual Studio and the .NET Framework classes are new to you, it may take some time to learn how they work.

A Tour of the Visual Studio Integrated Development Environment

With all that as background, we're ready to take a tour of the Visual Studio IDE. Along the way, we'll touch on some basic techniques for working in this environment. We'll also go over how some of the terms we just went over are applied within the Integrated Development Environment.

How To Start Visual Studio

Select Start → All Programs from your Windows main screen as indicated in the diagram below:

Start All Programs

Figure CS-4: Start → All Programs



All programs will display a list of Program folders:

  1. Expand the Visual studio Folder by left-clicking the folder icon.
  2. Start the Visual Studio Application by left-clicking the Visual Studio icon as indicated in the diagram below:

Visual Studio Application

Figure CS-5: Start Visual Studio Application


When the Visual Studio program starts the Start Screen should look similar to the diagram below:

Visual Studio Start Screen

Figure CS-6: Visual Studio Start Screen