erweiterung
This commit is contained in:
10
BeispielGenerischeTypen/BeispielGenerischeTypen.csproj
Normal file
10
BeispielGenerischeTypen/BeispielGenerischeTypen.csproj
Normal file
@@ -0,0 +1,10 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
14
BeispielGenerischeTypen/IKarten.cs
Normal file
14
BeispielGenerischeTypen/IKarten.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BeispielGenerischeTypen
|
||||
{
|
||||
public interface IKarte
|
||||
{
|
||||
string Farbe { get; }
|
||||
string Wert { get; }
|
||||
}
|
||||
}
|
||||
22
BeispielGenerischeTypen/Karte.cs
Normal file
22
BeispielGenerischeTypen/Karte.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BeispielGenerischeTypen
|
||||
{
|
||||
|
||||
public class Karte : IKarte
|
||||
{
|
||||
public string Farbe { get; }
|
||||
public string Wert { get; }
|
||||
|
||||
public Karte(string farbe, string wert)
|
||||
{
|
||||
Farbe = farbe;
|
||||
Wert = wert;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
17
BeispielGenerischeTypen/Program.cs
Normal file
17
BeispielGenerischeTypen/Program.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace BeispielGenerischeTypen
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Stapel<Karte> stapel = new Stapel<Karte>();
|
||||
Karte karte1 = new Karte("Herz", "König");
|
||||
Karte karte2 = new Karte("Pik", "Ass");
|
||||
stapel.Push(karte1);
|
||||
stapel.Push(karte2);
|
||||
Console.WriteLine(stapel.Pop().Wert);
|
||||
Console.WriteLine(stapel.Pop().Wert);
|
||||
Console.WriteLine(stapel.IsEmpty());
|
||||
}
|
||||
}
|
||||
}
|
||||
43
BeispielGenerischeTypen/Stapel.cs
Normal file
43
BeispielGenerischeTypen/Stapel.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BeispielGenerischeTypen
|
||||
{
|
||||
|
||||
public class Stapel<T> where T : IKarte
|
||||
{
|
||||
private List<T> karten;
|
||||
|
||||
public Stapel()
|
||||
{
|
||||
karten = new List<T>();
|
||||
}
|
||||
|
||||
public void Push(T karte)
|
||||
{
|
||||
karten.Add(karte);
|
||||
}
|
||||
|
||||
public T Pop()
|
||||
{
|
||||
if (karten.Count == 0)
|
||||
{
|
||||
throw new InvalidOperationException("Der Stapel ist leer.");
|
||||
}
|
||||
|
||||
int lastIndex = karten.Count - 1;
|
||||
T karte = karten[lastIndex];
|
||||
karten.RemoveAt(lastIndex);
|
||||
return karte;
|
||||
}
|
||||
|
||||
public bool IsEmpty()
|
||||
{
|
||||
return karten.Count == 0;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user