|
1
|
- Axel Goldbach
- modulo3 gmbh
|
|
2
|
- 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
|
- 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
|
- 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
|
|
|
6
|
|
|
7
|
- 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
|
- 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
|
- 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
|
- 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
|
- Specifies how source languages (C#, C++, VisualBasic, JScript) must be
compile to the CL
- Defines
- Datatypes
- Interfaces, Classes
- Indentifiers
- Exceptions
- Access modifiers
- etc.
|
|
12
|
- .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
|
- class HelloWorld
- {
- static void Main ()
- {
- System.Console.WriteLine ( "Hello
World!" );
- }
- }
|
|
14
|
- DOS console
- Compiling
- csc HelloWorld.cs [Return]
- generates HelloWorld.exe
- Execution
|
|
15
|
- Write the „Hello World“ program
- Compile and start it
|
|
16
|
|
|
17
|
- class HelloWorld
- {
- static void Main ()
- {
- System.Console.WriteLine ( "Hello
World!" );
- }
- }
|
|
18
|
- class MainApp
- {
- static void Main ()
- {
- System.Console.WriteLine ( "Hello
World!" );
- }
- }
|
|
19
|
- class MainApp
- {
- static void Main ()
- {
- System.Console.WriteLine ( "Hello
World!" );
- }
- }
|
|
20
|
- 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
|
- namespace Samples
- {
- // declarations of Namespaces
Samples members
- }
|
|
22
|
- namespace Samples
- {
- // declarations of Namespaces
Samples members
- namespace HelloWorld
- {
- // declarations of Namespaces
Samples.HelloWorld
- // members
- }
- }
|
|
23
|
- namespace Samples.HelloWorld
- {
- // declarations of Namespaces
Samples.HelloWorld
- // members
- }
|
|
24
|
- 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
|
- A C# source file can contain
- zero, one, or more than one Namespaces
- one or more classes
- if a DLL will be generated:
- 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 a single line
- /* ... */ - comments a block of lines
- /// - comment symbol for generating a documentation
|
|
27
|
- 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
|
- ///<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
|
- <?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
|
- Document „Hello World“
- Generare a documentation
- Have a look at the .NET reference for additional documentation tags
|
|
31
|
|
|
32
|
- 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
|
|
|
34
|
- ...
- int i = 1;
- double d = 2;
- i = d; // Compiler-Fehler
- i = (int)d;
- checked
- {
- // bei Überlauf
System.OverflowException
- i = (int)d;
- }
- ...
|
|
35
|
- „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
|
|
|
37
|
- 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
|
- ...
- 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
|
- 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
|
- Methods are functions of classes
- ...
- class Test
- {
- static void Main ()
- ...
- static bool test ( int a, int
b )
- {
- // do something with a and b
- }
- }
- ...
|
|
41
|
- 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
|
- Write a function that checks an int value to be bigger than another
- Write a function that swaps to int values
|
|
43
|
|
|
44
|
- 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
|
- 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
|
- 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
|
- 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
|
- Try the different loop types
|
|
49
|
|