1.2 C#: A First Taste of Managed Code
Software that is written using the .NET Framework is called Managed Code. (Legacy or traditional software is sometimes referred to as Unmanaged Code). I will define managed code later in this tutorial. But for now you should think of managed code as code that runs with the aid of an execution engine to promote the goals of Internet software. These goals include security, robustness, and object-oriented design, amongst others. Managed code is not interpreted, and does run in the native machine language of the host processor, but I am getting ahead of myself.
First things first, I would like to show a couple of examples of managed code written using the C# (pronounced see-sharp) language.
class App{
public static void Main(){
System.Console.WriteLine("Hello World!");
}
}
Figure 1‑2 HelloConsole.cs
This short application is written using the C# language. The C# language is just one of the many languages that can be used to write managed code. This source code generates a program that displays the string “Hello World!” to the command line and then exits.
using System.Windows.Forms;
using System.Drawing;
class MyForm:Form{
public static void Main(){
Application.Run(new MyForm());
}
protected override void OnPaint(PaintEventArgs e){
e.Graphics.DrawString("Hello World!", new Font("Arial", 35),
Brushes.Blue, 10, 100);
}
}
Figure 1‑3 HelloGUI.cs
This slightly longer source sample is the GUI or windowed version of the Hello World! application. It takes advantage of a few more of the features of the .NET Framework to create a window in which to draw the message string.
Both Figure 1‑2 and Figure 1‑3 are examples of complete C# applications. One of the goals of the .NET Framework is to increase developer productivity and flexibility. One important way to do this is to make software easier to write.
As you can see, the syntax of C# is an object oriented C-based syntax much like C++ or Java. This allows developers to build on previous experience when targeting the .NET Framework with their software.
Before moving on I would like to point out some very simple details to jumpstart your exposure to C#. First, C# source code is typically maintained in files with a .cs extension. Note that both Figure 1‑2 and Figure 1‑3 are labeled with .cs names indicating that they are complete, compileable C# modules.
Second, if you are writing an executable your application must define an entry point function. (Modules containing nothing but reusable components do not require an entry point, but can not be executed as stand-alone applications). With C# the entry point, if there is one, is always a static method named Main().
The Main() function can accept an array of strings as command line arguments, and it can also return an integer value. The class in which the Main() method is defined can be of any name. It is common to name this class App, but as you can see from Figure 1‑3, it is also ok to use another class name such as MyForm. When reading C# source code, look to the Main() method as a starting point.
Exercise 1‑1 Compile Sample Code
1. The source code in Figure 1‑2 is a complete C# application.
2. Type in the code or copy it from the source code distributed with this tutorial. Save it into a file with the .cs extension.
3. Use either Visual Studio .NET or the command line compiler (named CSC.exe) to compile the application into an executable.
1. Hint: If you are using the command line compiler, the following line would compile a single module into an executable.
csc /target:exe HelloConsole.cs
2. Hint: If you are using the Visual Studio IDE then you should create an empty C# project, and add your .cs file to the project. Then build it to compile the exe.
4. Run the executable.
5. Try modifying the source code a little to change the text of the string, or perhaps to print several lines to the console window.
Exercise 1‑2 Compile GUI Sample Code
1. The source code in Figure 1‑3 is a complete GUI application written in C#. It will display a window with some text on the Window.
2. Like you did in Exercise 1‑1 type in or copy the source code and save it in a .cs file.
3. Compile the source code using the command line compiler or the Visual Studio IDE.
1. Hint: If you are using Visual Studio you will need to add references for System.Drawing.dll, System.Windows.Forms.dll, and System.dll.
4. Run the new executable.
Exercise 1‑3 EXTRA CREDIT: Modify the GUI Application
1. Starting with the project from Exercise 1‑2 you will make modifications to the GUI application.
2. Make modifications to the code to draw text more than once on the window in various locations. Perhaps change the color of the text or the strings printed.
3. Consider using a for or while loop to algorithmically adjust the location of drawing the text to the screen.
Popular Posts
-
INTRODUCTION TO 'C': C is a programming language developed at AT & T's Bell laboratories of USA in 1972.it was designed by d...
-
The SQL COUNT aggregate function is used to count the number of rows in a database table. The SQL COUNT syntax is simple and looks like this...
-
The SQL AVG aggregate function selects the average value for certain table column. Have a look at the SQL AVG syntax: SELECT AVG(Column1) FR...
-
Files in VB .NET Working with Directories We will work with the File and Directory classes in this section. We will create a directory and c...
-
More Events The Form has more events besides the Form_Load event. How can you find them? Click on the Drop-Down List that found in the upper...
-
The SQL DISTINCT clause is used together with the SQL SELECT keyword, to return a dataset with unique entries for certain database table col...
-
Steps to follow: 1. Write an abstract class called LivingThing.java as shown below in Code-11.4.a. (You are welcome to do this work using ei...
-
Introduction: If you are new to Exception handling, please read "Exception Handling Statements" section of the Java Progamming Tut...
-
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms: ...
-
Lab exercises and homeworks: * Things to check before you start the lab * Chapter 3 (Class #1, Jan. 16th homework) o Exerc...
No comments:
Post a Comment