Chapter 4. Language Syntax and Features

This chapter details the basic syntax, structure, and features of the C# language. We expect the reader to be familiar with the Java language, and we concentrate on C# features that are new or different.

General Program Structure

We begin with a simple program example to demonstrate the basic points that must be understood when writing C# code. The following Java code should be familiar:

public class Hello {
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}

Contrast it with the following C# code:

using System;

public class Hello {
    public static void Main(String[] args) {
        Console.WriteLine("Hello World!");
    }
}

Although obvious similarities exist, the following points should be noted:

  • The java.lang package is implicitly imported to all Java programs. C# does not implicitly import any classes. The using System; statement makes the classes required for this program available. Note that System in the C# code refers to a namespace from the .NET class libraries, not a specific class as it does in Java. Importing classes into C# programs is covered in the Using Namespaces section later in this chapter.

  • The classes and methods used to write to the console are different. The .NET class libraries are covered in Part III of this book; console output is covered in Chapter 10.

  • The application entry point—or main method—uses different capitalization; it’s Main in C#. See the next section for more information about the Main method.

The Main Method

The capitalization of the method name is not the only difference between the two Main methods. The Java main method has the following signature:

public static void main(String[] args)

The Main method in C# can return either an int or a void and can take either an array of string arguments or nothing as its parameters, resulting in the following four possible signatures:

static void Main()
static void Main(String[] args)
static int  Main()
static int  Main(String[] args)

Points to note:

  • The public access modifier is not required for the C# Main method. The application entry point is always accessible to the runtime regardless of its declared accessibility.

  • The int returned by the third and fourth signatures serves as the termination status code that is returned to the execution environment on termination of the program. This is equivalent to the use of Runtime.getRuntime.exit(int) or System.exit(int) in Java. The Main method signatures that return void will always return 0 as the termination status code.

Comments

C# supports both the single-line (//) and multiline (/*...*/) comment syntaxes.

Documentation Comments

C# implements a code documentation tool similar to the javadoc utility. Like javadoc, this works only if the developer places markup in appropriately formatted code comments. Unlike javadoc, this markup is extracted at compile time and is output to a separate file as XML. Being XML, this information can be easily processed to produce documentation in different formats—not just HTML, but any format that is supported by a valid XSL transformation. The most common approach, however, is to utilize an XSL transformation to generate HTML pages.

Code documentation comments are preceded by three forward slashes, as shown here:

/// A single line documentation comment

The C# specification also recommends use of the familiar /** token to identify multiline documentation comments. However, version 7.00 of the C# compiler does not support this syntax.

More Info

To produce code documentation output, use the /doc flag when compiling. Full coverage of compiler options is contained in Chapter 3.

More Info

Refer to the .NET documentation for complete coverage of the tags supported by the documentation comment tool.

Case Sensitivity

Like Java, C# is a case-sensitive language. However, case sensitivity shouldn’t be used to differentiate program elements. The multilanguage support in the .NET runtime means that components developed in C# can be called from components written in other .NET languages that might not be able to differentiate on the basis of case.

Naming Conventions

The C# specification advises a combination of Pascal and camel case conventions for the names of various programming identifiers. Pascal casing capitalizes the first character of each word—for example, MyMethod or SomeIntegerVariable. Camel case convention capitalizes the first character of each word except the first one—for example, myMethod or someIntegerVariable.

The .NET class libraries implement these guidelines. This guideline has the commendable goal of making all C# code and class libraries more readable, consistent, and predictable. In many cases, developers will continue using conventions that they are comfortable with, or that their organization dictates, but familiarity with these conventions will help to navigate and understand the .NET class libraries. Refer to the C# Language Specification for full details.

Warning

One result of these naming conventions is the difference in capitalization of method names between the Java and .NET class libraries. .NET uses Pascal convention for method names:

SomeObject.ToString();

Java uses camel convention:

SomeObject.toString();

This subtle difference is certain to cause frustration for Java developers when they first begin the transition to C# development.

Source Code Character Sets and Unicode

C#, .NET, and Java all support the use of Unicode internally and provide extensive Unicode support to the developer through their class libraries. However, there is some difference in the support for Unicode in source files. By default, all Java tools, including the Java compiler, can process only LATIN-1 and Unicode encoded characters in a source file. The Unicode characters in the source file must be encoded using Unicode escape sequences. For other character encodings, the –encoding compiler flag must be used, which forces the Java compiler to translate source files in different encoding formats into the default format before compiling the source code.

The C# compiler, on the other hand, will accept characters in the default code page (LATIN-1) or Unicode (UTF-8-encoded) files by default, conveniently allowing the programmer to use and view Unicode characters in source files. C# also supports the use of Unicode escape sequences.

Identifiers

The naming of C# program elements follows rules that are broadly similar to Java:

  • Both languages support Unicode characters in identifier names.

  • C# identifiers must begin with a letter or underscore, but the $ symbol permitted by Java is not valid in C#.

  • The remainder of the identifier is made up of letters and numbers.

  • C# allows the use of keywords as identifiers by preceding the identifier with the @ symbol. Refer to the Keywords as Identifiers section coming up for full details.

Source File Names and Contents

Java requires a single public class definition per source file but allows any number of nonpublic classes. The file name must match the public class name with the .java extension. C# permits any number of public and nonpublic classes to be contained in a single source file. The source file can have any valid operating system name independent of the classes it contains.

If more than one class contains a Main method, the application entry point must be specified at compile time by using the /main flag. Compiler options are discussed in Chapter 3.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset