Visual C .NET

Visual C# .NET

C# is an important aspect of the .NET Framework; it was announced in July 2000, and is built on the shoulders of not only C++, but also Java. Java programmers will find a lot that's familiar in C. Perhaps the most important aspect of Java that came over to C# is the emphasis on security, particularly on the use of pointers. Although C# still supports pointers, their use is highly restricted, making the old hacker's trick of using pointers to rewrite code impossible to implement. We'll see more on pointers and their place in C# (you have to designate code with pointers in it as "unsafe") in Chapter 13.

C# itself was developed by a small team, led by Anders Hejlsberg (who was a major player in the development of both Turbo Pascal and Borland Delphi) and Scott Wiltamuth. There are many differences between C++ and C#. For example, C# does not use header files as C++ does; it supports an XML style of documentation comments marked with /// , and supports declarative constructs called attributes , which add metadata indicating how to handle methods . C# de-emphasizes pointers (sometimes by inventing new safe types, such as delegates , which act much like function pointers to let you set up callbacks), implements structs as a lightweight type very different from classes (structs and classes are very close in C++), supports reflection for handling metadata, and more. These are only a few examples.

FOR C++ PROGRAMMERS

Note the following discussion on C#'s differences from C++.


There are also many small differences that we'll encounter throughout the bookfor example, the C# entry point is Main() , not main() , and, unlike ANSI C++, it can return either void or int . Conditional statements such as if are restricted to Boolean operands, which, as we'll see, avoids the confusion between a statement like if(temperature = 212).. and if(temperature == 212)... .

C# is an amazingly simple language, and consists of 77 keywords (only a few more than ANSI C++), listed in Table 1.1. All these keywords are reserved words in C#, and cannot be used as identifiers in your code unless you implement them with an ampersand, @ (such as @class ). You'll become very familiar with these keywords in the following chapters.

Table 1.1. The C# Keywords

KEYWORDS

abstract

event

new

struct

as

explicit

null

switch

base

extern

object

this

bool

false

operator

throw

break

finally

out

true

byte

fixed

override

try

case

float

params

typeof

catch

for

private

uint

char

foreach

protected

ulong

checked

goto

public

unchecked

class

if

readonly

unsafe

const

implicit

ref

ushort

continue

in

return

using

decimal

int

sbyte

virtual

default

interface

sealed

volatile

delegate

internal

short

void

do

is

sizeof

while

double

lock

stackalloc

 

else

long

static

 

enum

namespace

string

 

Now that you've been introduced to .NET and C#'s place in it, you can start dissecting the first example. We'll do that next, taking it apart line by line. Understanding this first simple program will give us the framework we need to run example code over the next 10 chapters.

Dissecting the First Program

Take a look at the first example's code:

 
 class ch01_01 {   static void Main()   {     System.Console.WriteLine("Hello from C#.");   } } 

If you're a C++ programmer, you're probably familiar with what's going on here (with the notable exception that Main is capitalized in C#). Everything begins, as it usually does in C#, with a class statement. A class named ch01_01 is created, whose body will appear inside the curly braces:

 
 class ch01_01 {     .     .     . } 

C# code is case-sensitive, so class ch01_01 is not the same as class CH01_01 . As programmers familiar with object-oriented programming (OOP) know, classes create new types , and creating new types is the very heart of OOP. All code must be enclosed in a class (or a similar construct like a struct ) in C#. You put code into a class's methods ; the term method simply refers to a procedure that is a member of a class. In this example, the single method is called Main :

 
 class ch01_01 {  static void Main()   {   .   .   .   }  } 

C# will call the method named Main in your code automatically as soon as the program starts, thus transferring execution to that method. In other words, Main is your program's entry point , andyou put the code you want to run first in this method. Declaring Main as static void Main() means we do not need to create an object of this new class in order to call this method (which allows C# to call the method when the program starts). This declaration also specifies void as this method's return type, which means this method does not return any value to the code that calls it. The empty parentheses after its name indicate that you do not pass any data to it.

The code in the Main method is enclosed in the curly braces, which form the body of that method. In this case, that's the single line of code, System.Console.WriteLine("Hello from C#."); :

 
 class ch01_01 {   static void Main()   {  System.Console.WriteLine("Hello from C#.");  } } 

FOR C++ PROGRAMMERS

Unlike C++, Main is not only capitalized in C#, but it can return void as well as int .


This line of code is a C# statement, and all statements must end with a semicolon, as in C++ and Java. This statement calls the WriteLine method of the Console class, passing that method the text "Hello from C#." . When you execute this text, "Hello from C#." will appear in the DOS window.

The Console class that supports the WriteLine method is one of the classes built into the Framework Class Library, the FCL. Instead of writing all the code to display text in a DOS window yourself, you can simply call the Console.WriteLine method. Note the dot operator ( . ) here Console.WriteLine indicates that we're calling the WriteLine method of the Console class in this case. Console.WriteLine is referred to as System.Console.WriteLine because the Console class is part of the FCL.

When you develop your own classes, it's possible to create one with the same name as one in the FCL, because there are tens of thousands of classes in the FCL. To avoid those kinds of clashes , C# uses namespaces , which, as the name implies, define a separate space for named elements. System is the namespace used for most FCL classes, which means that you refer to Console.WriteLine as System.Console.WriteLine .

There's another way to handle the namespace issue. You can add a using statement such as using System; to the beginning of your code, and when C# can't find a method you're calling in your code, it will search the System namespace as wellnote that after you indicate you're using the System namespace, you can refer to System.Console.WriteLine simply as Console.WriteLine :

 
  using System;  class FirstApp {   static void Main()   {  Console.WriteLine("Hello from C#.");  } } 

As we're developing examples in the next few chapters, input and output will be an issue, so it's worth taking a closer look at WriteLine . You've seen how you can use it to display quoted text. You can also use it to display numbers , like this:

 
 System.Console.WriteLine(32); 

This simply displays the number 32 and skips to the next line in the output. Sometimes, you don't want to skip to the next line in the output, as when you display a prompt where the user is supposed to enter some data on the same line and then press <Enter> . You can use the Console.Write method for that, which is the same as Console.WriteLine , except that it doesn't skip to the next line:

 
 System.Console.Write("Please enter a number: "); 

FOR C++ PROGRAMMERS

C# input/output relies on the runtime library of the .NET Framework, not on C++ objects like cin and cout .


Both Write and WriteLine let you embed values in a quoted string of text. To do so, you use {0} as a placeholder for the first item to be embedded, {1} for the next, and so on, and follow the quoted text with the items you want to embed. Here's an example (note that this example also shows you can break a single C# statement over more than one line, because C# will know the statement is done when it reaches the terminating semicolonjust don't break quoted text this way or you'll introduce extra spaces and a carriage return into the text):

 
 class FirstApp {   static void Main()   {  System.Console.WriteLine("The temperature is between {0} and {1}",   32, 212);  } } 

The output of this code is as follows :

 
 The temperature is between 32 and 212 

What about reading data in from the user using ReadLine and working with that data? To do that, we'll need some way of working with and storing data. Although this is basic stuff, it's worth taking a look at how C# addresses it.



Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net