作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
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();
}
}
最佳答案
getdetails
不应该是静态的getdetails
中删除 Ele.
类(class)元素
变量您的代码:
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
来避免问题。
关于c# - 如何在C#中访问struct内部的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35169957/
我是一名优秀的程序员,十分优秀!