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();
}
}
答案 0 :(得分:4)
getdetails
不应该是静态的Ele.
getdetails
course 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();
}
}
正如@DavidHeffernan在评论设计不佳时所提到的那样,当a value type gives you a COPY of the value
时,您必须知道在class
而不是struct
使用的地方来逃避问题
答案 1 :(得分:3)
您无法使用实例调用静态方法。你有静态方法所以用struct调用它而不是用struct的实例调用它。
course.getdetails();
即使没有实例,静态成员也可以在类上调用 已经创建了类。静态成员总是被访问 类名,而不是实例名称
在此MSDN文章Static Classes and Static Class Members (C# Programming Guide)上阅读有关静态的更多信息。