简体   繁体   English

C#是否具有相当于Delphi的私有修饰符

[英]Does C# have the equivalent of Delphi's private modifier

In Delphi, the private modifier is likely unique: 在Delphi中, private修饰符可能是唯一的:

Contoso = class
private
   procedure DoStuff();
end;

you would think has the C# equivalent of: 你会认为C#相当于:

class Contoso
{
   private void DoStuff()
   {
   }
}

But in Delphi, the private keyword is more unit friend . 但在Delphi中, private关键字更像是单位朋友 In other words, other classes in the same code file can access private members. 换句话说, 同一代码文件中的其他类可以访问私有成员。 Transcoding Delphi to C#, it would be the equivalent of the following code working: 将Delphi转码为C#,它将等效于以下代码:

public class Contoso
{
   private void DoStuff()
   {
   }
}

internal class Fabrikam
{
   Contoso _contoso;

   //constructor
   Fabrikam()
   {
      _contoso = new Contoso();
      _contoso.DoStuff(); //i can call a private method of another class
   }
}

Even though the method DoStuff is private to Contoso , other classes in the same file can call the method. 尽管DoStuff方法对Contoso是私有的,但同一文件中的其他类可以调用该方法。

What i don't want is to make the method internal : 我不想要的是使方法internal

class Contoso
{
   internal void DoStuff();
}

because then other code in the assembly can see or call the DoStuff method; 因为然后程序集中的其他代码可以看到或调用DoStuff方法; which i don't want. 这是我不想要的。


Does C# support some sort of unit friend or unit internal access modifier? C#是否支持某种单位朋友单位内部访问修饰符?

Not exactly. 不完全是。

In C# you can declare a member as internal , which gives classes (code) in the same assembly access to that member. 在C#中,您可以将一个成员声明为internal ,它在同一个程序集中为该成员提供类(代码)。 This has more or less the same usage as Delphi's Unit access rule. 这与Delphi的单元访问规则或多或少具有相同的用法。

The scope is wider but you have more control. 范围更广,但您有更多的控制权。

You specifically state you don't want this, but think about the use cases. 您明确声明您不希望这样,但请考虑用例。 In Delphi you are unwillingly giving access to all private members. 在Delphi中,您不情愿地允许所有私人成员访问。 And you are prevented from putting each class in its own Unit. 而且你被阻止将每个班级都放在自己的单位中。

In .NET, an assembly is 1 project and managing unwanted acces inside one assembly is easy. 在.NET中,程序集是一个项目,并且在一个程序集中管理不需要的访问很容易。 Putting up a fence to the rest of the world is much more important. 为世界其他地区设置围栏更为重要。

You cannot limit on a file boundary because a file has almost no significance in C#. 您不能限制文件边界,因为文件在C#中几乎没有意义。 A file boundary is not present in IL, and a rule like that could conflict with partial classes for one thing. IL中不存在文件边界,这样的规则可能与部分类冲突。

The file a class is defined in doesn't really mean that much in C# - you can have multiple classes in one file that are in different namespaces and you can have classes split across multiple files (partial classes). 定义类的文件在C#中实际上并不是那么多 - 您可以在一个文件中有多个类,这些类位于不同的命名空间中,您可以将类拆分为多个文件(部分类)。

The same level of access that you have in delphi is not present in C# - in C# you can give access to an inherited class via protected, a class in the same assembly via internal or within the same class and nested classes via private. 在C#中没有与delphi中相同的访问级别 - 在C#中,您可以通过protected访问继承的类,通过内部或同一类在同一程序集中访问类,通过private访问嵌套类。

Perhaps what you're looking for is a nested class - it's hard to tell without a concrete example. 也许您正在寻找的是嵌套类 - 如果没有具体的例子,很难说清楚。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM