带有struct和private const的静态类 [英] Static class with struct and private const

查看:60
本文介绍了带有struct和private const的静态类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么结构材质中的常量必须是公共的?

如果我将它们设为私有,则无法与其他类一起使用。

Material1 is私有结构中的public并且在方法GetMaterialWeight中可用,它看起来很奇怪。

谢谢,

Robert



 命名空间汽车
{
公共 静态 class 助手
{
public static int GetMaterialWeight()
{
return Material.Material1;
// 返回Material.Material2; - >这是不可能的(为什么?)

}
}

private < span class =code-keyword> struct 材料
{
public const int Material1 = 10 ;
private const int Material2 = 20 ;
}
}

解决方案

私有意味着它只能访问包含类型。由于Material2包含在Material类型中,如果您将其设为私有,则只能通过 inside 材质结构访问它。



这个如果你的结构中有方法,那就更有意义...



所以,回顾一下...



public =可访问包含类型内外的任何人。

protected =可访问包含类型内的任何人或从包含类型派生的任何人。

internal =就像包含程序集中的public一样,但就像对程序集之外的任何人一样私有。

[注意,如果有一个excelption案例内部成员: InternalsVisibleTo 使程序集的内部成员可见并可用于其他程序集。]

private =只能访问包含类型内的任何人,不能在其外部访问。


访问说明符定义谁可以在定义它的类型之外看到它。由于您的struct位于包含类型内并标记为private,因此无法在包含类型(Helper)之外看到它。由于结构的成员是公共的,因此可以在包含类型(材料)之外看到它们,但如果它们被标记为私有,则在包含类型(材料)之外不能看到它们。



所以是的,它确实看起来很奇怪,但你已经习惯了它,拥有私人类型和公共成员。您正在定义谁可以看到它。


请看这里:

如何:了解传递结构和将类引用传递给方法之间的区别(C#编程指南) [ ^ ]

类和结构(C#编程指南) [ ^ ]



正如我在问题评论中所提到的, Material1 Material2 应该是 Helper 类的私有成员。

请注意,您的示例代码甚至不会编译,因为您将Struct声明为private,并且您不能在Namespace的范围内声明私有元素:Elements在命名空间中定义的内容不能显式声明为private,protected或protected internal。



即使你在Helper类中找到了Struct,你也需要声明它的内部字段公开供Helper Class访问它们;但是,那么Struct将是Helper Class的私有...... ...我认为你所追求的是:

  public  静态 助手
{
public static int GetMaterialWeight()
{
// 返回Material.Material1;
返回 Material.Material2; // 有效!

}

< span class =code-comment> // 材料在Helper之外不可见
private struct 材料
{
public const int Material1 = 10 ;
public const int Material2 = 20 ;
}
}

还有其他方法可以声明Struct,在Helper Class 中,这将使它只能由Helper类使用:

  private   struct 材料
{
internal const int Material1 = 10 ;
internal const int Material2 = 20 ;
}

如果您愿意,可以声明没有访问修饰符的Struct:这相当于将其声明为私有。


Why do the constants in the struct Material have to be public?
If I make them private they are not accessible with the rest of the class.
Material1 is public within the private struct and is available in method GetMaterialWeight, it looks strange.
Thanks,
Robert

namespace Cars
{
    public static class Helper
    {
        public static int GetMaterialWeight()
        {
            return Material.Material1; 
            //return Material.Material2; --> this is not possible (why?)
  
        }
    }

    private struct Material
    {
        public const int Material1 = 10;  
        private const int Material2 = 20; 
    }
}

解决方案

Private means that its only accessible to the containing type. Since Material2 is contained by Material type, if you make it private, it can only be accessed by anything inside the material struct.

This makes more sense if you have methods inside your struct...

So, to recap...

public = Accessible to anybody inside or outside of the containing type.
protected = Accessible to anybody inside the containing type or anybody that derives from the containing type.
internal = Just like public inside the containing assembly, but just like private to anybody outside of the assembly.
[Note, there is an excelption in case of internal members: InternalsVisibleTo makes internal members of assembly visible and usable to other assemblies.]
private = Accessible to anybody inside the containing type only, not accessible outside of it.

Access specifiers define who can see it outside of the type that defines it. Since your struct is inside a containing type and marked private, it cannot be seen outside of the containing type (Helper). Since the members of the struct are public, they can be seen outside of the containing type (Material), but if they are marked private, they cannot be seen outside of the containing type (Material).

So yes, it does "look weird", but you get used to it, having a private type and public members. You are defining who can see it.


Please, have a look here:
How to: Know the Difference Between Passing a Struct and Passing a Class Reference to a Method (C# Programming Guide)[^]
Classes and Structs (C# Programming Guide)[^]

As i mentioned in the comment to the question, Material1 and Material2 should be private member of Helper class.


Note that your example code will not even compile, since you declared the Struct as private, and you cannot declare a private element within the scope of a Namespace: "Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal."

Even if you locate the Struct inside the Helper Class, you will need to declare its internal Fields public for the Helper Class to access them; but, then the Struct will be private to the Helper Class ... which I think is what you are after:

public static class Helper
{
    public static int GetMaterialWeight()
    {
        //return Material.Material1;
        return Material.Material2; // works !

    }

    // Material is not "visible" outside Helper
    private struct Material
    {
        public const int Material1 = 10;
        public const int Material2 = 20;
    }
}

There are other ways you could declare the Struct, inside the Helper Class, which will render it usable only by the Helper Class:

private struct Material
{
    internal const int Material1 = 10;
    internal const int Material2 = 20;
}

You can declare the Struct with no access modifiers, if you wish: that's equivalent to declaring it private.


这篇关于带有struct和private const的静态类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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