C# Program Structure 1 Lecture
C# Program Structure 1 Lecture
C#
Program Structure
Overview
Organizing Types
Namespaces
References
Main Method
Syntax
Program Structure
Organizing Types
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
C2 c; // Error! C2 is undefined
N1.N2.C2 d; // One of the C2 classes
C1.C2 e; // The other one
Program Structure
Namespaces
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
csc HelloWorld.cs
/reference:System.WinForms.dll
Program Structure
Namespaces vs. References