一类中的一个结构域的防护等级 [英] Protection level of a struct field within a class

查看:162
本文介绍了一类中的一个结构域的防护等级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是相当新的C#编程所以这可​​能是一个初学者的问题。

I am fairly new to C# programming so this is likely a beginner's question.

我得到的'A.Test.That.Fails'是人迹罕至,由于其保护级别的上下面的代码错误,我不明白其中的道理。

I get a "'A.Test.That.Fails' is inaccessible due to its protection level" error on the following code snippet and I fail to understand the reason.

namespace A
{
    class Test
    {
        public void Demo()
        {
            That[] it = new That[42];
            it[0].fails = 21;
        }

        public struct That
        {
            int fails;
        }
    }
}



从C ++编程,并具有即将读取保护规则几乎是完全一样的,因为有一个单独的类我希望它的工作,即使结构和演示<两种/ code>方法是私人

由于侧面说明,一些页面的链接总结向C ++程序员范围和保护规则将不胜感激。

As the side note, a link to some page summing up scope and protection rules toward the C++ programmer would be greatly appreciated.

推荐答案

已经给其他的答案有你的答案,所以我赢了'T在这里打一个死马。需要声明,以便能够从外部代码访问该字段公共

The other answers given already have your answer, so I won't beat a dead horse here. You need to declare the field public in order to be able to access it from external code.

在C ++中,结构和类是等效的,唯一的区别是它们各自的成员的缺省访问级别

In C++, structs and classes are equivalent, with the only difference being the default access level of their respective members.

然而,这不是在C#的情况。通常情况下,你应该只使用一个结构为是不可变的(不会改变)小,短期对象。的结构具有的值类型的语义,其中作为一类具有的引用类型的语义。你理解值类型和引用类型之间的区别,如果你正在学习C#编程是非常重要的。乔恩斯基特出版试图提供解释的文章。但是,你肯定会想拿起一个很好的入门书,C#,在更详细的对待这些问题。

However, that's not the case in C#. Generally, you should only use a struct for small, short-lived objects that are immutable (won't change). A structure has value type semantics, where as a class has reference type semantics. It's very important that you understand the difference between value types and reference types if you're learning to program in C#. Jon Skeet has published an article that attempts to provide that explanation. But you'll definitely want to pick up a good introductory book to C# that treats these issues in more detail.

通常情况下,你要使用类在C#,而不是一个结构。而当你使用这个类,注意微软的C#设计指南倾向于推荐的的公开公共字段。相反,他们建议您使用公共财产,由私人领域的支持。该准则背后的基本原理的更深入的阐述,这里给出 。例如:

More often than not, you'll want to use a class in C#, rather than a structure. And when you use that class, note that Microsoft's design guidelines for C# tend to recommend against exposing public fields. Instead, they recommend that you use a public property, backed by a private field. A more thorough exposition of the rationale behind that guideline is given here. For example:

class TimePeriod
{
    private double seconds;

    public double Seconds
    {
        get { return seconds; }
        set { seconds = value; }
    }
}



或者你可以使用简单的自动属性语法,其中有编译器自动生成私人支持字段:

Or you can use the simpler "automatic properties" syntax, which has the compiler automatically generate that private backing field:

class TimePeriod
{
    public double Seconds { get; set; }
}

这篇关于一类中的一个结构域的防护等级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆