C# 中的默认访问修饰符

发布于 2024-09-18 00:09:05 字数 98 浏览 5 评论 0原文

如果我将创建一个如下所示的新对象,默认情况下它将具有哪个访问修饰符?

Object objectA = new Object();

If I will create a new object like the following, which access modifier will it have by default?

Object objectA = new Object();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷情妓 2024-09-25 00:09:06

任何成员都将始终拥有最严格的可用成员 - 因此在这种情况下,objectA 的可访问性是private。 (假设它是一个实例变量。它作为局部变量没有任何意义,因为它们没有任何访问规则。)

因此:

class Foo
{
    Object objectA = new Object();
}

相当于:

internal class Foo
{
    private Object objectA = new Object();
}

“默认为大多数私有”意味着对于类型来说,可访问性取决于上下文。这:

class Outer
{
    class Nested
    {
    }
}

相当于:

internal class Outer
{
    private class Nested
    {
    }
}

... 因为你不能有一个私有的非嵌套类。

只有一个地方添加显式访问修饰符可以使某些内容比没有时更加私有,那就是在属性声明中:

public string Name { get; set; } // Both public

public string Name { get; private set; } // public get, private set

Any member will always have the most restrictive one available - so in this case the accessibility of objectA is private. (Assuming it's an instance variable. It makes no sense as a local variable, as they don't have any access rules as such.)

So this:

class Foo
{
    Object objectA = new Object();
}

is equivalent to this:

internal class Foo
{
    private Object objectA = new Object();
}

The "default to most private" means that for types, the accessibility depends on the context. This:

class Outer
{
    class Nested
    {
    }
}

is equivalent to this:

internal class Outer
{
    private class Nested
    {
    }
}

... because you can't have a private non-nested class.

There's only one place where adding an explicit access modifier can make something more private than it is without, and that's in property declarations:

public string Name { get; set; } // Both public

public string Name { get; private set; } // public get, private set
梦中的蝴蝶 2024-09-25 00:09:06
void Foo()
{
    // private in method scope
    Object objectA = new Object();
}

class Bar()
{
    // private in class scrope
    Object objectA = new Object();
}
void Foo()
{
    // private in method scope
    Object objectA = new Object();
}

class Bar()
{
    // private in class scrope
    Object objectA = new Object();
}
说谎友 2024-09-25 00:09:06

默认情况下它是私有的。

َََََ

It is private by default.

َََََ

再见回来 2024-09-25 00:09:06

作为类成员:private

如果它是在方法体内声明的局部变量,则在该方法之外无法访问它。但我猜你已经知道了。

As a class member: private.

If it's a local variable declared within the body of a method, it has no accessibility outside that method. But I'm guessing you already knew that.

情话难免假 2024-09-25 00:09:06

对于类成员和结构成员(包括嵌套类和结构),私有是默认值。

对于类和结构 - 内部是默认值

您可以查看 MSDN供进一步阅读..

For class members and struct members, including nested classes and structs, private is the default.

For classes and structs - internal is the default

You can check out MSDN for further reading..

心如狂蝶 2024-09-25 00:09:06

类/类型本身将默认为“内部”。您创建的对象将默认为“私有”。

The class/type itself will default to "internal". The object you create will default to "private".

素手挽清风 2024-09-25 00:09:06

类和结构默认声明为内部!

了解更多信息此处

Classes and structs are declared as internal by default!

Read more here

梦途 2024-09-25 00:09:06

类的访问说明符是内部的。

变量的访问说明符是私有的。

The access specifier of class is internal.

The access specifier of a variable is private.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文