Program Structure
C#
Program Structure
Overview
Organizing Types
Namespaces
References
Main Method
Syntax
Program Structure
Organizing Types
Physical organization Assembly
Types are defined in files Module
Files
are compiled into File
modules Type
Modules are grouped
into assemblies
Program Structure
Organizing Types
Types are defined in files
A file can contain multiple types
Each type is defined in a single file
Files are compiled into modules
Module is a DLL or EXE
A module can contain multiple files
Modules are grouped into assemblies
Assembly can contain multiple modules
Assemblies and modules are often 1:1
C# Program Structure
Namespaces
Contain types and other namespaces
Type declarations
Classes, structs, interfaces, enums,
and delegates
Members
Constants, fields, methods, properties, indexers, events, operators,
constructors, destructors
Organization
No header files, code written “in-line”
No declaration order dependence
using System;
C# Program namespace System.Collections
{
Structure public class Stack
{
Entry top;
public void Push(object data) {
top = new Entry(top, data);
}
public object Pop() {
if (top == null) throw new InvalidOperationException();
object result = top.data;
top = top.next;
return result;
}
}
}
Program Structure
Organizing Types
Types are defined in ONE place
“One-stop programming”
No header and source files to synchronize
Code is written “in-line”
Declaration and definition are one and
the same
A type must be fully defined in one file
Can’t put individual methods in different files
No declaration order dependence
No forward references required
Type System
Value types
Directly contain data
Cannot be null
Reference types
Contain references to objects
May be null
int i = 123;
string s = "Hello world";
i 123
s "Hello world"
Type System
Value types
Primitives int i;
Enums enum State { Off, On }
Structs struct Point { int x, y; }
Reference types
Classes class Foo: Bar, IFoo {...}
Interfaces interface IFoo: IBar {...}
Arrays string[] a = new string[10];
Delegates delegate void Empty();
Program Structure
Namespaces
Namespaces provide a way to
uniquely identify a type
Provides logical organization of types
Namespaces can span assemblies
Can nest namespaces
There is no relationship between namespaces and file structure
(unlike Java)
The fully qualified name of a type includes all namespaces
Program Structure
Namespaces
namespace N1 { // N1
class C1 { // N1.C1
class C2 { // N1.C1.C2
}
}
namespace N2 { // N1.N2
class C2 { // N1.N2.C2
}
}
}
Program Structure
Namespaces
The using statement lets you use types without typing
the fully qualified name
Can always use a fully qualified name
using N1;
C1 a; // The N1. is implicit
N1.C1 b; // Fully qualified name
C2 c; // Error! C2 is undefined
N1.N2.C2 d; // One of the C2 classes
C1.C2 e; // The other one
Program Structure
Namespaces
The using statement also lets you create aliases
using C1 = N1.N2.C1;
using N2 = N1.N2;
C1 a; // Refers to N1.N2.C1
N2.C1 b; // Refers to N1.N2.C1
Program Structure
Namespaces
Best practice: Put all of your types in a
unique namespace
Have a namespace for your company,
project, product, etc.
Look at how the .NET Framework classes
are organized
Program Structure
References
In Visual Studio you specify references
for a project
Each reference identifies a specific assembly
Passed as reference (/r or /reference)
to the C# compiler
csc HelloWorld.cs
/reference:System.WinForms.dll
Program Structure
Namespaces vs. References
Namespaces provide language-level naming
shortcuts
Don’t have to type a long fully qualified name
over and over
References specify which assembly to use
Program Structure
Main Method
Execution begins at the static Main() method
Can have only one method with one of
the following signatures in an assembly
static void Main()
static int Main()
static void Main(string[] args)
static int Main(string[] args)
Program Structure
Syntax
Identifiers
Names for types, methods, fields, etc.
Must be whole word – no white space
Unicode characters
Begins with letter or underscore
Case sensitive
Must not clash with keyword
Unless prefixed with @
Thank You!