内部VS大众在C# [英] internal vs public in c#

查看:112
本文介绍了内部VS大众在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道之间的差异公开内部可见性修饰符。

I want to know the difference between the public and internal visibility modifiers.

当我们要使用内部类上,当公开?我很困惑。当一个方法应该是公开内部

When should we use internal on a class and when public? I am confused with when a method should be public or internal.

我读内部可通过大会访问,而公开,也可以通过组装,其中使用不同之处在于。

I read that internal can be accessed through the Assembly, while public can also be used through the assembly where the difference lies.

推荐答案

公开是从哪里看到。

内部是可见的,只有装配在

internal is visible only within an assembly

您倾向于使用内部只是为了保护内部API。例如,你可以公开的方法的几个重载:

You tend to use internal only to protect internal APIs. For example, you could expose several overloads of a method:

public int Add(int x, int y)
public int Add(int x,int y, int z)

这两者调用内部方法

Both of which call the internal method

internal int Add(int[] numbers)

您就可以把很多复杂的一个方法,但保护它使用的门面方法,可以帮助程序员正确地调用该方法。 (实施方法与阵列参数可以具有值的任意限制,例如。)

You can then put a lot of sophistication on a method, but "protect" it using facade methods that may help the programmer to call the method correctly. (The implementation method with the array parameter may have an arbitrary limit of values, for example.)

另外值得一提的是,使用反射,任何和所有的方法都是调用无论其知名度。另一位黑客来控制内部隐藏的API /增益访问。

Also worth noting that using Reflection, any and all methods are callable regardless of their visibility. Another "hack" to control/gain access to internally hidden APIs.

这篇关于内部VS大众在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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