C# 为什么可以';您不能像访问C+中的结构一样访问C中的类的一部分+;? 名称空间SnakesAndLadders { 类节点 { int snakeHead;//指向玩家下潜到的另一个节点 int ladderFoot;//指向另一个节点,玩家可以到达该节点 } 班级计划 { 节点[]游戏板=新节点[100]; void loadStructure() { //首先,将所有蛇头和梯形脚设置为零 对于(int i=0;i

C# 为什么可以';您不能像访问C+中的结构一样访问C中的类的一部分+;? 名称空间SnakesAndLadders { 类节点 { int snakeHead;//指向玩家下潜到的另一个节点 int ladderFoot;//指向另一个节点,玩家可以到达该节点 } 班级计划 { 节点[]游戏板=新节点[100]; void loadStructure() { //首先,将所有蛇头和梯形脚设置为零 对于(int i=0;i,c#,C#,在C#中,这将不起作用。对于gameBoard[i],intellisense不显示它有蛇头组件。在C#中,类的字段默认为私有字段 namespace SnakesAndLadders { class Node { int snakeHead ; // points to another node where the player goes down to int ladderFoot; // points to another node

在C#中,这将不起作用。对于gameBoard[i],intellisense不显示它有蛇头组件。

在C#中,类的字段默认为私有字段

namespace SnakesAndLadders
{

    class Node
    {
        int snakeHead ;   // points to another node where the player goes down to 
        int ladderFoot;  // points to another node where to player goes up to
    }

    class Program
    {
        Node[] gameBoard = new Node[100];

        void loadStructure()
        {
            // first, set all the snakeheads and ladderFoots to zero
            for (int i =0; i < 100; i++)
            {
                gameBoard[i].snakeHead = 0;
                gameBoard[i].ladderFoot = 0;
            }
        }
        static void Main(string[] args)
        {
        }
    }
只要将字段公开就可以解决问题。 编辑:使用最佳做法,您可以将字段保持为私有,但可以创建属性并使用它们处理数据。使用下划线命名私有也是一种常见做法:

class Node
{
    public int snakeHead ;   // points to another node where the player goes down to 
    public int ladderFoot;  // points to another node where to player goes up to
}

默认的访问修饰符是
private
。创建属性以访问您的私有字段

class Node
{
    private int _snakeHead ;   // points to another node where the player goes down to 
    public int SnakeHead
    {
        get {return _snakeHead;}
        set {_snakeHead = value;}
    }

    private int _ladderFoot;  // points to another node where to player goes up to
    public int LadderFoot
    {
        get {return _ladderFoot;}
        set {_ladderFoot = value;}
    }
}
然后你可以
gameBoard[i].SnakeHead=0;

您甚至可以这样定义属性:

class Node
{
    int snakeHead ;   // points to another node where the player goes down to 
    int ladderFoot;  // points to another node where to player goes up to

    public int SnakeHead
    {
        get { return snakeHead;}
        set { snakeHead = value;}
    }

    public int LadderFoot
    {
        get { return ladderFoot; }
        set { ladderFoot = value;}
    }
}

在这种情况下,您甚至不需要私有字段。

回答您的问题:
在C++的结构域中,默认为

namespace SnakesAndLadders
{

    class Node
    {
        int snakeHead ;   // points to another node where the player goes down to 
        int ladderFoot;  // points to another node where to player goes up to
    }

    class Program
    {
        Node[] gameBoard = new Node[100];

        void loadStructure()
        {
            // first, set all the snakeheads and ladderFoots to zero
            for (int i =0; i < 100; i++)
            {
                gameBoard[i].snakeHead = 0;
                gameBoard[i].ladderFoot = 0;
            }
        }
        static void Main(string[] args)
        {
        }
    }
在C#中,默认情况下,struct的字段是
private

namespace SnakesAndLadders
{

    class Node
    {
        int snakeHead ;   // points to another node where the player goes down to 
        int ladderFoot;  // points to another node where to player goes up to
    }

    class Program
    {
        Node[] gameBoard = new Node[100];

        void loadStructure()
        {
            // first, set all the snakeheads and ladderFoots to zero
            for (int i =0; i < 100; i++)
            {
                gameBoard[i].snakeHead = 0;
                gameBoard[i].ladderFoot = 0;
            }
        }
        static void Main(string[] args)
        {
        }
    }
您声明的字段没有访问修饰符关键字(
private.
public`),因此它们具有上面提到的默认可访问性

要在C#中获得相同的行为,需要显式声明访问修饰符

public int SnakeHead{get; set;}

默认情况下,C++中的类字段默认为<代码>私下<代码>。E不是

private
,而是
internal
。您所写的内容适用于嵌套类。在c#class成员中,默认访问修饰符是private,但类默认访问修饰符是internal而非private,这是正确的。顺便说一句,使用公共字段是一种破坏封装的糟糕做法,在这些s中应该优先使用属性ituations.thank you@SomeUser