我可以将此方法更改为私有吗? [英] can I change this method to private?

查看:80
本文介绍了我可以将此方法更改为私有吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面定义了我的课程。如果我改变Assign方法从内部到私有的可见性,这个改变的好处和坏处是什么?



方法Assign()仅在此类中使用。< br $> b $ b

I have defined my class below. if I change visibilty for Assign method from internal to private, what is good and bad side of this change?

method Assign() is only used inside this class.

public class AxisCap
 {
    internal AxisCap (string initString)
     {
         this.Assign (initString);
     }

    internal void Assign (string initString)
     {
      //omitted contents here
 }
  }





感谢您的回答。我改变的真正意图是在将此方法更改为私有时对其进行模糊处理。



thanks for the answers. the real intention of my change is to obfuscate this method when it is changed to private.

推荐答案

只需阅读主题:https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx [ ^ ]。



问题是访问修饰符的改变总是荒谬的。让我们说,私人对于你想保密的成员来说是一个很好的修饰符。 :-)等等。



-SA
Just read on the topic: https://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx[^].

The question is the change of access modifier it total absurd. Let's say, "private" is a good modifiers for members you want to keep private. :-) And so on.

—SA


As 内部标记的类或类成员可从它们所属的同一命名空间程序集内部访问。它们在命名空间程序集中是准公共的。这是一个相对较少使用的访问修饰符,在使用它时应注意不要滥用其几乎公开的性质。



As 私人标记的班级成员只能从他们所属的班级内访问。



选择这个班级没有任何内在的好或坏一个或另一个 - 它只是取决于。你的分配 -Method只能由类 AxisCap 使用?然后将其设为 private 。只应该 AxisCap 和从它继承的类能够使用它吗?然后将其设为 protected 。是否可以从同一命名空间程序集中的类访问它?设为内部。如果它可以从任何地方访问,请将其设为 public



您有一个想法是非常重要的解决方案的整体设计以及不同部分如何协同工作。始终使用最严格的修饰符。



编辑:三振出局
As internal marked classes or class-members are accessible from inside the same namespace assembly they belong to. They're quasi public within that namespace assembly. It's a comparatively rarely used access modifier and care should be taken when to use it in order not to abuse its "almost-public" nature.

As private marked class-members are only accessible from within the class they belong to.

There's nothing inherently good or bad about choosing the one or the other - it just depends. Is your Assign-Method meant to be used only by the class AxisCap? Then make it private. Should only AxisCap and classes inheriting from it be able to use it? Then make it protected. Should it be accessible from classes within the same namespace assembly? Make it internal. Should it be accessible from anywhere, make it public.

It's just important that you have an idea for an overall design of your solution and how the different parts work together. Always use the most strict modifier possible.

strikeouts


您可以将其更改为私人的。如果这样做,则使用此类的其他类(在同一名称空间内)将无法访问此方法。
You can change it to private. If you do, this method will not be accessible to other classes (within the same namespace) that use this class.


这篇关于我可以将此方法更改为私有吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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