Flylib.com

Books Software

 
 
 

Application Development Using C# and .NET - page 33

for RuBoard

Summary

.NET solves the problems that have plagued Windows development in the past. There is one development paradigm for all languages. Design and programming language choices are no longer in conflict. Deployment is more rational and includes a versioning strategy. While we will talk more about it in later chapters, metadata, attribute-based security, code verification, and type-safe assembly isolation make developing secure applications much easier. The plumbing code for fundamental system services is provided, yet you can extend or replace it if you must.

The Common Language Runtime provides a solid base for developing applications of the future. The CLR is the foundation whose elements are the Common Type System, metadata, the Common Language Specification, and the Virtual Execution System (VES) that executes managed code. [14] As we shall see in future chapters, .NET makes it easier to develop Internet applications for both service providers and customer-based solutions. With the unified development platform .NET provides, it will be much easier than in the past for Microsoft or others to provide extensions.

[14] The Base Class Libraries classes (BCL) are also part of the CLR.

All this is made possible by putting old technologies together in the CLR creatively: intermediate languages, type-safe verification, and of course, metadata. As you will see, metadata is used in many features in .NET.

We shall expand on these topics in the course of the book. We next cover the C# language. Depending on your knowledge of C#, you might be able to skim Chapters 3, 4, and 5. Chapter 4 introduces the Acme Travel Agency case study, which is used throughout the book. Chapter 5 covers important topics about the interaction of C# and the .NET Framework.

for RuBoard
for RuBoard

Chapter 3. C# Overview for Sophisticated Programmers

In this chapter we quickly cover the essentials of the C# language, which should be quite easy for you to learn if you have experience with C++ or Java. A "hello, world" program introduces the basic structure of C# programs. We then cover variables , operators, control structures, formatting, methods , and input/output. Classes are fundamental in C#, and we examine them in some detail. Besides the standard features, C# adds some convenience features, such as properties. We cover the essentials of data types in C#, which correspond to types in the Common Type System. We discuss the fundamental distinction between value and reference types and see how to convert between them using boxing and unboxing operations.

C# has a string type, and the StringBuilder class can be used for dynamically changing strings. We examine arrays in C# and some operations provided by the System.Array class. We then cover some additional topics concerning methods, including parameter passing, variable length parameter lists, method overloading, and operator overloading. We discuss exception handling in C# in some detail, including the use of user -defined exception classes and structured exception handling.

We conclude the chapter by looking at how you can have "unsafe" sections of C# code, which can be used to work with pointers for efficiency or for interoperating with legacy code.

for RuBoard
for RuBoard

Hello World in C#

Whenever learning a new programming language, a good first step is to write and run a simple program that will display a single line of text. Such a program demonstrates the basic structure of the language, including output.

Here is "Hello, World" in C#. (See the Hello directory for this chapter.)

// Hello.cs 

using System; 

class Hello 
{ 
    public static int Main(string[] args) 
    { 
        Console.WriteLine("Hello, World"); 
        return 0; 

    } 
}

Compiling and Running (Command Line)

You can learn how to use the Microsoft Visual Studio.NET IDE (integrated development environment) in Appendix A. You can also use the command-line tools of the .NET Framework SDK. Be sure to get the environment variables set up properly, as described in the sidebar. To compile this program at the command line, enter the command

>csc Hello.cs

An executable file Hello.exe will be generated. To execute your program, type at the command line:

>Hello

The program will now execute, and you should see displayed the greeting:

Hello, World

Setting Environment Variables

In order to run command-line tools such as the C# compiler using a simple name such as csc rather than a complete path , we must set certain environment variables. To do so we can use a batch file, corvars.bat , which can be found in the bin directory of the Framework SDK.

I experienced different behavior in different beta versions of the .NET Framework SDK. In one version the environment variables were set up automatically as part of the install, and in another version I had to use the corvars.bat file.

If you have Visual Studio.NET installed, you can ensure that the environment variables are set up by starting your command prompt session from Start Programs Microsoft Visual Studio.NET 7.0 Microsoft Visual Studio Tools Microsoft Visual Studio.NET Command Prompt.

Program Structure

// Hello.cs

class Hello


{

...

}

Every C# program has at least one class . A class is the foundation of C#'s support of object-oriented programming. A class encapsulates data (represented by variables ) and behavior (represented by methods ). All of the code defining the class (its variables and methods) will be contained between the curly braces. We will discuss classes in detail later.

Note the comment at the beginning of the program. A line beginning with a double slash is present only for documentation purposes and is ignored by the compiler. C# files have the extension . cs .


// Hello.cs

...

An alternate form of comment is to use an opening /* and a closing */.

/* This is a comment 
   that may be continued over 
   several lines */

There is a distinguished class, which has a method whose name must be Main . The method should be public and static . An int exit code can be returned to the operating system. Note that in C# the file name need not be the same as the name of the class containing the Main method.

// Hello.cs 

using System; 

class Hello 
{

public static int Main(string[] args)

{ 
        ...

return 0;

} 
}

Use void if you do not return an exit code.

public static void Main(string[] args)

Command-line arguments are passed as an array of strings. The runtime will call this Main method ”it is the entry point for the program. All the code of the Main method will be between the curly braces.

// Hello.cs 

using System; 

class Hello 
{ 
    public static int Main(string[] args) 
    {

Console.WriteLine("Hello, World");

return 0; 
    } 
}

Every method in C# has one or more statements . A statement is terminated by a semicolon. A statement may be spread out over several lines.

The Console class provides support for standard output and standard input. The method WriteLine displays a string, followed by a new line.

Namespaces

Much standard functionality in C# is provided through many classes in the .NET Framework. Related classes are grouped into namespaces . Many useful classes, such as Console , are in the System namespace. The fully qualified name of a class is specified by the namespace followed by a dot followed by a class name.

System.Console

A using statement allows a class to be referred to by its class name alone.

// Hello.cs

using System;

class Hello 
{ 
    public static int Main(string[] args) 
    {

Console

.WriteLine("Hello, World"); 
        return 0; 
    } 
}
for RuBoard