如果已将私有变量标记为私有变量,该怎么办? [英] What for should I mark private variables as private if they already are?

查看:107
本文介绍了如果已将私有变量标记为私有变量,该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class Foo 
{
私人字符串栏;
}

class Foo
{
string bar;
}

我猜这两个声明是一样的。



所以我的问题是:如果私有变量已经是私人的,那么我应该如何将私有变量标记为 private

解决方案

我已经在围栏上一段时间了。我曾经争辩说是隐含的,但现在我觉得我倾向于明确表达。



隐藏的原因:




  • 这意味着非私人会员(或者比默认访问权限更多的内容)有更大的差异;这显示了阅读代码时的区别



显示原因:




  • 某些开发人员可能不知道默认值:明确表示

  • 它表明您已经积极地作出了决定,而不是仅仅将其保留为默认值。



这些后一点是基本上是由Eric Lippert制作的,当我们之前讨论过的时候。


As far as I know, in C# all fields are private for default, if not marked otherwise.

class Foo
{
  private string bar;
}

class Foo
{
  string bar;
}

I guess these two declarations are equal.

So my question is: what for should I mark private variables as private if they already are private?

解决方案

I've been on the fence for a while about this. I used to argue for leaving it implicit, but now I think I'm tipped over towards making it explicit.

Reasons for leaving it implicit:

  • It means there's a bigger difference for non-private members (or anything with more access than the default); this highlights the difference when reading the code

Reasons for making it explicit:

  • Some developers may not know the defaults: making it explicit means it's clear to everyone
  • It shows you've actively made a decision, rather than just leaving it up to the default

These latter points are basically the ones made by Eric Lippert when we discussed it a while ago.

这篇关于如果已将私有变量标记为私有变量,该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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