使用私有自动属性代替编程标准的简单变量 [英] Using a private auto property instead of a simple variable for a programming standard

查看:78
本文介绍了使用私有自动属性代替编程标准的简单变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在与同龄人的讨论中,我们认为应该考虑对所有类级别变量使用自动属性,包括私有变量.

In a discussion with a peer, it was brought up that we should consider using auto properties for all class level variables... including private ones.

因此,除了像这样的公共财产:

So in addition to a public property like so:

public int MyProperty1 { get; set; }

我们的私有类级变量如下所示:

Our private class-level variables would look like this:

private int MyProperty2 { get; set; }

代替:

private int _myProperty2;

我对为什么有人要这么做的想法持怀疑态度,但是我不能决定我是否不愿意接受这是因为我自己内部洗脑与如何按照相同的编程标准和命名约定编写代码我已经使用了10年,或者因为以前从未见过(出于某种原因).

I'm on the fence about why someone would want to do this but I can't decide if my reluctance to accept this is because of my own internal brainwashing with how I write my code to the same programming standards and naming conventions I've used for 10 years or because I've never seen this before (for a reason).

我意识到这是键入额外的代码,但是老实说,当使用自动属性时,我认为由于"prop"和"propg"代码段我从来没有键入过它,所以它非常设置新片段以创建私有自动属性很简单,因此多余的代码不会给我带来太多麻烦,因为我永远不必键入它.

I realize it's extra code to type but to be honest, when using auto-properties, I don't think I've ever typed it out due to the 'prop' and 'propg' snippets so it'd be very simple to set up a new snippet to create a private auto property so the extra code doesn't bother me too much since I never have to type it.

除了美学可能只是我的潜意识之外,使用完全私有的汽车属性还会导致任何问题吗?是否有充分的理由这样做或不这样做?那天,我在stackoverflow,codeplex,codeproject等上看过很多代码,但是我从未见过有人使用此标准....有什么理由吗?

Other than aesthetics which may just be my subconscious, are there any issues that could result from using fully private auto properties? Are there any good reasons to do this or not to do it? I've seen a lot of code in my day on stackoverflow, codeplex, codeproject, etc. and I've never seen anyone use this standard.... is there a reason why?

推荐答案

这没有太大意义.

我可以想到一个好处":

I can think of a 'benefit':

  • 您以后可以在getter和/或setter中添加逻辑,并确保始终将其传递

但是坦率地说,您的类不应太大而不能用.

but frankly your classes should not become so big that this is useful.

有任何问题"吗?

"are there any issues" ?

您的属性不能用作refout参数的参数.

Your properties won't work as arguments to ref or out parameters.

这篇关于使用私有自动属性代替编程标准的简单变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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