Skip to content
Snippets Groups Projects
Commit 79ce931d authored by Uwe Wienkop's avatar Uwe Wienkop
Browse files

Generische Delegates

parent d9956081
No related branches found
No related tags found
No related merge requests found
...@@ -37,7 +37,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P15 GenericSort", "P15 Gene ...@@ -37,7 +37,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P15 GenericSort", "P15 Gene
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P16 GenericKeyValueList", "P16 GenericKeyValueList\P16 GenericKeyValueList.csproj", "{43F7D344-EBBF-4672-9452-AE59A1C977BF}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P16 GenericKeyValueList", "P16 GenericKeyValueList\P16 GenericKeyValueList.csproj", "{43F7D344-EBBF-4672-9452-AE59A1C977BF}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P17 Intro Delegates", "P17 Intro Delegates\P17 Intro Delegates.csproj", "{8E449A0E-B181-4E03-9BF5-732A32CF7A02}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P17 Intro Delegates", "P17 Intro Delegates\P17 Intro Delegates.csproj", "{8E449A0E-B181-4E03-9BF5-732A32CF7A02}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P18 Delegate Uebg", "P18 Delegate Uebg\P18 Delegate Uebg.csproj", "{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P19 WPF_Demo", "P19 WPF_Demo\P19 WPF_Demo.csproj", "{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P20 Liste mit Delegate", "P20 Liste mit Delegate\P20 Liste mit Delegate.csproj", "{FF28F272-A827-4234-B95C-947C69D26349}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
...@@ -117,6 +123,18 @@ Global ...@@ -117,6 +123,18 @@ Global
{8E449A0E-B181-4E03-9BF5-732A32CF7A02}.Debug|Any CPU.Build.0 = Debug|Any CPU {8E449A0E-B181-4E03-9BF5-732A32CF7A02}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8E449A0E-B181-4E03-9BF5-732A32CF7A02}.Release|Any CPU.ActiveCfg = Release|Any CPU {8E449A0E-B181-4E03-9BF5-732A32CF7A02}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8E449A0E-B181-4E03-9BF5-732A32CF7A02}.Release|Any CPU.Build.0 = Release|Any CPU {8E449A0E-B181-4E03-9BF5-732A32CF7A02}.Release|Any CPU.Build.0 = Release|Any CPU
{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}.Release|Any CPU.Build.0 = Release|Any CPU
{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}.Release|Any CPU.Build.0 = Release|Any CPU
{FF28F272-A827-4234-B95C-947C69D26349}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FF28F272-A827-4234-B95C-947C69D26349}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FF28F272-A827-4234-B95C-947C69D26349}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FF28F272-A827-4234-B95C-947C69D26349}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
double schrittweite, ZulaessigeFunktionen fkt) double schrittweite, ZulaessigeFunktionen fkt)
{ {
for (double x = von; x <= bis ; x+= schrittweite) for (double x = von; x <= bis ; x+= schrittweite)
Console.WriteLine( $"{x:f8} | {fkt(x):f8}"); Console.WriteLine( $"{x,8:f2} | {fkt(x),8:f2}");
Console.WriteLine( "------------"); Console.WriteLine( "------------");
} }
static void Main(string[] args) static void Main(string[] args)
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
ZulaessigeFunktionen meineFunktRef; ZulaessigeFunktionen meineFunktRef;
meineFunktRef = Square; meineFunktRef = Square;
Console.WriteLine(meineFunktRef(10)); Console.WriteLine(meineFunktRef(10));
meineFunktRef = z => z + 10; meineFunktRef += z => z + 10;
Console.WriteLine(meineFunktRef(10)); Console.WriteLine(meineFunktRef(10));
} }
} }
......
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>P18_Delegate_Uebg</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
namespace P18_Delegate_Uebg
{
// 1) Erstellen Sie eine Methode PrintAll, die ein Feld eines
// beliebigen Typs entgegennimmt und alle Feldelemente
// in einer Zeile ausdruckt.
//
// 2) Erstellen Sie eine Methode ForAll, die eine Methode und
// ein Feld eines beliebigen Typs entgegennimmt (gerne auch
// per params) und die Methode auf alle Feldelemente anwendet
// und das geänderte Feld zurückliefert.
// ForAll(x=>x*x, 1,2,3,4,5)
//
// 3) Erstellen Sie eine Methode Select, die wiederum eine
// Methode und ein Feld eines beliebigen Typs entgegennimmt
// und ein Ergebnisfeld zurückliefert, welches nur diejenigen
// Feldelemente enthält, die den Kriterien der Filtermethode
// entsprechen.
// Select(x=>x%2==0, 1,2,3,4,5)
internal class Program
{
delegate T ChangeFn<T>(T x);
delegate bool FilterFn<T>(T x);
static int[] ForAll(ChangeFn<int> fn, params int[] array)
{
for (int i = 0; i < array.Length; i++)
{
array[i] = fn(array[i]);
}
return array;
}
//static T[] ForAll<T>(ChangeFn<T> fn, params T[] array)
//{
//}
static T[] Select<T>(FilterFn<T> fn, T[] array)
{
List<T> list = new List<T>();
list.Add(array[0]);
return list.ToArray();
}
static void Main(string[] args)
{
ForAll(x => x * x, 1, 2, 3, 4, 5);
}
}
}
<Application x:Class="P19_WPF_Demo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:P19_WPF_Demo"
StartupUri="MainWindow.xaml">
<Application.Resources>
</Application.Resources>
</Application>
using System.Configuration;
using System.Data;
using System.Windows;
namespace P19_WPF_Demo
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
}
}
using System.Windows;
[assembly: ThemeInfo(
ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located
//(used if a resource is not found in the page,
// or application resource dictionaries)
ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located
//(used if a resource is not found in the page,
// app, or any theme specific resource dictionaries)
)]
<Window x:Class="P19_WPF_Demo.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:P19_WPF_Demo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="77*"/>
<ColumnDefinition Width="723*"/>
</Grid.ColumnDefinitions>
<TextBox x:Name="meineTextbox" Grid.Column="1" HorizontalAlignment="Left" Margin="178,114,0,0" TextWrapping="Wrap" Text="Mein Text" VerticalAlignment="Top" Width="271" Height="103" MouseEnter="MausFaehrtHinein" MouseLeave="MausVerlaesstTb"/>
<Button Grid.Column="1" Content="Start!" HorizontalAlignment="Left" Margin="361,282,0,0" VerticalAlignment="Top" Click="Button_Click"/>
<Label x:Name="meinLabel" Grid.Column="1" Content="Label" HorizontalAlignment="Left" Margin="494,274,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Width="193">
<Label.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="1.17"/>
<TranslateTransform/>
</TransformGroup>
</Label.RenderTransform>
</Label>
</Grid>
</Window>
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace P19_WPF_Demo
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void MausFaehrtHinein(object sender, MouseEventArgs e)
{
if (sender is TextBox tb)
{
var defaultBrush = tb.Background;
tb.Background = new SolidColorBrush(Colors.Red);
tb.Tag = defaultBrush;
}
}
private void MausVerlaesstTb(object sender, MouseEventArgs e)
{
if (sender is TextBox tb && tb.Tag is Brush defaultbrush)
{
tb.Background = defaultbrush;
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
meinLabel.Content = meineTextbox.Text;
}
}
}
\ No newline at end of file
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<RootNamespace>P19_WPF_Demo</RootNamespace>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>P20_Liste_mit_Delegate</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

namespace P20_Liste_mit_Delegate
{
class Liste<T>
{
class LItem
{
public T data;
public LItem next = null;
public LItem(T data) { this.data = data; }
}
LItem first = null, last = null;
public void Add(params T[] dataarray)
{
foreach (var data in dataarray)
{
LItem item = new LItem(data);
if (last == null)
{
first = last = item;
}
else
{
last = last.next = item;
}
}
}
public delegate bool meinTest<T>(T s);
internal IEnumerable<T> AllPers(meinTest<T> test)
{
for (LItem item = first; item != null; item = item.next)
{
if (test(item.data)==true)
yield return item.data;
}
}
}
internal class Program
{
static void Main(string[] args)
{
Liste<string> pers = new Liste<string>();
pers.Add("Anton");
pers.Add("Berta", "Claudia", "Dieter", "Egon");
//foreach (var item in pers.AllPers(s => s.Contains("er")))
foreach (var item in pers.AllPers(s => s[^1] == 'n'))
{
Console.WriteLine(item);
}
Liste<int> il = new();
il.Add(1,2,3,4,5,6,7,8,9);
foreach (var item in il.AllPers(x => x%2==0))
{
Console.WriteLine(item);
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment