Tech Talk – C# und das Microsoft .NET Framework Heinrich Wendel, DLR Simulations- und Softwaretechnik 30. Juli 2008
Entstehungsgeschichte Microsofts Dominanz der PC-Kerntechnologien in Gefahr  Wunsch nach konkurrenzfähiger Technik „gegen Java“ Alte, nicht zueinander kompatible Windows Technologien ersetzen
History 2000: Bill Gates stellt die .NET- „Vision“ vor 2002: .NET (V1.0) mit SDK wird vorgestellt 2003: .NET 1.1 und Visual Studio 2003 2005: Release des .NET Framework 2.0 und Visual Studio 2005 2006: .Net Framework 3.0 verfügbar 2007: .NET 3.5 und Visual Studio 2008
Architektur
Common Language Infrastructure Common Language Specification Minimales Set an Features zur Kompatibilität zwischen Sprachen [ClsCompliant(true)] Beispiel Pointer unsigned Typen Groß/Kleinschreibung Common Type System
Interoperabilität
Assemblies Bibliothek enthält CIL Code, Dateitype .dll / .exe Name Version Kultur (Satellite assemblies) Public Key (Strong name) Global Assembly Cache Eingebettete Resourcen Manifest
Dynamic Assembly loading Assemblies können dynamisch zur Laufzeit geladen aber nicht entladen werden Application Domains (vgl. OSGi) Abgetrennte Bereiche Sicherheit Konfiguration ExceptionHandling Können gestartet und gestoppt werden Kommunikation über .NET Remoting
C# - Namespaces // Defining namespaces namespace  Dlr.Sistec { class Test { … } } // Nested namespaces namespace  Dlr { namespace  Sistec { class Test {…} } } // Using namespaces Dlr.Sistec.Test  = new Dlr.Sistec.Test(); // Short form Using Dlr.Sistec; Test = new  Test(); // Aliases Using ns = Dlr.Sistec; ns.Test = new  ns.Test();
C# - Control Flow Iteration For Foreach While Do…While Kontrollstrukturen If/else if/else Kurzfassung: Bedingung ? Anweisung1 : Anweisung2 Switch
C# - Iterators Months months = new Months();  foreach (string temp in months)    Console.WriteLine(temp);  public class Months :  IEnumerable  {     string[] month = { &quot;Januar&quot;, &quot;Februar&quot;, &quot;März&quot;, &quot;April&quot;, &quot;Mai&quot;, &quot;Juni&quot;, &quot;Juli&quot;, &quot;August&quot;, &quot;September&quot;, &quot;Oktober&quot;, &quot;November&quot;, &quot;Dezember&quot;};   public IEnumerator GetEnumerator() {      for (int i = 0; i < month.Length; i++)         yield  return month[i];    }  }
C# - Switch / Enums string s = Console.ReadLine();; switch(s) { case „ January “:  … break; case „ Februar “:  … break; case „ March “:  … break; … }
C# - Switch / Enums public  Enum  Month { January = 1, Februar = 2, March = 3 … } Month m = …; switch (m) { case  Month.January :  break; case  Month.February :  break; case  3 :  break; … }
C# - Switch / Enums [Flags]  public enum Keys {  Shift = 1,  Ctrl = 2,  Alt = 4  } Keys k = Keys.Shift | Keys.Ctrl if ((k & Keys.Shift) == Keys.Shift) {…}
C# - Indexer String[] People = { „Schreiber, „Legenhausen“, „Wendel“}; Console.Write( People[0] ) People[1]  = Console.ReadLine() public  dataType   this[int index] { get { /// … } set { /// … } }
C# - Properties public class Person { private string  firstName ; private string  lastName ; public void  setFirstName (string name) this.firstName = name; }  public string  getLastName () return this.lastName; } public void  setLastName (string name) this.lastName = name }  public string  getFirstName () return this.firstName; } }   public class Person {    public string  FirstName  { get  { return firstName; } set  { firstName =  value ; } } private string  firstName ;   public string  LastName  { get  { return lastName; } set  { lastName =  value ; } } private string  lastName ; }   Public class Person {    public string  FirstName  {  get; set ; } public string  LastName  {  get; set ; } }
C# - Attributes Annotations in Java / Decorators in Python Anwendbar auf Assemblies, Klassen, Methoden, Felder … Vordefinierte Attribute [Serializable] [ClsCompliant(true)] [Obsolete] … Eigene Attribute
C# - Attributes [AttributeUsage(AttributeTargets.All)]  public class DeveloperAttribute :  Attribute  {  public string Zuname;  public int PersID; public DeveloperAttribute(string name) {  Zuname = name;  }  } [DeveloperAttribute(&quot;Meier&quot;, PersID = 8815)]  class Program {  static void Main(string[] args) {  DeveloperAttribute  attr  =  (DeveloperAttribute)Attribute.  GetCustomAttribute(typeof(Program),  typeof(DeveloperAttribute));  Console.WriteLine(&quot;Name: {0}&quot;,  attr.Zuname );  Console.WriteLine(&quot;PersID: {0}&quot;,  attr.PersID );  }
C# - Delegates / Events public class Logger { public  delegate  void Log(string message); private  static  Log  log ger ; public  static void Main(string[] args) { log ger  = new Log(PrintToConsole); log ger.Invoke (&quot;message&quot;); } public static void  PrintToConsole(string  message) { Console.WriteLine(message); } } public class Logger { public  delegate  void Log(string message); private  static  Log  logList; public  static void Main(string[] args) { logList  +=  new Log(PrintToConsole); logList  +=  new Log(PrintTo File ); logList (&quot;message&quot;); } public static void  PrintToConsole (string message) {  … }  public static void  PrintTo File (string message) {  … } } public class Logger { public  delegate  void Log(string message); public  static  event   Log  logList ; public  static void Main(string[] args) { logList  +=  PrintToConsole; logList  +=  PrintTo File ; logList (&quot;message&quot;); } public static void  PrintToConsole (string message) {  … }  public static void  PrintTo File (string message) {  … } }
C# - Lambda Funktionen public class Logger {  public delegate void Log(string message); private static Log logList; public  static void Main(string[] args) { logList =  delegate(string message)  { Console.WriteLine(message); }; logList(&quot;message&quot;); } } public class Logger {  public delegate void Log(string message); private static Log logList; public  static void Main(string[] args) { logList =  message  =>  { Console.WriteLine(message); }; logList(&quot;message&quot;); } } List <int> evenNumbers = list.FindAll(  i => (i%2) == 0  );
Queries auf IEnumerable<T>, Relationale Datenbanken, XML Dateien from, in, where, select, join, on, equals, into, orderby, ascending, descending, group, by C# - LINQ int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 }; var  subset =  from i in numbers where i < 10 select i ; foreach (int i in subset) Console.WriteLine(i);
C# - Structs Fast alle Features einer Klasse Keine Vererbung Value Types statt Reference Types public  struct  Point {    int x {get; set} int y {get; set} public Point(int x, int y) { … } public int getDistance(int x, int y) { … } public static Point operator +(Point p) {…} }
C# - Constructors public class Point { public Point(int x, int y) { X = x; Y = y; } public Point() { } public int X { get; set; } public int Y { get; set; } } Point p = new Point(1, 2) Point p = new Point() P.X = 1; p.Y = 2; Point p = new Point { X = 1, Y = 2 }
C# - Constructors public class Point { public int X { get; set; } public int Y { get; set; } } public class Rectangle { public Point topLeft { get; set; } public Point bottomRight { get; set; } } Rectangle r = new Rectangle {  topLeft = new Point{ X = 0, Y = 0 }, bottomRight = new Point { X = 2, Y = 2 }  } Rectangle r = new Rectangle(); Point p1 = new Point(); p1.X = 0; p1.Y = 0; Point p2 = new Point(); p2.X = 2; p2.Y = 2; r.topLeft = p1; r.bottomRight = p2;
C# - Unsafe Code Erlaubt Pointer-Operationen unsafe keyword Höhere Performance Fixed keyword int var1 = 5;  unsafe   {  int  *  ptr1, ptr2;  ptr1 =  & var1;  ptr2 = ptr1;  *ptr2 = 20;  }  Console.WriteLine(var1);
C# - Preprocessor #define / #undef #if / #else / #elif / #endif #region / #endregion
C# - Exception Handling try / catch / finally Python like statt Java like Exceptions müssen nicht gefangen und deklariert werden! Nicht mehrere in einem catch-Block behandelbar Exception Klasse TargetSite StackTrace HelpLink Data Eigene Exceptions sollen von ApplicationException ableiten SystemException IndexOutOfRangeException NullReferenceException …
C# - Method Calls void FunctionA( ref  int Val) { int x= Val;  Val = x* 4;  } int a= 5; FunctionA( ref  a); Console.WriteLine(a);  bool GetNodeValue( out  int Val)  { Val = 1; return true;  }  int Val; GetNodeValue(Val); Console.WriteLine(a);  void Func(params  int[]  array) { Console.WriteLine(array.Length); } Func(1); Func(7,9); Func(new int[] {3,8,10});
C# - Extension Methods public  static  class  ExtensionClass  { public  static  int MultiplyByTwo( this int number) { return number * 2; } } public class Class1 { public static void Main(string[] args) { int number = 2; Console.WriteLine( number.MultiplyByTwo() ); } }
C# - Casting object [] myObjects = new object[1] myObjects[0] = new MyClass1(); string s; // Exception s = (string) myObjects[0] public static explicit operator String(MyClass1) { return “SomeString”; } If (myObjects[0]  is  string) { s = (string) myObjects[1] } s = myObjects[0] as string
C# - Keywordmania sealed internal / protected internal const readonly this / base nullable (?) partial (un)checked abstract / virtual / override / new
C# - Was sonst noch geht … Generics Operatoren überladen Strings können mit == übeprüft werden Auto(un)boxing Eigene Listen (System.Collecions / System.Collections.Generic) Sortierung (IComparable / ICompare) String formatierung (String.Format)
Library Isolated Storage ADO.NET Windows Communication Foundation Windows Workflow Foundation Windows Forms Windows Presentation Foundation
Parallel Programming Builtin Mechanisms: Locks Monitors synchronisation keyword Threads ParallelFX Parallel.For LINQ per default parallelisiert
Entwicklungsumgebungen Microsoft Visual Studio Express Standard Professional Team System (Architecture-, Database-, Development-, Test- Edition) (http://msdn.microsoft.com/de-de/vs2008/cc149003.aspx) SharpDevelop MonoDevelop
Tools Nunit Ndoc FxCop Lutz Roeder's Reflector NAnt
Lizenzen CLI und C#: ECMA Standard VM (CIL) C# Compiler Neue Sprachen Klassenbibliotheken und andere Teile: shared source WindowsForms  ADO.NET ASP.NET
Alternative .NET Implementierungen Mono DotGNU CrossNet (Assemblies to Unmanaged C++) .NET for Symbian Shared Source Common Language Infrastructure (MS)
IKVM.NET Java für .NET Java Virtual Machine .NET implementation der Java Klassenbibliothek Tools um Java und .NET Programme zu verbinden Java Bytecode in einer JVM auf der CLR laufen lassen Java in CLI compilieren Noch kein AWT/Swing
IronPython Implementiernug von Python für .NET Keine C-Extensions (e.g. NumPy) Ironclad Dynamic Language Runtime Dynamic Type System JScript VBx Integration in Silverlight Applikationen
 

TechTalk - Dotnet

  • 1.
    Tech Talk –C# und das Microsoft .NET Framework Heinrich Wendel, DLR Simulations- und Softwaretechnik 30. Juli 2008
  • 2.
    Entstehungsgeschichte Microsofts Dominanzder PC-Kerntechnologien in Gefahr Wunsch nach konkurrenzfähiger Technik „gegen Java“ Alte, nicht zueinander kompatible Windows Technologien ersetzen
  • 3.
    History 2000: BillGates stellt die .NET- „Vision“ vor 2002: .NET (V1.0) mit SDK wird vorgestellt 2003: .NET 1.1 und Visual Studio 2003 2005: Release des .NET Framework 2.0 und Visual Studio 2005 2006: .Net Framework 3.0 verfügbar 2007: .NET 3.5 und Visual Studio 2008
  • 4.
  • 5.
    Common Language InfrastructureCommon Language Specification Minimales Set an Features zur Kompatibilität zwischen Sprachen [ClsCompliant(true)] Beispiel Pointer unsigned Typen Groß/Kleinschreibung Common Type System
  • 6.
  • 7.
    Assemblies Bibliothek enthältCIL Code, Dateitype .dll / .exe Name Version Kultur (Satellite assemblies) Public Key (Strong name) Global Assembly Cache Eingebettete Resourcen Manifest
  • 8.
    Dynamic Assembly loadingAssemblies können dynamisch zur Laufzeit geladen aber nicht entladen werden Application Domains (vgl. OSGi) Abgetrennte Bereiche Sicherheit Konfiguration ExceptionHandling Können gestartet und gestoppt werden Kommunikation über .NET Remoting
  • 9.
    C# - Namespaces// Defining namespaces namespace Dlr.Sistec { class Test { … } } // Nested namespaces namespace Dlr { namespace Sistec { class Test {…} } } // Using namespaces Dlr.Sistec.Test = new Dlr.Sistec.Test(); // Short form Using Dlr.Sistec; Test = new Test(); // Aliases Using ns = Dlr.Sistec; ns.Test = new ns.Test();
  • 10.
    C# - ControlFlow Iteration For Foreach While Do…While Kontrollstrukturen If/else if/else Kurzfassung: Bedingung ? Anweisung1 : Anweisung2 Switch
  • 11.
    C# - IteratorsMonths months = new Months(); foreach (string temp in months) Console.WriteLine(temp); public class Months : IEnumerable {   string[] month = { &quot;Januar&quot;, &quot;Februar&quot;, &quot;März&quot;, &quot;April&quot;, &quot;Mai&quot;, &quot;Juni&quot;, &quot;Juli&quot;, &quot;August&quot;, &quot;September&quot;, &quot;Oktober&quot;, &quot;November&quot;, &quot;Dezember&quot;};   public IEnumerator GetEnumerator() {     for (int i = 0; i < month.Length; i++)       yield return month[i];   } }
  • 12.
    C# - Switch/ Enums string s = Console.ReadLine();; switch(s) { case „ January “: … break; case „ Februar “: … break; case „ March “: … break; … }
  • 13.
    C# - Switch/ Enums public Enum Month { January = 1, Februar = 2, March = 3 … } Month m = …; switch (m) { case Month.January : break; case Month.February : break; case 3 : break; … }
  • 14.
    C# - Switch/ Enums [Flags] public enum Keys { Shift = 1, Ctrl = 2, Alt = 4 } Keys k = Keys.Shift | Keys.Ctrl if ((k & Keys.Shift) == Keys.Shift) {…}
  • 15.
    C# - IndexerString[] People = { „Schreiber, „Legenhausen“, „Wendel“}; Console.Write( People[0] ) People[1] = Console.ReadLine() public dataType this[int index] { get { /// … } set { /// … } }
  • 16.
    C# - Propertiespublic class Person { private string firstName ; private string lastName ; public void setFirstName (string name) this.firstName = name; } public string getLastName () return this.lastName; } public void setLastName (string name) this.lastName = name } public string getFirstName () return this.firstName; } } public class Person { public string FirstName { get { return firstName; } set { firstName = value ; } } private string firstName ; public string LastName { get { return lastName; } set { lastName = value ; } } private string lastName ; } Public class Person { public string FirstName { get; set ; } public string LastName { get; set ; } }
  • 17.
    C# - AttributesAnnotations in Java / Decorators in Python Anwendbar auf Assemblies, Klassen, Methoden, Felder … Vordefinierte Attribute [Serializable] [ClsCompliant(true)] [Obsolete] … Eigene Attribute
  • 18.
    C# - Attributes[AttributeUsage(AttributeTargets.All)] public class DeveloperAttribute : Attribute { public string Zuname; public int PersID; public DeveloperAttribute(string name) { Zuname = name; } } [DeveloperAttribute(&quot;Meier&quot;, PersID = 8815)] class Program { static void Main(string[] args) { DeveloperAttribute attr = (DeveloperAttribute)Attribute. GetCustomAttribute(typeof(Program), typeof(DeveloperAttribute)); Console.WriteLine(&quot;Name: {0}&quot;, attr.Zuname ); Console.WriteLine(&quot;PersID: {0}&quot;, attr.PersID ); }
  • 19.
    C# - Delegates/ Events public class Logger { public delegate void Log(string message); private static Log log ger ; public static void Main(string[] args) { log ger = new Log(PrintToConsole); log ger.Invoke (&quot;message&quot;); } public static void PrintToConsole(string message) { Console.WriteLine(message); } } public class Logger { public delegate void Log(string message); private static Log logList; public static void Main(string[] args) { logList += new Log(PrintToConsole); logList += new Log(PrintTo File ); logList (&quot;message&quot;); } public static void PrintToConsole (string message) { … } public static void PrintTo File (string message) { … } } public class Logger { public delegate void Log(string message); public static event Log logList ; public static void Main(string[] args) { logList += PrintToConsole; logList += PrintTo File ; logList (&quot;message&quot;); } public static void PrintToConsole (string message) { … } public static void PrintTo File (string message) { … } }
  • 20.
    C# - LambdaFunktionen public class Logger { public delegate void Log(string message); private static Log logList; public static void Main(string[] args) { logList = delegate(string message) { Console.WriteLine(message); }; logList(&quot;message&quot;); } } public class Logger { public delegate void Log(string message); private static Log logList; public static void Main(string[] args) { logList = message => { Console.WriteLine(message); }; logList(&quot;message&quot;); } } List <int> evenNumbers = list.FindAll( i => (i%2) == 0 );
  • 21.
    Queries auf IEnumerable<T>,Relationale Datenbanken, XML Dateien from, in, where, select, join, on, equals, into, orderby, ascending, descending, group, by C# - LINQ int[] numbers = { 10, 20, 30, 40, 1, 2, 3, 8 }; var subset = from i in numbers where i < 10 select i ; foreach (int i in subset) Console.WriteLine(i);
  • 22.
    C# - StructsFast alle Features einer Klasse Keine Vererbung Value Types statt Reference Types public struct Point { int x {get; set} int y {get; set} public Point(int x, int y) { … } public int getDistance(int x, int y) { … } public static Point operator +(Point p) {…} }
  • 23.
    C# - Constructorspublic class Point { public Point(int x, int y) { X = x; Y = y; } public Point() { } public int X { get; set; } public int Y { get; set; } } Point p = new Point(1, 2) Point p = new Point() P.X = 1; p.Y = 2; Point p = new Point { X = 1, Y = 2 }
  • 24.
    C# - Constructorspublic class Point { public int X { get; set; } public int Y { get; set; } } public class Rectangle { public Point topLeft { get; set; } public Point bottomRight { get; set; } } Rectangle r = new Rectangle { topLeft = new Point{ X = 0, Y = 0 }, bottomRight = new Point { X = 2, Y = 2 } } Rectangle r = new Rectangle(); Point p1 = new Point(); p1.X = 0; p1.Y = 0; Point p2 = new Point(); p2.X = 2; p2.Y = 2; r.topLeft = p1; r.bottomRight = p2;
  • 25.
    C# - UnsafeCode Erlaubt Pointer-Operationen unsafe keyword Höhere Performance Fixed keyword int var1 = 5; unsafe { int * ptr1, ptr2; ptr1 = & var1; ptr2 = ptr1; *ptr2 = 20; } Console.WriteLine(var1);
  • 26.
    C# - Preprocessor#define / #undef #if / #else / #elif / #endif #region / #endregion
  • 27.
    C# - ExceptionHandling try / catch / finally Python like statt Java like Exceptions müssen nicht gefangen und deklariert werden! Nicht mehrere in einem catch-Block behandelbar Exception Klasse TargetSite StackTrace HelpLink Data Eigene Exceptions sollen von ApplicationException ableiten SystemException IndexOutOfRangeException NullReferenceException …
  • 28.
    C# - MethodCalls void FunctionA( ref int Val) { int x= Val; Val = x* 4; } int a= 5; FunctionA( ref a); Console.WriteLine(a); bool GetNodeValue( out int Val) { Val = 1; return true; } int Val; GetNodeValue(Val); Console.WriteLine(a); void Func(params int[] array) { Console.WriteLine(array.Length); } Func(1); Func(7,9); Func(new int[] {3,8,10});
  • 29.
    C# - ExtensionMethods public static class ExtensionClass { public static int MultiplyByTwo( this int number) { return number * 2; } } public class Class1 { public static void Main(string[] args) { int number = 2; Console.WriteLine( number.MultiplyByTwo() ); } }
  • 30.
    C# - Castingobject [] myObjects = new object[1] myObjects[0] = new MyClass1(); string s; // Exception s = (string) myObjects[0] public static explicit operator String(MyClass1) { return “SomeString”; } If (myObjects[0] is string) { s = (string) myObjects[1] } s = myObjects[0] as string
  • 31.
    C# - Keywordmaniasealed internal / protected internal const readonly this / base nullable (?) partial (un)checked abstract / virtual / override / new
  • 32.
    C# - Wassonst noch geht … Generics Operatoren überladen Strings können mit == übeprüft werden Auto(un)boxing Eigene Listen (System.Collecions / System.Collections.Generic) Sortierung (IComparable / ICompare) String formatierung (String.Format)
  • 33.
    Library Isolated StorageADO.NET Windows Communication Foundation Windows Workflow Foundation Windows Forms Windows Presentation Foundation
  • 34.
    Parallel Programming BuiltinMechanisms: Locks Monitors synchronisation keyword Threads ParallelFX Parallel.For LINQ per default parallelisiert
  • 35.
    Entwicklungsumgebungen Microsoft VisualStudio Express Standard Professional Team System (Architecture-, Database-, Development-, Test- Edition) (http://msdn.microsoft.com/de-de/vs2008/cc149003.aspx) SharpDevelop MonoDevelop
  • 36.
    Tools Nunit NdocFxCop Lutz Roeder's Reflector NAnt
  • 37.
    Lizenzen CLI undC#: ECMA Standard VM (CIL) C# Compiler Neue Sprachen Klassenbibliotheken und andere Teile: shared source WindowsForms ADO.NET ASP.NET
  • 38.
    Alternative .NET ImplementierungenMono DotGNU CrossNet (Assemblies to Unmanaged C++) .NET for Symbian Shared Source Common Language Infrastructure (MS)
  • 39.
    IKVM.NET Java für.NET Java Virtual Machine .NET implementation der Java Klassenbibliothek Tools um Java und .NET Programme zu verbinden Java Bytecode in einer JVM auf der CLR laufen lassen Java in CLI compilieren Noch kein AWT/Swing
  • 40.
    IronPython Implementiernug vonPython für .NET Keine C-Extensions (e.g. NumPy) Ironclad Dynamic Language Runtime Dynamic Type System JScript VBx Integration in Silverlight Applikationen
  • 41.

Editor's Notes

  • #2 Coding Guidelines Sprache