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

Events, Delegate-Verkettung

parent 75bfe8cc
No related branches found
No related tags found
No related merge requests found
Showing
with 120 additions and 8 deletions
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -46,6 +46,28 @@ namespace _21_GenListeMitDelegates
current = current.Next;
}
}
public IEnumerator<T> GetEnumerator()
{
LinkedListNode<T> current = head;
while (current != null)
{
yield return current.Value;
current = current.Next;
}
}
public delegate T Func(T x);
public void ForAll(Func f)
{
LinkedListNode<T> current = head;
while (current != null)
{
Console.WriteLine(current.Value);
current.Value = f(current.Value);
current = current.Next;
}
}
public delegate bool MyTest(T x);
public void PrintFilter(MyTest testFkt)
{
......@@ -104,6 +126,8 @@ namespace _21_GenListeMitDelegates
list.PrintFilter(IstUngerade);
list.PrintFilter(x => x % 2 != 0);
list.PrintFilter(x =>true);
list.ForAll(x => x + 1);
list.ForAll(delegate (int x) { return x + 2; });
LinkedList<string> sl = new LinkedList<string>();
sl.Add("Anton");
......@@ -126,6 +150,11 @@ namespace _21_GenListeMitDelegates
{
Console.WriteLine(item.Name);
}
foreach (var item in pl)
{
Console.WriteLine(item.Name); ;
}
}
}
}
......@@ -2,10 +2,18 @@
namespace _22_Informationsverteiler
{
delegate void Message(string txt);
class MessageBox
{
public Message callback=null;
public delegate void Message(string txt);
public event Message callback=null;
// event-Delegates schränken die Verwendungsmöglichkeiten des
// Delegates auf += und -= für Methoden außerhalb dieser Klasse ein!
public void Send(string txt)
{
// z.B. Validierung des Inhalts von "txt"
callback?.Invoke(">>> " + txt); // if (callback != null) callback(...);
}
}
class Person
......@@ -23,17 +31,22 @@ namespace _22_Informationsverteiler
Person p1 = new Person("Anton");
Person p2 = new Person("Berta");
mb.Send("Noch keine Methode registriert");
mb.callback += Console.WriteLine;
mb.callback += p1.MyPrint;
mb.callback += p2.MyPrint;
mb.callback("Hallo");
mb.Send("Hallo");
Console.WriteLine("------------------");
mb.callback += Console.WriteLine;
mb.callback("Hallo zwei");
// ConsoleWriteLine("Hallo");
// p1.MyPrint("Hallo");
// p2.MyPrint("Hallo");
mb.callback -= Console.WriteLine;
mb.Send("Hallo zwei");
// mb.callback("Hallo")
// --> Console.WriteLine("Hallo");
// --> p1.MyPrint("Hallo");
// --> p2.MyPrint("Hallo");
}
}
......
No preview for this file type
No preview for this file type
......@@ -11,3 +11,4 @@ C:\Users\wienkop\source\repos\oop-ss2023-wienkop\OOP2023\22 Informationsverteile
C:\Users\wienkop\source\repos\oop-ss2023-wienkop\OOP2023\22 Informationsverteiler\obj\Debug\netcoreapp3.1\22 Informationsverteiler.dll
C:\Users\wienkop\source\repos\oop-ss2023-wienkop\OOP2023\22 Informationsverteiler\obj\Debug\netcoreapp3.1\22 Informationsverteiler.pdb
C:\Users\wienkop\source\repos\oop-ss2023-wienkop\OOP2023\22 Informationsverteiler\obj\Debug\netcoreapp3.1\22 Informationsverteiler.genruntimeconfig.cache
C:\Users\wienkop\source\repos\oop-ss2023-wienkop\OOP2023\22 Informationsverteiler\obj\Debug\netcoreapp3.1\22 Informationsverteiler.csproj.AssemblyReference.cache
No preview for this file type
No preview for this file type
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_23_Button</RootNamespace>
</PropertyGroup>
</Project>
using System;
namespace _23_Button
{
// Firma Winzelweich
delegate void Action();
class Button
{
public event Action OnClick;
public void Eventloop()
{
OnClick?.Invoke();
}
}
// Ich-AG
class Program
{
static void ButtonOnClick() { Console.WriteLine("Button wurde angeklickt"); }
static void Main(string[] args)
{
Button btn1 = new Button();
btn1.OnClick += ButtonOnClick;
btn1.Eventloop();
}
}
}
{
"runtimeTarget": {
"name": ".NETCoreApp,Version=v3.1",
"signature": ""
},
"compilationOptions": {},
"targets": {
".NETCoreApp,Version=v3.1": {
"23 Button/1.0.0": {
"runtime": {
"23 Button.dll": {}
}
}
}
},
"libraries": {
"23 Button/1.0.0": {
"type": "project",
"serviceable": false,
"sha512": ""
}
}
}
\ No newline at end of file
File added
File added
File added
{
"runtimeOptions": {
"additionalProbingPaths": [
"C:\\Users\\wienkop\\.dotnet\\store\\|arch|\\|tfm|",
"C:\\Users\\wienkop\\.nuget\\packages"
]
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment