diff --git a/OOP2024_AMP.sln b/OOP2024_AMP.sln index f3ae1abd30d4390a6104af0a6b86c45b9deafcc6..5e3c16eaa3aae3f6bf7beb5517f3581778a96b4e 100644 --- a/OOP2024_AMP.sln +++ b/OOP2024_AMP.sln @@ -39,11 +39,15 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "P16 GenericKeyValueList", " EndProject 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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "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}" +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 Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -135,6 +139,14 @@ Global {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 + {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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/P18 Delegate Uebg/Program.cs b/P18 Delegate Uebg/Program.cs index 509b06c59b77b691634544f18eb84b59bbba2f70..907d0b35d5fe3d7477697eb0afc41f4d8cb3932a 100644 --- a/P18 Delegate Uebg/Program.cs +++ b/P18 Delegate Uebg/Program.cs @@ -21,7 +21,24 @@ delegate T ChangeFn<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++) { @@ -29,19 +46,20 @@ } return array; } - //static T[] ForAll<T>(ChangeFn<T> fn, params T[] array) - //{ - - //} - static T[] Select<T>(FilterFn<T> fn, T[] array) + static T[] Select<T>(Predicate<T> test, T[] array) { 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(); } 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")); } } } diff --git a/P20 Liste mit Delegate/Program.cs b/P20 Liste mit Delegate/Program.cs index 6c088a0a4e7dcc8c40de421e673a52b0cf61eb35..2235859906c19a55c78a95ea5d62b6fecdb6e307 100644 --- a/P20 Liste mit Delegate/Program.cs +++ b/P20 Liste mit Delegate/Program.cs @@ -23,11 +23,10 @@ namespace P20_Liste_mit_Delegate { last = last.next = item; } - } } - public delegate bool meinTest<T>(T s); - internal IEnumerable<T> AllPers(meinTest<T> test) + //public delegate bool Predicate<U>(U s); + public IEnumerable<T> GetData(Predicate<T> test) { for (LItem item = first; item != null; item = item.next) { @@ -35,6 +34,11 @@ namespace P20_Liste_mit_Delegate yield return item.data; } } + public IEnumerator<T> GetEnumerator() + { + for (LItem item = first; item != null; item = item.next) + yield return item.data; + } } internal class Program { @@ -43,18 +47,22 @@ namespace P20_Liste_mit_Delegate 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')) + //foreach (var item in pers.GetData(s => s.Contains("er"))) + foreach (var item in pers.GetData(LetztesZeichenN)) { 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)) + foreach (var item in il.GetData(x => x%2==0)) { Console.WriteLine(item); } } + static bool LetztesZeichenN(string s) + { + return s[^1] == 'n'; + } } } diff --git a/P21 Ref/P21 Ref.csproj b/P21 Ref/P21 Ref.csproj new file mode 100644 index 0000000000000000000000000000000000000000..d7f7f81b05299930d750dd586f690ac60ed0e34f --- /dev/null +++ b/P21 Ref/P21 Ref.csproj @@ -0,0 +1,11 @@ +<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> diff --git a/P21 Ref/Program.cs b/P21 Ref/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..5dc6584f24bd37a40237d66d5669d8e8b77850e8 --- /dev/null +++ b/P21 Ref/Program.cs @@ -0,0 +1,40 @@ +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]); + } + } + } +} diff --git a/P22 DelVariableButton/P22 DelVariableButton.csproj b/P22 DelVariableButton/P22 DelVariableButton.csproj new file mode 100644 index 0000000000000000000000000000000000000000..ed7d2319f1a7cde7dde3f2d25174849cc3a70b7b --- /dev/null +++ b/P22 DelVariableButton/P22 DelVariableButton.csproj @@ -0,0 +1,11 @@ +<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> diff --git a/P22 DelVariableButton/Program.cs b/P22 DelVariableButton/Program.cs new file mode 100644 index 0000000000000000000000000000000000000000..ae9fbcf13c5398b011e2895eecf932ebba001367 --- /dev/null +++ b/P22 DelVariableButton/Program.cs @@ -0,0 +1,42 @@ +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"); + } + } +}