Post on 25-Oct-2019
2009-04-02
Datavetenskap, Karlstads universitet 1
Karlstads universitet
Datavetenskap
DVGB07 – VT09
C#.Net
VT 2009
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Course Contents
• C#
– 6 hp approx.
• BizTalk
– 1,5 hp approx.
• No exam, but laborations
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Course contents
• Architecture
• Visual Studio
• Syntax
• Classes
• Forms
• Class Libraries
• Inheritance
• Other C# essentials
• XML + intro BizTalk
• BizTalk
• Seminars
2009-04-02
Datavetenskap, Karlstads universitet 2
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Contact
• Thijs Holleboom
• 21F411
• Thijs.Holleboom@kau.se
• 054-700 1148
• Per Hurtig
• 21F422
• Per.Hurtig@kau.se
• 054-700 2335
• Inger Bran
• 21E414
• Inger.Bran@kau.se
• 054-700 1970
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Programming
• All programming is the same– sequence
– iteration
– selection
• Manipulating computers
• Level of abstraction differs– assembler, c, c++, java, c#, lisp, prolog, sql
• Expressiveness varies– Some languages can do more
• If you know programming you can use any language (almost)
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Programming
SequenceIterationSelection
Memory op’sCalculations
Computer
Programming Language
Nifty StuffPre-done stuff
Abstraction
2009-04-02
Datavetenskap, Karlstads universitet 3
Karlstads universitet
Datavetenskap
DVGB07 – VT09
C++
Programming
Hardware (computer and peripherals)Machine code
Assembler Assembler
C Byte Code
Virtual Machine
Java
CIL
CLR
C#
Difficult
Full control
Easy
Less control
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Structure of .net technology
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Another visualisation of .net
2009-04-02
Datavetenskap, Karlstads universitet 4
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Important concepts
• FCL
– Common class library
• CTS
– Common type system
• CLR
– The environment
everything builds upon.
The ”motor” i a .net
application.
Karlstads universitet
Datavetenskap
DVGB07 – VT09
From code to program
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Common Language Runtime (CLR)
• Is essentially an implementation of the CLI
– Common Language Infrastructure
– Other implementations exist, e.g. the Mono
runtime
• Responsible for almost everything
• Uses only OS for threads and bulk memory
management
2009-04-02
Datavetenskap, Karlstads universitet 5
Karlstads universitet
Datavetenskap
DVGB07 – VT09
CLR
• JIT
– just in time-compiler
– Compiles code late
• Garbage Collector
– automatic memory handling
– You don't have to deallocate memory, it is
handled by the garbage collector
– In C/C++ you have to take care of your own
garbage, in C# you don't
Karlstads universitet
Datavetenskap
DVGB07 – VT09
CLR
• Common Type System (CTS)
– All languages in the .net family are basically the same,
except for the syntax
– You can include code written in one language into a
program written in another
• Exception handler
– When something goes wrong, the exception handler
comes into play.
Karlstads universitet
Datavetenskap
DVGB07 – VT09
• All .NET languages
compile to CIL, which
basically is advanced
assembler.
• The JIT compiles parts
of the CIL to native
object code, when that
part of the CIL is
needed.
Stubcode
CIL
Before JIT
Objectcode
After JIT
Common Intermediate Language (CIL)
2009-04-02
Datavetenskap, Karlstads universitet 6
Karlstads universitet
Datavetenskap
DVGB07 – VT09
CIL
• Is a strongly typed, verbose, assembler language
• stack-based
• generic
• low-level
• easy to compile
• has more features than are used by any existing supported language
– tail recursion (used in functional languages like LISP)
Karlstads universitet
Datavetenskap
DVGB07 – VT09
CIL Example
.assembly HelloWorld{}
.method public hidebysig static void Main() cil man aged {
.entrypoint ldstr "Hello World!" call void [mscorlib]System.Console::WriteLine(strin g) ret
}
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Garbage Collection
• Allocation is fast, simple list of used memory
instead of linked list
• Works well with cache memory
• Your code is smaller, simpler, and more reliable
– But in a sense uglier
2009-04-02
Datavetenskap, Karlstads universitet 7
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Garbage collection
• You never have memory leaks
– But your program is almost always too big
• Old stuff lying around
– Watch out when switching to a language without GC
or your program will leak
• alloc/free in C
• new/delete in C++
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Garbage Collection
• ”You may be thinking that it doesn't matter how garbage collection can help you if it means your program might lock up for several seconds anytime it gets asked to do something. And you'd be right - that did suck, back in the '70's and '80's on Lisp machines and such. ”
• Thus, garbage collection is nowadays a very fast process
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Common Type System (CTS)
• All .Net languages understand each other's data
types
• All use the same primitives
• Classes from different languages can inherit from
each other
• Value types
– scalars, such as integers
– records (eg. structs) and objects(!)
2009-04-02
Datavetenskap, Karlstads universitet 8
Karlstads universitet
Datavetenskap
DVGB07 – VT09
CTS
• Same base classes (highest level)
• All .Net languages’ highest objects can be cast to
any other.
• Interfaces are supported
– like in Java
– “Can do” instead of “Is-a” represented by inheritance
Karlstads universitet
Datavetenskap
DVGB07 – VT09
CTS
• All objects inherit via single
inheritance from a top-most
object
• Multiple inheritance is not
supported
– Good(?)
– multiple inheritance is complex
– multiple inheritance is seldom
needed
– interfaces are better
Object
…
…
…
… …
Karlstads universitet
Datavetenskap
DVGB07 – VT09
”Leaving” .NET
• So far, the lesson has covered theoretical aspects
of the .NET architecture
• Only a few things of what we covered have
practical implications
– Garbage collector
– The possibility to interact between .NET languages
• The rest of this lecture will be more practical!
2009-04-02
Datavetenskap, Karlstads universitet 9
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Namespaces
• We start with namespaces!
• Divided into layers hierarchically
– divided by period, like System.IO
• Code in different files can belong to the same
namespace
• Use is optional
– Except for components
• Helps avoid name clashes
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Namespaces
• ”using” removes need to type full namespace
name
– using System;
– makes it possible to for instance access
– Console.WriteLine();
– without spelling out
– System.Console.WriteLine();
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Namespaces
namespace x.y{
class Slask{}
}// is shorthand for writingnamespace x{
namespace y{
class Slask{}
}}
If one wants to use class Slask
from another namespace the
namespace must be addressed
Either:
using x.y;…Slask s=new Slask();
or:
x.y.Slask s=new x.y.Slask();
2009-04-02
Datavetenskap, Karlstads universitet 10
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Boxing
• Enables conversion from value type to reference
type
• Unboxing does the reverse, converting from
reference box to value type
• Boxing implies making a copy of the value
int i = 123;object box = i;if(box is int)
// box contains an int
int y = (int) box;// y contains int value// contained in box
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Keywords
abstract as base bool breakbyte case catch char checkedclass const continue decimal defaultdelegate do double else enumevent explicit extern false finallyfixed float for foreach gotoif implicit in int interfaceinternal is lock long namespacenew null object operator outoverride params private protected publicreadonly ref return sbyte sealedshort sizeof stackalloc static stringstruct switch this throw truetry typeof uint ulong uncheckedunsafe ushort using virtual voidvolatile while
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Visual Studio
• Graphical environment (IDE) for developing
.net-applications
• Microsoft
• Syntax highlighting
• Code completion
• GUI designer
– drag n drop, very easy to use, supernice
• Easy handling of events
2009-04-02
Datavetenskap, Karlstads universitet 11
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Visual Studio – competitors
• .net is standardized
• C# is also standardized
• Other compilers and environments exist
– e.g. Mono for Linux
Karlstads universitet
Datavetenskap
DVGB07 – VT09
Visual Studio (cont’d)
Karlstads universitet
Datavetenskap
DVGB07 – VT09
VS – starting a new project
2009-04-02
Datavetenskap, Karlstads universitet 12
Karlstads universitet
Datavetenskap
DVGB07 – VT09
VS – starting…(cont’d)
Karlstads universitet
Datavetenskap
DVGB07 – VT09
VS – editing
Karlstads universitet
Datavetenskap
DVGB07 – VT09
VS – handling events