Notizen
Bildschirmpräsentation
Gliederung
1
The C# programming language
An Introduction
  • Axel Goldbach
  • modulo3 gmbh
2
Preface from the Editor
  • When setting up this Seminar we did by purpose focus on the Language C# itself.
  • Which means, the Seminar may be used without having VisualStudio.NET but using a “common” Text editor (Think of as “VisualNotapad“ ;-)
  • The .NET-Framework too ist only used where it is necessary as part of the Language C# itself. This means this Tutorial focuses on teaching the Language C#
  • Depending on ressonance and reception of this tutorial we will setup further tutorials with focuses like
  • - The .NET Framework
  • - VisualStudio.NET and
  • -Client-Server-Applications using C# and VisualStudio.Net.


  • Now we wish you to enjoy this newest baby of the modulo3 Seminar-Program.
  • As ever this Seminar is also available as Hand-On-Seminar with a Training teaching a Group of a maximum of 6 students the same knowledge as in this tutorial within 3 days.
  • Now the modulo3 GmbH wishes you a lot of success with the knowledge provided in this tutorial.
  • A proper installation of the .NET-Framework on you machine is needed to compile and run the examples.
3
Goal
  • Mediated knowledge
    • Basics about C#
    • Classes, Inner Classes
    • Interfaces, abstract classes, factory classes
    • In-/Output (Console, File)
    • Often used classes: Enumerators, Collections
    • Error handling with Exceptions
    • Delegates, Events
  • A commandline application
4
.net-Framework
  • Former NGWS (Next Generation Windows Services)
  • Runtime Environment (CLR, Common Language Runtime) for several programming languages
    • C#
    • C++
    • VisualBasic
    • JScript
  • Languages must support the CLS (Common Language Specification)
5
Architecture
6
Execution
7
Compiled Files
  • Portable Executable (PE)
    • Executable Code
    • .exe or .dll
  • Assembly
    • groups(assembles) one or more PEs and ressources into one logical unit
    • .exe or .dll
8
Assembly
  • Describes a collection of files for the CLR
    • Interfaces and classes
    • Ressources like Bitmaps, JPEG files, ressource files, etc.
  • Reusable, versionable, self-describing
  • Code can only be executed by the CLR if it is contained within assemblies
9
Metadaten
  • Assembly contains metadata
    • Name, Version, Culture (country-specific settings: language, date and number formats, etc.), Public Key
    • Exported types
    • Assemblies that this PE or Assembly is derived from
    • Security information („what this PE or Assembly is allowed to do“)
10
Metadata
  • Description of the contained Types
    • Name, visibility, base class, implemented Interfaces
    • Members (Methods, Fieds, Properties, Events, etc.)
  • Attributes
    • Additionaly holds the assembly describing elements
11
Common Language Specification
  • Specifies how source languages (C#, C++, VisualBasic, JScript) must be compile to the CL
  • Defines
    • Datatypes
    • Interfaces, Classes
    • Indentifiers
    • Exceptions
    • Access modifiers
    • etc.
12
Minimum requirements for C#
  • .NET-Framework
  • Path to .NET (Beta 1)
    • %windir%\Microsoft.NET\Framework\v1.0.????
    • ???? is the final build number
    • must be set in the „Path“ env.-variable
    • contains
      • mscorlib.dll (.NET class library)
      • csc.exe (C# console compiler)
  • Texteditor like Notepad


13
Hello World



  • class HelloWorld
  • {
  •   static void Main ()
  •   {
  •     System.Console.WriteLine ( "Hello World!"  );
  •   }
  • }
14
Hello World
  • DOS console
    • Compiling
      • csc HelloWorld.cs [Return]
      • generates HelloWorld.exe
    • Execution
      • HelloWorld [Return]

15
Your job
  • Write the „Hello World“ program
  • Compile and start it
16
Solution
  • samples\01_HelloWorld
17
Hello World



  • class HelloWorld
  • {
  •   static void Main ()
  •   {
  •     System.Console.WriteLine ( "Hello World!"  );
  •   }
  • }
18
Hello World



  • class MainApp
  • {
  •   static void Main ()
  •   {
  •     System.Console.WriteLine ( "Hello World!"  );
  •   }
  • }
19
Hello World



  • class MainApp
  • {
  •   static void Main ()
  •   {
  •     System.Console.WriteLine ( "Hello World!"  );
  •   }
  • }
20
Namespaces
  • Namespaces are used to group classes logically
  • can be nested, e.g. System.IO
  • Two classes defined in different Namespaces may have the same name
    • allows more flexibility by the naming of classes with same functionality in different contexts
21
Definition of own Namespaces



  • namespace Samples
  • {


  •   // declarations of Namespaces Samples members


  • }
22
Nesting of Namespaces
  • namespace Samples
  • {


  •   // declarations of Namespaces Samples members


  •   namespace HelloWorld
  •   {


  •    // declarations of Namespaces Samples.HelloWorld
  •    // members


  •   }
  • }
23
Nesting of Namespaces



  • namespace Samples.HelloWorld
  • {


  •   // declarations of Namespaces Samples.HelloWorld
  •   // members


  • }
24
Namespaces
  • If no Namespace is declared the class is contained in the global Namespace
  • using relieves the access to classes of Namespaces



  • is equivalent to
25
C# source file
  • A C# source file can contain
    • zero, one, or more than one Namespaces
    • one or more classes
    • if a DLL will be generated:
      • no Main method
    • if a EXE will be generated:
      • exactly one Main method of
      • if more than one Main method exist the compiler must know which Main method is the main entry point
26
Comments
  • // - comments a single line
  • /* ... */ - comments a block of lines
  • /// - comment symbol for generating a documentation
27
Generating a documentation
  • Format is XML
  • csc.exe generates Documentation too
    • csc /doc:HelloWorld.xml HelloWorld.cs [Return]
  • Predifined tags for documentation (excerpt)
    • <summary>
    • <param>
    • <returns>
    • <code>
    • etc.
28
Generating a documentation

  • ///<summary>Hello world example.</summary>
  • class HelloWorld
  • {
  •   ///<summary>Main method.</summary>
  •   ///<param name="Args">Command line arguments.</param>
  •   static void Main ( string[] Args )
  •   {
  •     Console.WriteLine ( "Hello World!" );
  •   }
  • }
29
Generating a documentation
  • <?xml version="1.0"?>
  • <doc>
  •   <assembly>
  •     <name>HelloWorld</name>
  •   </assembly>
  •   <members>
  •     <member name="T:MainApp">
  •       <summary>Hello world example.</summary>
  •     </member>
  •     <member name="M:MainApp.Main(System.String[])">
  •       <summary>Main method.</summary>
  •         <param name="Args">Command line arguments.
  •         </param>
  •     </member>
  •   </members>
  • </doc>
30
Your job
  • Document „Hello World“
  • Generare a documentation
  • Have a look at the .NET reference for additional documentation tags
31
Solution
  • samples\02_Documentation
32
Datatypes
  • All datatypes are represented by classes (no  base datatypes like int)
  • int in C# is a alias for System.Int32
  • void means „no datatype“, but for void too a Class named System.Void exists
33
Datatypes (excerpt)
34
Casting
  • ...
  •   int i = 1;
  •   double d = 2;


  •   i = d;  // Compiler-Fehler


  •   i = (int)d;


  •   checked
  •   {
  •     // bei Überlauf System.OverflowException
  •     i = (int)d;
  •   }
  • ...
35
Your job
  • „Cast a little bit“
  • Try to cast datentypes, that produce overflows and have a look what happens (see System.Int32.MaxValue or System.Int32.MaxValue)
  • Try the checked keyword
36
Solution
  • samples\03_Casting
37
Output
  • Strings can be concatenated dynamically for output (e.g. with Console.WriteLine)
    • +: direct string concatenation, e.g. Console.WriteLine( “Hello “ + Name );
    • {<Index>}: index of a string, e.g. Console.WriteLine( “Hello {0}“, Name );
  • If “{<Index>}“ is used, output formats may be added (see .NET reference)
38
Output
  • ...
  •     string s1 = "Axel";
  •     string s2 = "how do you do";
  •     Console.WriteLine ( "Hello {0}, {1}?",
  •       s1, s2 );
  •     Console.WriteLine ( "Hello " + s1 + ", " + s2 +
  •         "?" );
  •     int i = 1;
  •     int j = 2;
  •     Console.WriteLine ( i + "+" + j + "=" + (i + j) );
  • ...
39
Strings
  • Escape sequences
    • A backslash followed by a control character, e.g. \n (new line)
  • To have a backslash within a string write two backslashes hintereinander
    • string a = “d:\\humpf\\foo.txt“;
  • Suppression of Escape sequences
    • string a = @“d:\humpf\foo.txt“;
40
Passing parameters
  • Methods are functions of classes
  • ...
  •   class Test
  •   {
  •     static void Main ()
  •     ...


  •     static bool test ( int a, int b )
  •     {
  •       // do something with a and b
  •     }
  •   }
  • ...
41
Passing parameters
  • The type of passing parameters is determined by reserved words before the declaration of the parameters datatype
  • Three types of passing parameters to methods
    • none: call by value
      • „Copies“ of the variables are passed
      • Changes have no effect at the outside of the called methods
    • ref: call by reference
      • References to the variables are passed
      • Changes have effect at the outside of the called methods
    • out: see .NET reference
42
Your job
  • Write a function that checks an int value to be bigger than another
  • Write a function that swaps to int values
43
Solution
  • samples\04_Parameters
44
Loops
  • for loop
    • for ( <Initialzation>; <Condition>; <Modificator> ) <Commands>

  • class Loop
  • {
  •   static void Main ( string[] Args )
  •   {
  •     for ( int i = 0; i < Args.Length; i++ )
  •     {
  •       System.Console.WriteLine( "Args[{0}]={1}", i, Args[i] );
  •     }
  •   }
  • }
45
Loops
  • while loop
    • while ( <Condition> ) <Commands>

  • class Loop
  • {
  •   static void Main ( string[] Args )
  •   {
  •     int i = 0;
  •     while ( i < Args.Length )
  •     {
  •       System.Console.WriteLine( "Args[{0}]={1}", i,
  •           Args[i++] );
  •     }
  •   }
  • }
46
Loops
  • do-while loop
    • do <Commands> while ( <Condition> )

  • class Loop
  • {
  •   static void Main ( string[] Args )
  •   {
  •     int i = 0;
  •     do
  •     {
  •       System.Console.WriteLine ( "Args[{0}]={1}", i,
  •           Args[i++] );
  •     }
  •     while ( i < Args.Length );
  •   }
  • }
47
Loops
  • foreach loop
    • foreach ( <Datatype> <Variable> in <List> ) <Commands>

  • class Loop
  • {
  •   static void Main ( string[] Args )
  •   {
  •     int i = 0;
  •     foreach ( string Arg in Args )
  •     {
  •       System.Console.WriteLine ( "Args[{0}]={1}", i++, Arg );
  •     }
  •   }
  • }
48
Your job
  • Try the different loop types
49
Solution
  • samples\05_Loops