Statische Main Methode fehlt (C#)?
Moin,
Ich bin grad dran ein Programm in C# zu erstellen das als Konsolen Anwendung eine Sound Datei abspielt.
Ich kriege aber als Compiling Fehler Das Programm enthält keine als Einstiegspunkt geeignete statische Main-Methode.
Wie könnte ich das beheben? Code ist unten drin
using System;
using System.Media;
namespace When_the_Imposter_is_SUS
{
class Program
{
private SoundPlayer player;
private void InitializeSound()
{
player = new SoundPlayer();
player.SoundLocation = "Sound.wav";
player.Play();
}
}
}
3 Antworten
Dein C#-Programm benötigt, wie es die Fehlermeldung schon sagt, eine Main-Methode. Sie dient als Einstiegspunkt. Wenn das Programm gestartet wird, startet der Programmfluss in ihr.
Du könntest deine Klasse folgendermaßen umschreiben:
// usings ...
namespace When_the_Imposter_is_SUS
{
class Program
{
private static SoundPlayer player;
public static void Main()
{
player = new SoundPlayer();
player.SoundLocation = "Sound.wav";
player.Play();
}
}
}
Oder du rufst deine Methode in einer Main-Methode auf.
// usings ...
namespace When_the_Imposter_is_SUS
{
class Program
{
private SoundPlayer player;
public static void Main()
{
var program = new Program();
program.InitializeSound();
}
private void InitializeSound()
{
player = new SoundPlayer();
player.SoundLocation = "Sound.wav";
player.Play();
}
}
}
Nunja ohne Main geht's nicht..
Da hat wohl jemand nicht gelernt 🤭
Schnell etwas Nachhilfe: https://www.w3schools.com/cs/trycs.php?filename=demo_helloworld
Indem du eine Methode hinzufügst, die diesem Gerüst entspricht:
static void Main(string[] args){
//Code...
}