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

Delegate Variablen

parent 79ce931d
No related branches found
No related tags found
No related merge requests found
...@@ -39,11 +39,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P16 GenericKeyValueList", " ...@@ -39,11 +39,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P16 GenericKeyValueList", "
EndProject EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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 EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P18 Delegate Uebg", "P18 Delegate Uebg\P18 Delegate Uebg.csproj", "{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P18 Delegate Uebg", "P18 Delegate Uebg\P18 Delegate Uebg.csproj", "{0E2DADFD-9A74-499C-8B0F-8D5F05A0C890}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P19 WPF_Demo", "P19 WPF_Demo\P19 WPF_Demo.csproj", "{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P19 WPF_Demo", "P19 WPF_Demo\P19 WPF_Demo.csproj", "{5E98294F-B48D-4D2D-B26E-43B36AEAD46F}"
EndProject 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}" Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P20 Liste mit Delegate", "P20 Liste mit Delegate\P20 Liste mit Delegate.csproj", "{FF28F272-A827-4234-B95C-947C69D26349}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P21 Ref", "P21 Ref\P21 Ref.csproj", "{BFA3D217-5696-43B1-94A5-2B90851732EA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "P22 DelVariableButton", "P22 DelVariableButton\P22 DelVariableButton.csproj", "{32D834E6-C426-432B-A162-78CE0E88E714}"
EndProject EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
...@@ -135,6 +139,14 @@ Global ...@@ -135,6 +139,14 @@ Global
{FF28F272-A827-4234-B95C-947C69D26349}.Debug|Any CPU.Build.0 = 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.ActiveCfg = Release|Any CPU
{FF28F272-A827-4234-B95C-947C69D26349}.Release|Any CPU.Build.0 = Release|Any CPU {FF28F272-A827-4234-B95C-947C69D26349}.Release|Any CPU.Build.0 = Release|Any CPU
{BFA3D217-5696-43B1-94A5-2B90851732EA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BFA3D217-5696-43B1-94A5-2B90851732EA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BFA3D217-5696-43B1-94A5-2B90851732EA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BFA3D217-5696-43B1-94A5-2B90851732EA}.Release|Any CPU.Build.0 = Release|Any CPU
{32D834E6-C426-432B-A162-78CE0E88E714}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{32D834E6-C426-432B-A162-78CE0E88E714}.Debug|Any CPU.Build.0 = Debug|Any CPU
{32D834E6-C426-432B-A162-78CE0E88E714}.Release|Any CPU.ActiveCfg = Release|Any CPU
{32D834E6-C426-432B-A162-78CE0E88E714}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE
......
...@@ -21,7 +21,24 @@ ...@@ -21,7 +21,24 @@
delegate T ChangeFn<T>(T x); delegate T ChangeFn<T>(T x);
delegate bool FilterFn<T>(T x); delegate bool FilterFn<T>(T x);
static int[] ForAll(ChangeFn<int> fn, params int[] array) //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 void PrintAll<T>(params T[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.Write($"{array[i]} ");
}
Console.WriteLine();
}
static T[] ForAll<T>(Func<T,T> fn, params T[] array)
{ {
for (int i = 0; i < array.Length; i++) for (int i = 0; i < array.Length; i++)
{ {
...@@ -29,19 +46,20 @@ ...@@ -29,19 +46,20 @@
} }
return array; return array;
} }
//static T[] ForAll<T>(ChangeFn<T> fn, params T[] array) static T[] Select<T>(Predicate<T> test, T[] array)
//{
//}
static T[] Select<T>(FilterFn<T> fn, T[] array)
{ {
List<T> list = new List<T>(); List<T> list = new List<T>();
list.Add(array[0]); for (int i = 0; i < array.Length; i++)
{
if (test(array[i]) == true)
list.Add(array[i]);
}
return list.ToArray(); return list.ToArray();
} }
static void Main(string[] args) static void Main(string[] args)
{ {
ForAll(x => x * x, 1, 2, 3, 4, 5); PrintAll(Select(x=> x%2==0, ForAll(x => x * x, 1, 2, 3, 4, 5)));
PrintAll(ForAll(s => s.ToUpper(), "Anton", "Berta", "Claudia", "Dieter"));
} }
} }
} }
...@@ -23,11 +23,10 @@ namespace P20_Liste_mit_Delegate ...@@ -23,11 +23,10 @@ namespace P20_Liste_mit_Delegate
{ {
last = last.next = item; last = last.next = item;
} }
} }
} }
public delegate bool meinTest<T>(T s); //public delegate bool Predicate<U>(U s);
internal IEnumerable<T> AllPers(meinTest<T> test) public IEnumerable<T> GetData(Predicate<T> test)
{ {
for (LItem item = first; item != null; item = item.next) for (LItem item = first; item != null; item = item.next)
{ {
...@@ -35,6 +34,11 @@ namespace P20_Liste_mit_Delegate ...@@ -35,6 +34,11 @@ namespace P20_Liste_mit_Delegate
yield return item.data; yield return item.data;
} }
} }
public IEnumerator<T> GetEnumerator()
{
for (LItem item = first; item != null; item = item.next)
yield return item.data;
}
} }
internal class Program internal class Program
{ {
...@@ -43,18 +47,22 @@ namespace P20_Liste_mit_Delegate ...@@ -43,18 +47,22 @@ namespace P20_Liste_mit_Delegate
Liste<string> pers = new Liste<string>(); Liste<string> pers = new Liste<string>();
pers.Add("Anton"); pers.Add("Anton");
pers.Add("Berta", "Claudia", "Dieter", "Egon"); pers.Add("Berta", "Claudia", "Dieter", "Egon");
//foreach (var item in pers.AllPers(s => s.Contains("er"))) //foreach (var item in pers.GetData(s => s.Contains("er")))
foreach (var item in pers.AllPers(s => s[^1] == 'n')) foreach (var item in pers.GetData(LetztesZeichenN))
{ {
Console.WriteLine(item); Console.WriteLine(item);
} }
Liste<int> il = new(); Liste<int> il = new();
il.Add(1,2,3,4,5,6,7,8,9); il.Add(1,2,3,4,5,6,7,8,9);
foreach (var item in il.AllPers(x => x%2==0)) foreach (var item in il.GetData(x => x%2==0))
{ {
Console.WriteLine(item); Console.WriteLine(item);
} }
} }
static bool LetztesZeichenN(string s)
{
return s[^1] == 'n';
}
} }
} }
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>P21_Ref</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
namespace P21_Ref
{
internal class Program
{
static void DoIt1(int[] f)
{
for (int i = 0; i < f.Length; i++)
{
f[i] = f[i] + 1;
}
}
static void DoIt2(int[] f)
{
int[] erg = new int[f.Length];
for (int i = 0; i < f.Length; i++)
{
erg[i] = f[i] + 1;
}
f=erg;
}
static void DoIt3(ref int[] f)
{
int[] erg = new int[f.Length];
for (int i = 0; i < f.Length; i++)
{
erg[i] = f[i] + 1;
}
f = erg;
}
static void Main(string[] args)
{
int[] ff = { 1, 2, 3, 4, 5 }; // #100.000
DoIt3(ref ff);
for (int i = 0; i < ff.Length; i++)
{
Console.WriteLine(ff[i]);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>P22_DelVariableButton</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>
namespace P22_DelVariableButton
{
public delegate void OnClickDelegate(string s);
class Button
{
public event OnClickDelegate onClick;
// Event ~ Einschränkung des Del.-Zugriffs:
// nur += und -= sowie Del.Aufruf aus der definierenden Klasse
public void FireClick(string msg)
{
//if (onClick != null)
// onClick(msg);
onClick?.Invoke(msg);
foreach (var item in onClick.GetInvocationList()) {
((OnClickDelegate) item)(msg);
}
}
}
class Program
{
static void MeinClickHandler(string s)
{
Console.WriteLine($"Es ist ein Klick erfolgt. Msg: {s}");
}
static void Main(string[] args)
{
Button button = new Button();
//button.FireClick("Klick");
// Registrierung der interessierten Event-Handler
button.onClick += MeinClickHandler;
button.onClick += s => Console.WriteLine($"Mich interessiert die Message >{s}< ebenso");
// button.onClick = MeinClickHandler; -- Bei Events ist = verboten
// Aufruf der Delegate-Variablen
button.FireClick("Klick");
//button.onClick("Hallo Welt");
}
}
}
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