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

2021-06-08 Erweiterungsmethoden, LINQ-Intro

parent 3b5c45a2
Branches
No related tags found
No related merge requests found
Showing
with 481 additions and 3 deletions
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_11_3_Erweiterungsmethoden</RootNamespace>
</PropertyGroup>
</Project>
using System;
using System.Linq;
namespace _11_3_Erweiterungsmethoden
{
public static class Erweiterung
{
public static int Abs(this int i)
=> (i > 0) ? i : -i;
public static T[] Sortieren<T> (this T[] feld) where T:IComparable<T>
{
// ... Hier Ihre Sort-Methode
Array.Sort(feld);
return feld;
}
}
class Program
{
static void Main(string[] args)
{
int ix = -25;
//Console.WriteLine(ix.Abs());
int[] ints = { 10, 45, 15, 38, 21, 26 };
var res = ints.Sortieren();
foreach (var item in res)
{
Console.WriteLine(item);
}
Console.WriteLine("---------");
var res2 = ints.OrderBy(g=>-g);
foreach (var item in res2)
{
Console.WriteLine(item);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_11_4_LINQ_Intro</RootNamespace>
</PropertyGroup>
</Project>
using System;
using System.Linq;
namespace _11_4_LINQ_Intro
{
class AnonymeVariableImProperty
{
public int MeinProperty { get; set; }
}
class AnonymeTypen
{
public static void AnonymousTypes()
{
#region var
var x = "123";
//x = 123;
object o = 123;
o = "123";
#endregion
var r1 = new { Name = "Wienkop", Tel = 1614, Adr = "Hohfederstraße 40" };
var r2 = new { Name = "Wienkop", Tel = 1614};
Console.WriteLine(r1);
Console.WriteLine(r2.Name);
// r1.Name = r2.Name; Nicht zulässig, die Properties sind read-only!
var r3 = new { Name = r1.Name, AdrLaenge = r1.Adr.Length };
Console.WriteLine(r3);
}
}
class Program
{
static void Main(string[] args)
{
// AnonymeTypen.AnonymousTypes();
string[] wörter = {
"Anton", "Berta", "Charles", "Dieter", "Emma", "Franz", "Gustav", "Heiner", "Ida",
"Johanna", "Klaus", "Ludwig", "Manni", "Norbert", "Otto", "Paul", "Quasimodo", "Rainer",
"Stefan", "Thorsten", "Uwe", "Victoria", "Willi", "Xaver", "Yvonne", "Zacharias"
};
//var r1 = from w in wörter
// where w.Length < 6
// orderby w descending
// select new { OrgName = w, GrossName = w.ToUpper(), Länge = w.Length };
//foreach (var item in r1)
//{
// Console.WriteLine(item);
//}
var r2 = from w in wörter
where w.Length < 7
group w by w.Length;
foreach (var group in r2)
{
Console.WriteLine($"Länge: {group.Key}");
foreach (var item in group)
{
Console.WriteLine(item);
}
}
}
}
}
# Rules in this file were initially inferred by Visual Studio IntelliCode from the C:\Users\wienkop\source\repos\prog2-ss2021-wienkop\11-5 BinTreePersVerwaltung\ codebase based on best match to current usage at 08.06.2021
# You can modify the rules from these initially generated values to suit your own policies
# You can learn more about editorconfig here: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference
[*.cs]
#Core editorconfig formatting - indentation
#use soft tabs (spaces) for indentation
indent_style = space
#Formatting - spacing options
#do not place space characters after the opening parenthesis and before the closing parenthesis of a method call
csharp_space_between_method_call_parameter_list_parentheses = false
#place a space character after the opening parenthesis and before the closing parenthesis of a method declaration parameter list.
csharp_space_between_method_declaration_parameter_list_parentheses = false
#Style - expression bodied member options
#prefer block bodies for methods
csharp_style_expression_bodied_methods = false:suggestion
#Style - language keyword and framework type options
#prefer the language keyword for local variables, method parameters, and class members, instead of the type name, for types that have a keyword to represent them
dotnet_style_predefined_type_for_locals_parameters_members = true:suggestion
#Style - modifier options
#do not prefer accessibility modifiers to be specified
dotnet_style_require_accessibility_modifiers = never:suggestion
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_11_5_BinTreePersVerwaltung</RootNamespace>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Text;
namespace _11_5_BinTreePersVerwaltung
{
abstract class Personal
{
public string Name { get; set; }
public Personal (string Name) { this.Name = Name; }
abstract public double Kosten { get; }
}
class Mitarbeiter : Personal
{
protected double gehalt;
public Mitarbeiter(string Name, double Gehalt) : base(Name) { gehalt = Gehalt; }
public override double Kosten { get => 12.65 * gehalt; }
}
class Führungskraft : Personal
{
protected double gehalt;
protected double erfolgsbeteiligung;
public Führungskraft (string Name, double Gehalt, double Erfolgsbeteiligung) : base (Name)
{
gehalt = Gehalt;
erfolgsbeteiligung = Erfolgsbeteiligung;
}
public override double Kosten { get => 12.65 * gehalt + erfolgsbeteiligung; }
}
class Werkvertragler : Personal
{
protected double betrag;
public Werkvertragler(string Name, double Betrag) : base (Name)
{
betrag = Betrag;
}
public override double Kosten { get => betrag; }
}
}
using System;
namespace _11_5_BinTreePersVerwaltung
{
class Program
{
static void Main(string[] args)
{
Personal[] persFeld = new Personal[10];
persFeld[0] = new Mitarbeiter("Xaver", 3000);
foreach (var item in persFeld)
{
Console.WriteLine(item.Kosten);
}
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>_11_Ubg_Mailverteiler_Di</RootNamespace>
</PropertyGroup>
</Project>
using System;
using System.Collections.Generic;
using System.Text;
namespace _11_Ubg_Mailverteiler_Di
{
// Legen Sie ein Delegate void Callback(string s) an
// Erstellen Sie eine Klasse MessageBox mit
// - Einem Event Verteiler
// - Einer Methode Send(string s), welche die verteiler Variable aufruft
// Experimentieren Sie ...
// 1) Legen Sie zwei Dummy-Klassen ClassA und ClassB an. Beide Klassen sollen öffentliche Methoden
// (statisch/nicht-statisch) besitzen, die der Callback-Signatur entsprechen
// 2) Definieren Sie auch in Main noch eine statische Methode, die der Callback-Signatur entspricht
// 3) Legen Sie eine Instanz der MessageBox an und registrieren Sie die Methoden in der Event-Variablen
// 4) Rufen Sie Send() mit einem Text auf
// 5) Entfernen Sie eine der registrierten Methoden und geben Sie noch einen Text aus
class MessageBox
{
public delegate void Callback(string s);
public delegate void TransformDel(ref string s);
public event Callback Verteiler;
public event TransformDel Transformationen;
public void Send(string s)
{
Transformationen?.Invoke(ref s);
Verteiler?.Invoke(s);
}
}
}
using System;
namespace _11_Ubg_Mailverteiler_Di
{
class DummyA
{
public static void ToUpperAusgabe(string s)
{
Console.WriteLine(s.ToUpper());
}
public static void ToUpper(ref string s)
{
s = s.ToUpper();
}
}
class DummyB
{
public int x = 0;
public void Bereich(string s)
{
Console.WriteLine($"x: {x} --{s.Substring(6)}");
}
}
class Program
{
static void Ausgabe(string s)
{
Console.WriteLine(s);
}
static void Main(string[] args)
{
MessageBox mb = new MessageBox();
DummyB [] db = new DummyB[10];
db[1] = new DummyB();
db[1].x = 99;
mb.Verteiler += Program.Ausgabe;
mb.Verteiler += DummyA.ToUpperAusgabe;
mb.Verteiler += db[1].Bereich;
mb.Transformationen += DummyA.ToUpper;
mb.Send("Hallo Welt");
//mb.Verteiler("Hallo");
}
}
}
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
</Project>
using System;
namespace SS2020Test
{
delegate void myDelegate(bool x);
//delegate int myLogic(int x, int y);
abstract class LogicElement
{
bool[] inputs;
bool out1;
string me;
public LogicElement(int count, string me)
{
inputs = new bool[count];
this.me = me;
Update();
}
public event myDelegate Output;
public abstract bool Logic(bool x, bool y);
private void Input(int idx, bool val)
{
Console.Write($"<{me} = {val} | {out1} ");
inputs[idx] = val;
Update();
Console.WriteLine($"--> {out1}>");
Output?.Invoke(out1);
}
private void Update()
{
out1 = (inputs.Length == 2) ?
Logic(inputs[0], inputs[1]) : Logic(inputs[0], inputs[0]);
}
public void Input1(bool in1) { Input(0, in1); }
public void Input2(bool in2) { Input(1, in2); }
public override string ToString() => $"[{me}]: {out1}";
}
class AndGate : LogicElement
{
public AndGate(string me) : base(2, me) { }
public override bool Logic(bool in1, bool in2) => in1 && in2 ? true : false;
}
class OrGate : LogicElement
{
public OrGate(string me) : base(2, me) { }
public override bool Logic(bool in1, bool in2) => in1 == false && in2 == false ? false : true;
}
class NotGate : LogicElement
{
public NotGate(string me) : base(1, me) { }
public override bool Logic(bool in1, bool in2) => !in1;
}
class Port : LogicElement
{
public Port(string me) : base(1, me) { }
public bool Input { set { Input1(value); } }
public override bool Logic(bool in1, bool in2) => in1;
}
class HalfAdder
{
public Port portX = new Port("x");
public Port portY = new Port("y");
public AndGate carry = new AndGate("cy ");
public OrGate sum = new OrGate("sum");
string me;
NotGate notX = new NotGate("nx");
NotGate notY = new NotGate("ny");
AndGate a1 = new AndGate("a1");
AndGate a2 = new AndGate("a2");
public HalfAdder(string me)
{
this.me = me;
portX.Output += notX.Input1;
portX.Output += a2.Input1;
portX.Output += carry.Input1;
portY.Output += notY.Input1;
portY.Output += a1.Input2;
portY.Output += carry.Input2;
notX.Output += a1.Input1;
notY.Output += a2.Input2;
a1.Output += sum.Input1;
a2.Output += sum.Input2;
}
}
class FullAdder
{
public Port portA = new Port("a");
public Port portB = new Port("b");
public Port portC = new Port("cIn");
private HalfAdder FAsum = new HalfAdder("sum");
private HalfAdder ha1 = new HalfAdder("HA");
private OrGate carry = new OrGate("cy ");
private string me;
public FullAdder(string me)
{
this.me = me;
portA.Output += ha1.portX.Input1;
portB.Output += ha1.portY.Input1;
portC.Output += FAsum.portX.Input1;
ha1.carry.Output += carry.Input2;
ha1.sum.Output += FAsum.portY.Input1;
portC.Output += FAsum.portX.Input1;
FAsum.carry.Output += carry.Input1;
}
class Program
{
static void Main(string[] args)
{
FullAdder fa = new FullAdder("FA");
fa.portA.Input = false;
fa.portB.Input = true;
fa.portC.Input = false;
Console.WriteLine($"\n{ fa.FAsum.sum,12} | {fa.carry}");
fa.portC.Input = true;
Console.WriteLine($"\n{ fa.FAsum.sum,12} | {fa.carry}");
fa.portA.Input = true;
Console.WriteLine($"\n{ fa.FAsum.sum,12} | {fa.carry}");
}
}
}
}
......@@ -85,11 +85,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-Ubg HashDict-Di", "10-Ub
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "10-3 WpfButton", "10-3 WpfButton\10-3 WpfButton.csproj", "{11636803-CB01-4A29-ADAE-F03F3F38ED70}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-Ubg Mailverteiler-Mo", "11-Ubg Mailverteiler-Mo\11-Ubg Mailverteiler-Mo.csproj", "{C36B7276-D500-4147-853C-9B90418514B1}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "11-Ubg Mailverteiler-Mo", "11-Ubg Mailverteiler-Mo\11-Ubg Mailverteiler-Mo.csproj", "{C36B7276-D500-4147-853C-9B90418514B1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-1 DataDriven", "11-1 DataDriven\11-1 DataDriven.csproj", "{0799E33E-C645-4CBD-BD98-623E290E947C}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "11-1 DataDriven", "11-1 DataDriven\11-1 DataDriven.csproj", "{0799E33E-C645-4CBD-BD98-623E290E947C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-2 Delegate Verkettung", "11-2 Delegate Verkettung\11-2 Delegate Verkettung.csproj", "{0FC3EC66-BD2E-46C4-8512-FB1DFD6A62F8}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "11-2 Delegate Verkettung", "11-2 Delegate Verkettung\11-2 Delegate Verkettung.csproj", "{0FC3EC66-BD2E-46C4-8512-FB1DFD6A62F8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-Ubg Mailverteiler-Di", "11-Ubg Mailverteiler-Di\11-Ubg Mailverteiler-Di.csproj", "{A602EA3B-7F14-4D94-B519-D3BD870CA1DA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-3 Erweiterungsmethoden", "11-3 Erweiterungsmethoden\11-3 Erweiterungsmethoden.csproj", "{64AE6304-67E0-4413-8D66-D12B3C133D09}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-4 LINQ-Intro", "11-4 LINQ-Intro\11-4 LINQ-Intro.csproj", "{7B612E44-F4A4-4857-BCAE-6DA1045A4FDE}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "11-5 BinTreePersVerwaltung", "11-5 BinTreePersVerwaltung\11-5 BinTreePersVerwaltung.csproj", "{FEC9D71A-A142-4EB5-920F-170DAD90F352}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
......@@ -273,6 +281,22 @@ Global
{0FC3EC66-BD2E-46C4-8512-FB1DFD6A62F8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FC3EC66-BD2E-46C4-8512-FB1DFD6A62F8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FC3EC66-BD2E-46C4-8512-FB1DFD6A62F8}.Release|Any CPU.Build.0 = Release|Any CPU
{A602EA3B-7F14-4D94-B519-D3BD870CA1DA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A602EA3B-7F14-4D94-B519-D3BD870CA1DA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A602EA3B-7F14-4D94-B519-D3BD870CA1DA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A602EA3B-7F14-4D94-B519-D3BD870CA1DA}.Release|Any CPU.Build.0 = Release|Any CPU
{64AE6304-67E0-4413-8D66-D12B3C133D09}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{64AE6304-67E0-4413-8D66-D12B3C133D09}.Debug|Any CPU.Build.0 = Debug|Any CPU
{64AE6304-67E0-4413-8D66-D12B3C133D09}.Release|Any CPU.ActiveCfg = Release|Any CPU
{64AE6304-67E0-4413-8D66-D12B3C133D09}.Release|Any CPU.Build.0 = Release|Any CPU
{7B612E44-F4A4-4857-BCAE-6DA1045A4FDE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7B612E44-F4A4-4857-BCAE-6DA1045A4FDE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7B612E44-F4A4-4857-BCAE-6DA1045A4FDE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7B612E44-F4A4-4857-BCAE-6DA1045A4FDE}.Release|Any CPU.Build.0 = Release|Any CPU
{FEC9D71A-A142-4EB5-920F-170DAD90F352}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FEC9D71A-A142-4EB5-920F-170DAD90F352}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FEC9D71A-A142-4EB5-920F-170DAD90F352}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FEC9D71A-A142-4EB5-920F-170DAD90F352}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment