Rozdzia 3.
Jzyki .NET
----------------------------------------------------------

C#
----------------------------------------------------------
Przykad C#
----------------------------------------------------------
// Przykad C#
interface IMath
{
   int Factorial(int f);
   double SquareRoot(double s);
}

class Compute : IMath
{
   public int Factorial(int f)
   {
      int i;
      int result = 1;
      for (i=2; i<=f; i++)
         result = result * i;
      return result;
   }

   public double SquareRoot(double s)
   {
      return System.Math.Sqrt(s);
   }
}

class DisplayValues
{
   static void Main()
   {
      Compute c = new Compute();
      int v;
      v = 5;
      System.Console.WriteLine(
         "{0} silnia: {1}",
         v, c.Factorial(v));
      System.Console.WriteLine(
         "Pierwiastek kwadratowy z {0}: {1:f4}",
         v, c.SquareRoot(v));
   }
}

----------------------------------------------------------
Typy w C#
----------------------------------------------------------
int i;
---
System.Int32 i;

----------------------------------------------------------
Klasy
----------------------------------------------------------
class Calculator : MathBasics, IAlgebra, ITrig { ... }
---
class PriorityValue
{
   private int pValue;
   public int Priority
   {
      get
      {
         return pValue;
      }
      set
      {
         if (value > 0 && value < 11)
            pValue = value;
      }
   }
}
class PropertyExample
{
   static void Main()
   {
      PriorityValue p = new PriorityValue();
      p.Priority = 8;
      System.Console.WriteLine("Priorytet: {0}",
         p.Priority);
   }
}

----------------------------------------------------------
Interfejsy
----------------------------------------------------------
Interface ITrig: ISine, ICosine, ITangent { ... }

----------------------------------------------------------
Struktury
----------------------------------------------------------
struct employee
{
   string name;
   int age;
}

----------------------------------------------------------
Tablice
----------------------------------------------------------
int[] ages;
---
ages = new int[10];
---
int[] ages = new int[10];
---
string[] names = new string[10];
---
int[,] points = new int[10,20];

----------------------------------------------------------
Delegaty i zdarzenia
----------------------------------------------------------
delegate void SDelegate(string s);
class DelegateExample
{
   public static void Main()
   {
      SDelegate del = new SDelegate(WriteString);
      CallDelegate(del);
   }
   public static void CallDelegate(SDelegate Write)
   {
      System.Console.WriteLine("W CallDelegate");
      Write("Witaj w delegacie");
   }
   public static void WriteString(string s)
   {
      System.Console.WriteLine("W WriteString: {0}", s);
   }
}
---
public delegate void MyEventHandler(object sender, MyEventArgs e);
---
public event MyEventHandler MyEvent;
---
public class EventSource
{
   public event System.EventHandler EventX;
   public void RaiseEventX()
   {
      if (EventX != null)
         EventX(this, System.EventArgs.Empty);
   }
}
public class EventSink
{
   public EventSink(EventSource es)
   {
      es.EventX += new
         System.EventHandler(ReceiveEvent);
   }
   public void ReceiveEvent(object sender,
      System.EventArgs e)
   {
      System.Console.WriteLine("EventX wywoane");
   }
}
public class EventMain
{
   public static void Main()
   {
      EventSource source = new EventSource();
      EventSink sink = new EventSink(source);
      source.RaiseEventX();
   }
}

----------------------------------------------------------
Typy generyczne
----------------------------------------------------------
class Pair<T>
{
   T element1, element2;

   public void SetPair(T first, T second)
   {
      element1 = first;
      element2 = second;
   }
   public T GetFirst()
   {
      return element1;
   }

   public T GetSecond()
   {
      return element2;
   }
}

class GenericsExample
{
   static void Main()
   {
      Pair<int> i = new Pair<int>();
      i.SetPair(42,48);
      System.Console.WriteLine("Para liczb cakowitych: {0} {1}",
         i.GetFirst(), i.GetSecond());

      Pair<string> s = new Pair<string>();
      s.SetPair("Carpe", "Diem");
      System.Console.WriteLine(
         "Para acuchw znakw: {0} {1}",
         s.GetFirst(), s.GetSecond());
   }
}
---
int? x;

----------------------------------------------------------
Struktury sterujce w C#
----------------------------------------------------------
if (x > y)
   p = true;
else
   p = false;
---
switch (x)
{
   case 1:
      y = 100;
      break;
   case 2:
      y = 200;
      break;
   default:
      y = 300;
      break;
}

----------------------------------------------------------
Inne cechy C#
----------------------------------------------------------
Praca z przestrzeniami nazw
----------------------------------------------------------
System.Console.WriteLine(...);
---
using System;
---
Console.WriteLine(...);

----------------------------------------------------------
Obsuga wyjtkw
----------------------------------------------------------
x = y/z;
---
try
{
   x = y/z;
}
catch
{
   System.Console.WriteLine("Wyjtek przechwycony");
}
---
try
{
   x = y/z;
}
catch (System.DivideByZeroException)
{
   System.Console.WriteLine("z jest zerem");
}
catch (System.Exception e)
{
   System.Console.WriteLine("Wyjtek: {0}", e.Message);
}

----------------------------------------------------------
Uywanie atrybutw
----------------------------------------------------------
[WebMethod] public int Factorial(int f) {...}
---
[assembly:AssemblyCompanyAttribute("QwickBank")]
---
class Risky
{
   unsafe public void PrintChars()
   {
      char[] charList = new char[2];
      charList[0] = 'A';
      charList[1] = 'B';

      System.Console.WriteLine("{0} {1}", charList[0], charList[1]);
      fixed (char* f = charList)
      {
         charList[0] = *(f+1);
      }
      System.Console.WriteLine("{0} {1}", charList[0], charList[1]);
   }
}

class DisplayValues
{
   static void Main()
   {
      Risky r = new Risky();
      r.PrintChars();
   }
}

----------------------------------------------------------
Dyrektywy preprocesora
----------------------------------------------------------
#define DEBUG
#if DEBUG
   // kod skompilowany, jeli DEBUG jest zdefiniowany
#else
   // kod skompilowany, jeli DEBUG nie jest zdefiniowany
#endif

----------------------------------------------------------
Visual Basic
----------------------------------------------------------
Przykad Visual Basic
----------------------------------------------------------
' Przykad VB
Module DisplayValues

Interface IMath
   Function Factorial(ByVal F As Integer) _
      As Integer
   Function SquareRoot(ByVal S As Double) _
      As Double
End Interface

Class Compute
   Implements IMath

   Function Factorial(ByVal F As Integer) _
      As Integer Implements IMath.Factorial
      Dim I As Integer
      Dim Result As Integer = 1

      For I = 2 To F
         Result = Result * I
      Next
      Return Result
   End Function

   Function SquareRoot(ByVal S As Double) _
      As Double Implements IMath.SquareRoot
      Return System.Math.Sqrt(S)
   End Function
End Class

Sub Main()
   Dim C As Compute = New Compute()
   Dim V As Integer
   V = 5
   System.Console.WriteLine( _
      "{0} silnia: {1}", _
      V, C.Factorial(V))
   System.Console.WriteLine( _
      "Pierwiastek kwadratowy z {0}: {1:f4}", _
      V, C.SquareRoot(V))
End Sub

End Module

----------------------------------------------------------
Typy w Visual Basic
----------------------------------------------------------
Klasy
----------------------------------------------------------
Class Calculator
   Inherits MathBasics
   Implements IAlgebra
   Implements ITrig
...
End Class
---
Module PropertyExample
   Class PriorityValue
      Private m_Value As Integer
      Public Property Priority() As Integer
         Get
            Return m_Value
         End Get
         Set(ByVal Value As Integer)
            If (Value > 0 And Value < 11) Then
               m_Value = Value
            End If
         End Set
      End Property
   End Class

   Sub Main()
      Dim P As PriorityValue = New PriorityValue()
      P.Priority = 8
      System.Console.WriteLine("Priorytet: {0}", _
        P.Priority)
   End Sub
End Module

----------------------------------------------------------
Interfejsy
----------------------------------------------------------
Interface ITrig
   Inherits ISine
   Inherits ICosine
   Inherits ITangent
...
End Interface

----------------------------------------------------------
Struktury
----------------------------------------------------------
Structure Employee
   Public Name As String
   Public Age As Integer
End Structure

----------------------------------------------------------
Tablice
----------------------------------------------------------
Dim Ages(10) as Integer
---
Dim Ages() As Integer
ReDim Ages(10)
---
Dim Points(10,20) As Integer

----------------------------------------------------------
Delegaty i zdarzenia
----------------------------------------------------------
Module DelegatesExample

   Delegate Sub SDelegate(ByVal S As String)
   Sub CallDelegate(ByVal Write As SDelegate)
      System.Console.WriteLine("W CallDelegate")
      Write("Witaj w delegacie")
   End Sub

   Sub WriteString(ByVal S As String)
      System.Console.WriteLine( _
         "W WriteString: {0}", S)
   End Sub

   Sub Main()
      Dim Del As New SDelegate( _
         AddressOf WriteString)
         CallDelegate(Del)
   End Sub

End Module
---
Module EventsExample
   Public Class EventSource
      Public Event EventX()
      Sub RaiseEventX()
         RaiseEvent EventX()
      End Sub
   End Class

   Public Class EventSink
      Private WithEvents Source As EventSource
      Public Sub New(ByVal Es As EventSource)
         Me.Source = Es
      End Sub
      Public Sub ReceiveEvent() _
         Handles Source.EventX
         System.Console.WriteLine("EventX wywoane")
      End Sub
   End Class

   Sub Main()
      Dim Source As EventSource = New EventSource()
      Dim Sink As EventSink = New EventSink(Source)
      Source.RaiseEventX()
   End Sub
End Module

----------------------------------------------------------
Typy generyczne
----------------------------------------------------------
Module GenericsExample
   Class pair(Of t)
      Dim element1, element2 As t
      Sub SetPair(ByVal first As t, ByVal second As t)
         element1 = first
         element2 = second
      End Sub

      Function GetFirst() As t
         Return element1
      End Function

      Function GetSecond() As t
         Return element2
      End Function
   End Class

   Sub Main()
      Dim i As New pair(Of Integer)
      i.SetPair(42, 48)
      System.Console.WriteLine( _
         "Para liczb cakowitych: {0} {1}", _
         i.GetFirst(), i.GetSecond())

      Dim s As New pair(Of String)
      s.SetPair("Carpe", "Diem")
      System.Console.WriteLine( _
         "Para acuchw znakw: {0} {1}", _
         s.GetFirst(), s.GetSecond())
   End Sub
End Module
---
Class pair(Of t)
---
class Pair<T>

----------------------------------------------------------
Struktury sterujce w Visual Basic
----------------------------------------------------------
If (X > Y) Then
   P = True
Else
   P = False
End If
---
Select Case X
   Case 1
      Y = 100
   Case 2
      Y = 200
   Case Else
      Y = 300
End Select

----------------------------------------------------------
Inne cechy Visual Basic
----------------------------------------------------------
Praca z przestrzeniami nazw
----------------------------------------------------------
Imports System
---
Console.WriteLine(...)

----------------------------------------------------------
Obsuga wyjtkw
----------------------------------------------------------
Try
   X = Y/Z
Catch
   System.Console.WriteLine("Wyjtek przechwycony")
End Try

----------------------------------------------------------
Uywanie atrybutw
----------------------------------------------------------
<WebMethod()> Public Function Factorial(ByVal F _
As Integer) As Integer Implements IMath.Factorial
---
<assembly:AssemblyCompanyAttribute("QwickBank")>

----------------------------------------------------------
C++
----------------------------------------------------------
C++/CLI
----------------------------------------------------------
Przykad C++/CLI
----------------------------------------------------------
// Przykad C++/CLI

interface class IMath
{
   int Factorial(int f);
   double SquareRoot(double s);
};

ref class Compute : public IMath
{
   public: virtual int Factorial(int f)
   {
      int i;
      int result = 1;
      for (i=2; i<=f; i++)
         result = result * i;
      return result;
   };

   public: virtual double SquareRoot(double s)
   {
      return System::Math::Sqrt(s);
   }
};

void main(void)
{
   Compute ^c = gcnew Compute;
   int v;
   v = 5;
   System::Console::WriteLine(
      "{0} silnia: {1}",
      v, c->Factorial(v));
   System::Console::WriteLine(
      "Pierwiastek kwadratowy z {0}: {1:f4}",
      v, c->SquareRoot(v));
}

----------------------------------------------------------
Inne cechy C++/CLI
----------------------------------------------------------
using namespace System;

----------------------------------------------------------
Managed C++
----------------------------------------------------------
Przykad Managed C++
----------------------------------------------------------
// Przykad Managed C++
#using <mscorlib.dll>

__gc __interface IMath
{
   int Factorial(int f);
   double SquareRoot(double s);
};

__gc class Compute : public IMath
{
   public: int Factorial(int f)
   {
      int i;
      int result = 1;
      for (i=2; i<=f; i++)
         result = result * i;
      return result;
   };
   public: double SquareRoot(double s)
   {
      return System::Math::Sqrt(s);
   }
};

void main(void)
{
   Compute *c = new Compute;
   int v;
   v = 5;
   System::Console::WriteLine(
      "{0} silnia: {1}",
      __box(v), __box(c->Factorial(v)));
   System::Console::WriteLine(
      "Pierwiastek kwadratowy z {0}: {1:f4}",
      __box(v), __box(c->SquareRoot(v)));
}
---










