Basic File Structure

At its most basic a C# file will look like

 

Don’t be alarmed, each element in the diagram will be covered in this course. Here is an example piece of code in a file called FileStructure.cs showing most of the elements above. A physical file is called a compilation unit.

NOTE to Java developers

A C# filename does not have to be equal to the first public class name in the file

Breakdown of some of the file elements

This is simply an overview, we will cover these in more detail in further sections

using (an optional construct)

The using statement pulls constructs from other compilation units. The rules around what is pulled in and what can be exported will be covered in more details in later sections.

namespace (an optional construct)

A declarative component for grouping programming constructs. Essentially allows for the creation of constructs with the same name so that they do not clash. Namespaces can be shared across files i.e. namespace is declarative only, and the declaration can be present in more than one file.

class (an optional construct - but without it, you can not assign behaviour to the compilation unit)

Declares a data structure that may contain

  • data members

    • constants

    • fields

  • function members

    • methods

    • properties

    • events

    • indexers

    • operators

    • instance constructors

    • destructors

    • static constructors

  • nested types

constants in a class

Representing constant values associated with the class. These value can NOT be changed once set.

fields in class

A variable associated with the class. Unless specified as readonly, these variables can be modified at any time. We will cover the details of this construct in a later section of the course.

Note on curly braces

The curly braces denote what is commonly termed a code block in programming. In C# it is simply a block.

The curly braces are always balanced.

Balanced braces

{ { } }