gpt4 book ai didi

c# - 如何在C#中访问struct内部的函数

转载 作者:太空狗 更新时间:2023-10-30 00:39:29 24 4
gpt4 key购买 nike

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();

}
}

最佳答案

  1. 你的方法 getdetails 不应该是静态的
  2. getdetails 中删除 Ele.
  3. 初始化类(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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com