Starting Out


Take a look at the infamous "Hello World!" example in Java:

 public class Hello { public static void main(String args []) { System.out.println("Hello world! This is Java Code!");  } } 

The corresponding C# code for this is as follows:

 using System; public class Hello { public static void Main(string [] args) { System.Console.WriteLine("Hello world! This is C# code!"); } } 

The first thing that you'll notice is that the two appear to be very similar syntactically and both languages are case-sensitive. C# is object-oriented like Java, and all functionality must be placed inside a class (declared by the keyword class). These classes can contain methods, constructors, and fields just as Java classes can, and a C# class can inherit methods and fields from another class or interface as in Java. The implementation of classes and methods is similar in both languages.

C# code blocks are enclosed by braces just as in Java. The entry point to a C# application is the static Main() method, as required by the compiler (similar to Java but note the uppercase "M"). Also note that only one class in the application can have a Main() method. Similar to Java, the static keyword allows for the method to be called without creating an instance of the class first. For the Main() method in C# you have the choice of either a void or int return type. void specifies that the method does not return a value and int specifies that it returns an integer type.

The using keyword in C# corresponds to the import keyword in Java. Therefore, in the preceding C# code above, you are essentially importing the C# equivalent of a class package called System. In C#, a class package is called a namespace, and you will look more closely at these in the next section.

Note that although it is written it with a lowercase s here, in C# the string type can also be written with a capital S as String. You will also notice that the array rank specifier ([]) has been shifted from in front of the args variable in the Java example, to between the string type and args variable in the C# sample. In fact, this specifier can occur before or after the variable in Java. However, in C#, the array rank specifier must appear before the variable name because an array is actually a type of its own indicated by type []. Arrays are discussed in more depth a bit later.

Finally, as you might expect, the names of methods tend to differ between the languages. For example, in Java you would use System.out.println() to display text in the command console. The equivalent to this method in C# is System.Console.WriteLine().

Compiling and Running C# Code

Chapter 2, "C# Basics," noted that like Java code, C# source code is compiled in two stages: first to Intermediate Language (IL), and then to native code. To run the previous C# code, you need to save it with an appropriate file name (for example, HelloWorld) and file extension .cs and then compile it to IL using the csc command:

 csc HelloWorld.cs 

The next step is to compile the IL to native code and run the example. To do this, just type the name of the file, without the extension (as you would with Java code):

 HelloWorld Hello world! This is C# code!

Namespaces

Whereas Java classes reside in logical divisions referred to as packages, C# (and other managed) classes are grouped together into namespaces. Packages and namespaces differ significantly in their implementation. A Java class that you want to make part of the com.samples package, for example, must have package com.samples; as the first line of code in the file. This is, of course, excluding any comments.

Any code within that file automatically becomes a part of the specified package. Also, a Java package name is associated with the folder containing the class file in that they must have the same name. The com.samples package must therefore be in class files that exist in the com/samples folder. Take a look at some examples of how packages work in Java:

 package java2csharp.javasamples; public class Hello { public static void main(String args []) { System.out.println("Hello world! This is Java Code!"); } } 

The following list provides examples of how the previous code could be referenced or executed. This list assumes that the class file has been made available to the JRE:

  • From the command line:

     java java2csharp.javasamples.Hello 
  • As a direct reference in the code:

     public class Referencer {  java2csharp.javasamples.Hello myHello = new java2csharp.samples.Hello(); 

  • By utilizing the import directive, you could omit fully qualified package names, so Referencer could also be written as:

     import java2csharp.javasamples.*; public class Referencer { Hello myHello = new Hello(); } 

Wrapping a class in a namespace is achieved in C# by using the namespace keyword with an identifier, and enveloping the target class in brackets. Here is an example:

 namespace java2csharp.csharpsamples { using System; public class Hello { public static void Main(string [] args) { System.Console.WriteLine("Hello world! This is C# code!"); } } } 

As you can see, you delimit layers of namespaces using the . operator, as in Java. Note that C# does not require an asterisk (*) needed in C# — applying the using directive implicitly imports all elements of the specified namespace. You will also have noticed the major difference from Java here: the use of name- space parentheses in which you place classes associated with the namespace. The advantage of using the parentheses like this is that you then disassociate package names from directory structures: feasibly you could place a file containing this namespace anywhere within the directory as long as the CLR recognizes it. Therefore, it also enables you to call the file containing these classes anything you wish (it doesn't have to be the same name as the class as in Java); you can have more than one public class defined per file; and you can split the classes defined in this namespace into different files in different parts of the directory structure, as long as the namespace declaration appears in each of the files.

You can also introduce multiple namespaces in the same file with no restriction. You could, for example, add the definition of a new class and place it in a new namespace in the same file and still not be outside the bounds of the language:

 namespace java2csharp.csharpsamples { using System; public class Hello { public static void Main(string [] args) { System.Console.WriteLine("Hello world! This is C# code!"); } } } namespace java2csharp.morecsharpsamples { using System; public class AnotherHello { public static void OtherMain(string [] args) { System.Console.WriteLine("Hello again! This is more C# code!"); } } } 

As pointed out in the previous section, classes from a particular namespace can be imported into another namespace with the using keyword. You can see that you import classes from the System namespace(the top level .NET Base Class namespace) into both namespaces in the preceding code. You can also import classes from other namespaces directly into your classes by referring to the imported class using its full name (namespace included), in a similar way to using direct referencing of classes in Java code.

Namespaces may also be defined within other namespaces. This type of flexibility is impossible in Java without having to create a subdirectory. You could change the previous example so that the AnotherHello class is in the java2csharp.csharpsamples.hellosamples namespace:

 namespace java2csharp.csharpsamples { namespace hellosamples { using System; public class AnotherHello { public static void Main(string [] args) { System.Console.WriteLine("Hello again! This is more C# code!"); } } } } 

Java classes are part of a package; all classes created are part of the default package. C# mimics this functionality. Even if you do not declare one, a default namespace is created for you. It is present in every file and available for use in named namespaces. Just as in Java you cannot change package information, in C# namespaces cannot be modified either. Packages can span multiple files in the same folder; name- spaces can span multiple files in any number of folders, and even multiple assemblies (the name given to code libraries in .NET).

Note that the default accessibility for types inside a namespace is internal. You must specify types as public if you want them available without full qualification; however, you are strongly advised against this practice. No other access modifiers are allowed. In Java, internal package types may also be marked as final or abstract or not marked at all (this default access makes them available only to consumers inside the package). Access modifiers are discussed later in this appendix.

One final feature of namespaces not available to Java packages is that they may be given a using alias. using aliases make it very easy to qualify an identifier to a namespace or class. The syntax is simple. Suppose you had a namespace Very.Very.Long.NameSpace.Name. You could define and use a using alias (here VVLNN) for the namespace as follows:

 using VVLNN = Very.Very.Long.Namespace.Name; 




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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