Source File Conventions (C# vs. Java)

The naming convention for files containing C# classes is a little different from Java. In Java, all source files have a .java extension. Each source file contains one top-level public class declaration, and the class name must match the file name. In other words, a class called Customer declared with a public scope must be defined in a source file with the name Customer.java.

C# source code is denoted by the .cs extension. Unlike Java, source files can contain more than one top-level public class declaration, and the file name does not need to match any of the classes' names.

Top-Level Declarations

In both Java and C#, source code begins with a few top-level declarations in a certain sequence. There are only a few differences between the declarations made in Java and C# programs.

Top-Level Declarations in Java

In Java, you can group classes together with the package keyword. A packaged class must use the package keyword in the first executable line of the source file. Any import statements required to access classes in other packages are next, and then the class declaration, as follows:

package Acme;
import java.io.*;
class Customer
{
    ...
}

Top-Level Declarations in C#

C# uses the concept of namespaces to group logically related classes through the namespace keyword. These act similarly to Java packages, and a class with the same name might appear within two different namespaces. To access classes defined in a namespace external to the current one, use the using directive followed by the namespace name, as follows:

using System.IO;

namespace Acme 
{
    class Customer
    {
        // ...
    }
}

Note that using directives can be placed inside a namespace declaration, in which case such imported namespaces form part of the containing namespace.

Java does not allow multiple packages in the same source file. However, C# does allow multiple namespaces in a single .cs file, as follows:

namespace AcmeAccounting
{
    public class GetDetails
    {
        // ...
    }
}

namespace AcmeFinance
{
    public class ShowDetails
    {
        // ...
    }
}

Fully Qualified Names and Namespace Aliases

As with Java, you can access classes in both the .NET Framework or in user-defined namespaces without a using reference for that namespace by providing the fully qualified name for the class, such as DataSet, or AcmeAccounting.GetDetails in the previous example.

Fully qualified names can get long and unwieldy, and in such cases, you can use the using keyword to specify a short name, or alias, to make your code more readable.

In the following code, an alias is created to refer to code written by a fictional company:

using DataTier = Acme.SQLCode.Client;

class OutputSales
{
    static void Main()
    {
        int sales = DataTier.GetSales("January");
        System.Console.WriteLine("January's Sales: {0}", sales);
    }
}

Note that in the syntax for WriteLine, with {x} in the format string, the x denotes the position in the argument list of the value to insert at that position. Assuming the GetSales method returned 500, the output of the application would be as follows:

January's Sales: 500

Preprocessing Directives

Similar to C and C++, C# includes preprocessing directives that provide the ability to conditionally skip sections of source files, report error and warning conditions, and to delineate distinct regions of source code. The term "pre-processing directives" is used only for consistency with the C and C++ programming languages, as C# does not include a separate preprocessing step. For more information, see C# Preprocessor Directives.

See Also

Concepts

C# Programming Guide

Other Resources

The C# Programming Language for Java Developers