class Program
{
public struct course
{
public string name;
public int elecode;
public int credit;
public static void getdetails()
{
Console.WriteLine("Enter your Name");
Ele.name = Console.ReadLine();
}
}
static void Main(string[] args)
{
course ele;
ele.getdetails();
}
}
Run Code Online (Sandbox Code Playgroud)
getdetails不应该是静态的Ele.里面getdetailscourse ele变量您的代码:
class Program
{
public struct course
{
public string name;
public void getdetails()
{
Console.WriteLine("Enter your Name");
name = Console.ReadLine();
}
}
static void Main(string[] args)
{
course ele = new course();
ele.getdetails();
}
}
Run Code Online (Sandbox Code Playgroud)
正如@DavidHeffernan 在关于糟糕设计的评论中提到的那样,当值类型为您提供值的 COPY时,您必须知道在哪里使用class而不是struct逃避问题